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:
This will create a bash variable called TS with a value of current date and time:Code:$ TS=$( date +%Y%m%d%H%M )
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:$ echo $TS 201101230708
This will rename all files in your current directory to:Code:$ for file in $( ls ); do mv $file $TS-$file; done
Of course you can combime all commands in a single command:Code:201101230708-myfilename
Hope this helps.Code:$ for file in $( ls ); do mv $file $( date +%Y%m%d%H%M )_$file; done


Reply With Quote
