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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the date in a copy command 3

Status
Not open for further replies.

bazil2

Technical User
Feb 15, 2010
148
DE
(Elementary user)

I have some log files that are named like this:

logfile_August 2010.txt
logfile_September 2010.txt
logfile_October 2010.txt

I would like to write a small script that would copy the current log (i.e. the month we are in) to another location.

Can I use the date and year flag to do this?

Here is my script in layman's terms:

cp -p /source/logfile_date(%B %Y).txt /target/current_logfile.txt

Alternatively, is there a copy flag that will select the logfile with the 'youngest' creation date?

Best regards
 
Do your filenames really have spaces in them?

ls -r | tail -1 will give you the most recent file (this assumes there are only log files in the directory), so you could do something with output of that to rename the file.

The internet - allowing those who don't know what they're talking about to have their say.
 
If you have more files:

ls /source/ | grep `date +%B` | while read i
do
cp /source/$i /destination/
done
 
may I suggest this?
cp -p "/source/logfile_$(date '+%B %Y').txt" /target/current_logfile.txt

Best Regards, Franz
--
System Manager (Solaris, HP-UX, Linux, some networking, some SAN)
 
Hey daFranze, haven't seen you around for a while - welcome back!

The internet - allowing those who don't know what they're talking about to have their say.
 
thanks Ken for the warm welcome back, pleased to meet you again!

thanks bazil2 for the star!

Best Regards, Franz
--
System Manager (Solaris, HP-UX, Linux, some networking, some SAN)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top