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 xyz@abc.com:~/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.

link|improve this question

67% accept rate
feedback

5 Answers

up vote 4 down vote accepted

try

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

link|improve this answer
This worked . Thanks!! – yogsma Sep 21 '10 at 20:35
1  
Quotes are recommended: mv "$fname" "./archive/$(basename "$fname")-$(date +%Y%m%d-%T)" – Dennis Williamson Sep 21 '10 at 20:47
feedback

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.

link|improve this answer
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
feedback

Add a basename to Warner's answer:

 mv $fname ./archive/`basename $fname`-`date +%Y%m%d_%H%M`
link|improve this answer
feedback

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

link|improve this answer
feedback

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

link|improve this answer
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
show 1 more comment
feedback

Your Answer

 
or
required, but never shown

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