My filename will be in date format. eg: 20160920-A.cc1. Everyday I will getting different file, with the current date (yyyymmdd-A.cc1). I need to archival this file with the same name that is loaded and also with date and timestamp as a suffix to the file.

So my input is like 20160920-A.cc1 and output that is moved to another folder and that should be like this 20160920-A.cc1_20160920-hh:mm:ss

  • Should the suffix be the actual time and date or should it be the first part of the name repeated? For example, if you receive a file called ` 20160920-A.cc1` at 1 millisecond before midnight, you will then be processing it on 2016/09/21 and not 2016/09/20. Should the new file be called 20160920-A.cc1_20160920-hh:mm:ss or 20160920-A.cc1_20160921-hh:mm:ss?
    – terdon
    Commented Sep 20, 2016 at 11:39

3 Answers 3


You just need to understand date command in detail. see date --help.
In your case to archive as a tarball use,

tar czf filename_`date +%Y%m%d_%H_%M_%S`.tar.gz filename


tar czf 20160920-A.cc1_`date +%Y%m%d_%H_%M_%S`.tar.gz 20160920-A.cc1  

colon : is not a valid file name hence used underscore_ instead for hh:mm:ss.


I think working with the date command should do the trick.

echo some fancy data for the file > $(date +"%Y%m%d")-A.cc1
cp $(date +"%Y%m%d")-A.cc1 <another folder path>/$(date +"%Y%m%d")-A.cc1_$(date +"%Y%m%d-%H:%M:%S")

If you want to copy the file on a different date that it was created on - use the following:

cp {,<another path>/}20160919-A.cc1
mv <another path>/20160919-A.cc1{,_$(date +"%Y%m%d-%H:%M:%S")}

Important thing: Use a loop if you want to copy multiple files


Thanks Tejas & Keocra.

Also I tried like below format. But it will work for the current day only.

mv 20160919-A.cc1 `date "+%Y%m%d"'-A.cc1_`date +%Y%m%d_%H_%M_%S`

And my OutPut is:

  • NOTE: This will pick move the file with the current date as prefix and suffix not a specific date of the input file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .