Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UNIX Date stamped files 1

Status
Not open for further replies.

Helfenmir

MIS
Sep 17, 2001
61
GB
How do you a copy a file from one directory to another and make the system automatically generate an extension or filename (when in the target directory) that equates to the date it was copied. Date format example: file.20010917 or/and 20010917.
 
Does this do what you want?

cp file newdir/file.`date +"%Y%m%d"`

The command in backward apostrophes -- `<cmd>` -- runs the command and uses the output of that command on the command line.

See the date man pages for more info on formatting the output of date.

Hope this helps!

- Steve StevieW85@go.com
 
define a variable with the following syntax :
date_formatted=`date +%Y%m%d`
then do the following :
mv myfile myfile.$date_formatted

If you want this done all the time, create an alias for rm
that will do the above
 
Ifoata is assuming that the reason for the rename is to allow undeletes. Therefore, alias rm to move the file rather than to actually delete it. At some point, you would then need to do a clean up where the old files are actually deleted. I don't have any idea why Helfenmir is trying to do the rename, but it would make sense that if it is to replace rm, the date could be used to have a cron job periodically remove files older than x days old.

- Steve StevieW85@go.com
 
Hi All

Many thanks for your help I've got that one covered now.

Helfenmir
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top