1832865 Members
2843 Online
110048 Solutions
New Discussion

Re: Renaming Files

 
SOLVED
Go to solution

Renaming Files

I am trying to write a small script that periodically renames a file and appends the date to the end of the new file name. For example, I want to rename 'file' to 'file.06/02/2002'. Unfortunatly I can not get this to work. If anyone could help me out I would be really greatfull.

thanks in advance,

Christian Briddon
10 REPLIES 10
Patrick Wallek
Honored Contributor

Re: Renaming Files

Try using this command in your script:

mv filename filename.`date +%d%m%y`

Note that the ` marks are the backwards tick marks.
A. Clay Stephenson
Acclaimed Contributor

Re: Renaming Files

Hi Christain,

If you man the date command, it will guide you through exactly what you need. However, are you sure that you want file.06/02/2002 rather than something like file.06-02-2002. You method will create a subdirectory, is that what you want?
If it ain't broke, I can fix that.
Victor BERRIDGE
Honored Contributor

Re: Renaming Files

Hi,
It should work except the for logfiles etc...
In such case why not copy the file, append the date and zeroe your file:
cp -p file file.;
date>>file.;
>file

No garanty, just thoughts...

All the best

Victor
S.K. Chan
Honored Contributor

Re: Renaming Files


# mv file file.$(date +%d%m%y)
James R. Ferguson
Acclaimed Contributor

Re: Renaming Files

Hi:

Try this:

# mv /tmp/me /tmp/me.`date +%d_%m_%Y`

Remember to avoid the usual "/" in the date since this would constitute additional directory levels.

Regards!

...JRF...
Justo Exposito
Esteemed Contributor

Re: Renaming Files

Hi Cristian,

As Patrick tell you, you can do:
mv filename filename.`date +%d%m%y`
and then echo `date` >> filename.`date +%d%m%y`

Regards,

Justo.
Help is a Beatiful word
Steve Steel
Honored Contributor
Solution

Re: Renaming Files

Hi


As everybody already said

no / it is not possible.

You can even add time in the date incase you need to rename more than once a day.

cp file file..$(date '+%d-%m-%y_%H:%M:%S')

Shame. But choose another character in the date.

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Steven Sim Kok Leong
Honored Contributor

Re: Renaming Files

Hi,

Personally, I prefer

# mv logfile logfile.`date +%Y%m%d`

This makes sorting the logfiles in chronological order much easier.

Hope this helps. Regards.

Steven Sim Kok Leong

Re: Renaming Files

Thanks for all your help. I have assigned points to you all.

Cheers,

Christian
Peter Kloetgen
Esteemed Contributor

Re: Renaming Files

Hi Christian,

the solution for your problem is very easy:

mv oldname oldname.`date +%d.%m.%y`

It is better to use dots as separators than slashes because these could be interpreted as pathes. You can use each character as seperator but allways be careful with special characters for the reason the shell could interpret them!

I hope this will help you, best regards

Peter Kloetgen
I'm learning here as well as helping