views:

322

answers:

4

I want add the date next to a filename ("somefile.txt"). For example: somefile_25-11-2009.txt or somefile_25Nov2009.txt or anything to that effect

Maybe a script will do or some command in the terminal window. I'm using Linux(Ubuntu).

Thanks in advance.

oh i almost forgot to add that the script or command should update the filename to a new date everytime you want to save the file into a specific folder but still keeping the previous files. So there would be files like this in the folder eventually: filename_18Oct2009.txt , filename_9Nov2009.txt , filename_23Nov2009.txt

+1  A: 

You can use backticks.

$ echo "myfilename-"`date +"%d-%m-%Y"`

Yields:

myfilename-25-11-2009
The MYYN
This appends the date to the filename - in the example in the question the date needs to go *between* the file name and the extension.
Dave Webb
A: 
cp somefile somefile_`date +%d%b%Y`
Soufiane Hassou
+5  A: 

There's two problems here.

1. Get the date as a string

This is pretty easy. Just use the date command with the + option. We can use backticks to capture the value in a variable.

$ DATE=`date +%d-%m-%y`

You can change the date format by using different % options as detailed on the date man page.

2. Split a file into name and extension.

This is a bit trickier. If we think they'll be only one . in the filename we can use cut with . as the delimiter.

$ NAME=`echo $FILE | cut -d. -f1
$ EXT=`echo $FILE | cut -d. -f2`

However, this won't work with multiple . in the file name. If we're using bash - which you probably are - we can use some bash magic that allows us to match patterns when we do variable expansion:

$ NAME=${FILE%.*}
$ EXT=${FILE#*.}

Putting them together we get:

$ FILE=somefile.txt             
$ NAME=${FILE%.*}
$ EXT=${FILE#*.} 
$ DATE=`date +%d-%m-%y`         
$ NEWFILE=${NAME}_${DATE}.${EXT}
$ echo $NEWFILE                 
somefile_25-11-09.txt

And if we're less worried about readability we do all the work on one line (with a different date format):

$ FILE=somefile.txt  
$ FILE=${FILE%.*}_`date +%d%b%y`.${FILE#*.}
$ echo $FILE                                 
somefile_25Nov09.txt
Dave Webb
Thanks Dave but How or where do i run this script? Forgive me i'm new with Linux.
sami
Can you give some more details on precisely what you want to do. So you have a file in a directory which you want to date stamp in this way. Is it just one file with the same name each time or could it be a number of files with a number of different name?
Dave Webb
i edited my question on top, please have a look at it.
sami
I'm still not clear on what you want to do. It says the script should update the filename "everytime you want to save the file into a specific folder". What is saving the file into the folder? Where is it coming from?
Dave Webb
A: 

a bit more convoluted solution that fully matches your spec

echo `expr $FILENAME : '\(.*\)\.[^.]*'`_`date +%d-%m-%y`.`expr $FILENAME : '.*\.\([^.]*\)'`

where first 'expr' extracts file name without extension, second 'expr' extracts extension

catwalk