Transfered from Linux Config:

Question:


Hi is there a way to rename all files in a directory and prefix a time-stamp to all files? thanks

Answer:


For that you can use a following set of commands:

First declare a time-stamp variable in a format to fit your needs. For example:

Code:
$ TS=$( date +%Y%m%d%H%M )
This will create a bash variable called TS with a value of current date and time:

Code:
$ echo $TS
201101230708
Feel free to modify a timestamp to fit your needs. When ready navigate to a directory in where you wish to rename all files by appending a time-stamp prefix and execute a following command:

Code:
$ for file in $( ls ); do mv $file $TS-$file; done
This will rename all files in your current directory to:

Code:
201101230708-myfilename
Of course you can combime all commands in a single command:

Code:
$ for file in $( ls ); do mv $file $( date +%Y%m%d%H%M )_$file; done
Hope this helps.