I need to move a file to an archive folder, and add a timestamp in front of the file name.

mv /dev/DataStage/myProject/source/TEST/MyFile_*.csv 

moved MyFile_20180817.csv as My~1.csv instead of MyFile_20180817.csv

When I move the file to the archive folder, I also need to add a time stamp in the front of the file name, for example: MyFile_20180817.csv to 20180817_1057.MyFile_20180817.csv:

mv /dev/DataStage/myProject/source/TEST/MyFile_*.csv 
/dev/DataStage/myProject/Archive/TEST/`date +%Y%m%d_%H%M`.MyFile_*.csv

moved MyFile_20180817.csv as 201808~1.CSV instead of MyFile_20180817.csv

Thank you.

  • Does it have to be an individual timestamp for each and every file, or would one single stamp calculated upfront do? – RudiC Aug 17 '18 at 21:33
  1. shorter with rename (it will fail in case of different Filesystems):

    rename '' "/dev/DataStage/myProject/Archive/TEST/`date +%Y%m%d_%H%M`." /dev/DataStage/myProject/source/TEST/MyFile_*.csv

  2. with loop

    for file in /dev/DataStage/myProject/source/TEST/MyFile_*.csv ; do filename=`basename $file` mv $file /dev/DataStage/myProject/Archive/TEST/`date +%Y%m%d_%H%M`.${filename} done

| improve this answer | |
  • Thank you I tried the shorter rename, and it gave me this error: rename: renaming /dev/DataStage/myProject/source/TEST/MyFile_20180730.csv to /dev/DataStage/myProject/Archive/TEST/20180817_1450./dev/DataStage/myProject/source/TEST/MyFile_20180730.csv failed: No such file or directory . So it looks like it appends "/dev/DataStage/myProject/source/TEST/" to the new file name. – faujong Aug 17 '18 at 19:00
  • When using the loop, I get the error "sh: -c: line 1: syntax error: unexpected end of file" – faujong Aug 17 '18 at 21:45
  • Stack exchange broke code pasting. Try this: pastebin.com/7keRSd8C – Alexander Aug 17 '18 at 21:53
  • Thank you, Alexander ! That works !!! Thank you so much !! – faujong Aug 20 '18 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.