Operating System - Linux
1752464 Members
5841 Online
108788 Solutions
New Discussion юеВ

Re: Renaming files within a script

 
SOLVED
Go to solution
Andrew Kaplan
Super Advisor

Renaming files within a script

Hi there --

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?
A Journey In The Quest Of Knowledge
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Renaming files within a script

Hi Andrew:

# 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...
Ivan Ferreira
Honored Contributor

Re: Renaming files within a script

>>> I would like to change that so that it renames the files with a date extension

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 {} \;
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Andrew Kaplan
Super Advisor

Re: Renaming files within a script

Hi there --

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.


A Journey In The Quest Of Knowledge
Ivan Ferreira
Honored Contributor
Solution

Re: Renaming files within a script

You need a space between date and +.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor

Re: Renaming files within a script

Hi (again):

> 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...