1

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.

#!/bin/bash
SERVER='abc.com'
USER='xyz'
PASSWD='lddkdkdkas'
find /directory/ -name ABC002*.csv | while read fname
do
   scp $fname [email protected]:~/XYZ/
   mv $fname ./archive/
done

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

5 Answers 5

4

try

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

1
  • 1
    Quotes are recommended: mv "$fname" "./archive/$(basename "$fname")-$(date +%Y%m%d-%T)" Sep 21, 2010 at 20:47
2

Try:

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.

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

Add a basename to Warner's answer:

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

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

1

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!)

6
  • 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, 2010 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. Sep 21, 2010 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, 2010 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: Sep 22, 2010 at 10:07
  • find /dir -iname BLAH -exec sh -c 'new_name=$(basename {}); scp {} user@host:dest/ && mv {} /archive/$new_name$(date..)' \; Sep 22, 2010 at 10:09

You must log in to answer this question.

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