Rename all files and prefix a time-stamp to all file names

Transfered from Linux Config Disqus comments:

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:

$ TS=$( date +%Y%m%d%H%M )

This will create a bash variable called TS with a value of current date and time:

$ 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:

$ for file in $( ls ); do mv $file $TS-$file; done

This will rename all files in your current directory to:

201101230708-myfilename

Of course you can combime all commands in a single command:

$ for file in $( ls ); do mv $file $( date +%Y%m%d%H%M )_$file; done

Hope this helps.