- Community Home
- >
- Servers and Operating Systems
- >
- Operating System - Linux
- >
- General
- >
- Renaming files within a script
-
- Forums
-
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
- HPE Blog, Austria, Germany & Switzerland
- Blog HPE, France
- HPE Blog, Italy
- HPE Blog, Japan
- HPE Blog, Middle East
- HPE Blog, Latin America
- HPE Blog, Russia
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
-
Blogs
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Blog, Latin America
- HPE Blog, Middle East
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
-
Information
- Community
- Welcome
- Getting Started
- FAQ
- Ranking Overview
- Rules of Participation
- Tips and Tricks
- Resources
- Announcements
- Email us
- Feedback
- Information Libraries
- Integrated Systems
- Networking
- Servers
- Storage
- Other HPE Sites
- Support Center
- Aruba Airheads Community
- Enterprise.nxt
- HPE Dev Community
- Cloud28+ Community
- Marketplace
-
Forums
-
Blogs
-
Information
-
English
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
01-24-2008 09:52 AM
01-24-2008 09:52 AM
I have a script that copies files from the localhost to a remote directory. Currently, the script overwrites the existing files in the remote directory. Here is an excerpt:
cd
rm -rf fstab
rm -rf exports
rm -rf hosts.equiv
rm -rf hosts
rm -rf passwd
rm -rf .rhosts
I would like to change that so that it renames the files with a date extension, ie: filename_24jan08, instead of overwriting them. The script uses the sh shell, but that can be changed if necessary. How can this be done?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
01-24-2008 10:15 AM
01-24-2008 10:15 AM
Re: Renaming files within a script
# mv myfile myfile.$(date +%d%b%Y)
...renames "myfile" to "myfile.24Jan2008" (or whatever is the current date.
If the file already exists on the date given, then it will be overwritten during the rename.
By the way, adding the recursive ('-r') switch to a 'rm' for *files* is meaningless.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
01-24-2008 10:19 AM
01-24-2008 10:19 AM
Re: Renaming files within a script
This could have negative effects, as if you don't control the number of "versions", you will have enless list of files.
To avoid this you can:
Name the file as
That would be:
$FILENAME-$(date +%a)
In this case, the date of the file will be identified by the timestamp of the file.
You can put a "find" in your script:
find /somewhere -mtime +7 -exec rm {} \;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
01-24-2008 10:38 AM
01-24-2008 10:38 AM
Re: Renaming files within a script
I went ahead and replaced the rm lines with the mv lines. This is the syntax that I used:
mv fstab fstab_$(date+%d%b%Y)
mv exports exports_$(date+%d%b%Y)
mv hosts.equiv hosts.equiv_$(date+%d%b%Y)
mv hosts hosts_$(date+%d%b%Y)
mv passwd passwd_$(date+%d%b%Y)
mv .rhosts .rhosts_$(date+%d%b%Y)
However, when I ran the script, the error messages shown below appeared on-screen:
./backup_script: line 1: date+%d%b%Y: command not found
./backup_script: line 1: date+%d%b%Y: command not found
./backup_script: line 1: date+%d%b%Y: command not found
./backup_script: line 1: date+%d%b%Y: command not found
./backup_script: line 1: date+%d%b%Y: command not found
./backup_script: line 1: date+%d%b%Y: command not found
I believe it is a syntax error, but I am not sure what it is.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
01-24-2008 10:45 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
01-24-2008 11:45 AM
01-24-2008 11:45 AM
Re: Renaming files within a script
> You need a space between date and +.
Had you copied-and-pasted the command I posted (albeit it as a generic example), you would have "seen" the whitespace.
Regards!
...JRF...
Hewlett Packard Enterprise International
- Communities
- HPE Blogs and Forum
© Copyright 2021 Hewlett Packard Enterprise Development LP