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

Assigning Dates to output file names 2

Status
Not open for further replies.

wilsonk

Programmer
Aug 9, 2001
7
US
I want to take a file and rename it with the current date: sample.txt > sample101601.txt

The date format is not crucial, though I probably need a time stamp as well. I simply need a unique identifier for logging purposes.

Any ideas are appreciated.

 
The following should work:

mv sample.txt "sample`date +%m%d%y`.txt"

For the time, check out 'man date'.
 
Good idea, but when I try it, Unix (or the shell) treats the command literally.

So, mv sample.txt "sample'date+%m%d%y'.txt" yields:

sample'date+%m%d%y'.txt

I'm wondering if Unix (or the shell) can handle system variables in renaming commands. I haven't found any documentation on something like this so far.
 
The back ticks do the trick.

Thank you both!
 
Talking about back ticks... Back ticks are good - but difficult to "nest"

say that you want to find where the command 'ls' is and then change to that directory (silly example, I know, but bear with me)

with backticks you would have to do this

lspath=`whence ls`
lsdir=`dirname $lspath`
cd $lspath

but you can do it in one step like this

cd $(dirname $(whence ls))

with the added advantage that you don't mistake ' for ` or vice versa

the example above using $() would look like this

mv sample.txt "sample$(date+%m%d%y).txt" Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Very good Mike.

Being curious, I cut and pasted your code and tried it. It didn't work because there is a missing space after date.
Code:
mv sample.txt "sample$(date +%m%d%y).txt"
                           ^
                        space here
 
you got me <grin>

thx raider Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Genius ;-) That's got to be one of the most useful pieces of information I've come across in a long time!

Greg.
 
I'm just a mine of technical trivia :) thx Greg Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top