- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Renaming files within a script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- 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
- 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
- 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
- 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
- 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
- Report Inappropriate Content
тАО01-24-2008 10:45 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- 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...