Here is my script which I have created for transferring particular types of files from directories. I am struggling to find a way to move the file to archive directory with current time stamp.

find /directory/ -name ABC002*.csv | while read fname
   scp $fname xyz@abc.com:~/XYZ/
   mv $fname ./archive/

Here I can move the file to archive, but I am not able to move it with name changed to filename *timestamp.



mv $fname ./archive/$(basename $fname)-$(date +%Y%m%d-%T)

  • 1
    Quotes are recommended: mv "$fname" "./archive/$(basename "$fname")-$(date +%Y%m%d-%T)" – Dennis Williamson Sep 21 '10 at 20:47


mv $fname /tmp/`basename $fname`-`date +%Y%m%d_%H%M`

You can use variations of this theme if you prefer different timestamps or filenames. Be aware that this solution is not robust enough to handle multiple filenames.

  • The problem with this is that $fname is a complete path of file. – yogsma Sep 21 '10 at 20:07
  • My mistake, answered too quickly. Corrected. (Before anyone else did.) =) – Warner Sep 21 '10 at 20:25

Add a basename to Warner's answer:

 mv $fname ./archive/`basename $fname`-`date +%Y%m%d_%H%M`

Try this

mv $fname ./archive/`date +%m%d%y-%T`-$fname

There are single backticks in front of date and after T They do not show up in post for some reason


You might need to use the basename command to strip the complete file path.

Also you might want to use an array to generate a list of files to move later; or even to use the -exec option of find, something like:

find /dir -iname BLAH -exec sh -c 'new_name=$(basename {}); scp {} user@host:dest/ && mv {} /archive/$new_name$(date..)' \;

(don't forget the \; at the end)

That line archives the file only after (&&) a successful copy over ssh.

Another hint: perhaps you want to tar the files and send a compressed archive over the network? This might save you the ssh handshaking time (and use ssh keys! don't save passwords in scripts you will leave around!)

  • Thanks for suggestion, that password thing was default there in my script when I was trying to using password login to remote server, but I have adapted to passwordless login scheme using public key. – yogsma Sep 21 '10 at 20:36
  • That won't work at all. You need to use sh -c or bash -c and some quoting. You're missing a dollar sign on a variable. There's no need for a variable that you're only using once. Just use basename directly where it's needed. – Dennis Williamson Sep 21 '10 at 20:46
  • @Dennis fixed typo. The extra variable name is for clarity and for hope it could be used later on (say, print it in a log file). Anything else won't work? – lorenzog Sep 22 '10 at 7:18
  • Most shells won't accept spaces around the equal sign. The parentheses would create a subshell (if they didn't cause an error) which means the variable's value won't be available outside it. find can't set variables in its -exec. You need to use sh -c or bash -c to make the variable assignment and expansion work, but that also sets up a subshell so the variable's value won't be available outside it. The variable near the end will be expanded before the find is run, not during, since it's not escaped. I think that's a complete list. The following might work: – Dennis Williamson Sep 22 '10 at 10:07
  • find /dir -iname BLAH -exec sh -c 'new_name=$(basename {}); scp {} user@host:dest/ && mv {} /archive/$new_name$(date..)' \; – Dennis Williamson Sep 22 '10 at 10:09

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.