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 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.

yogsma
  • 245
  • 1
  • 3
  • 12

5 Answers5

4

try

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

user9517
  • 115,471
  • 20
  • 215
  • 297
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.

Warner
  • 23,756
  • 2
  • 59
  • 69
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

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
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!)

lorenzog
  • 2,799
  • 3
  • 20
  • 24
  • 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 07: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
  • @Dennis: thanks. I fixed it. Next time I promise I'll test it first, or wait til I have a shell to try it on. – lorenzog Sep 24 '10 at 08:28
1

Add a basename to Warner's answer:

 mv $fname ./archive/`basename $fname`-`date +%Y%m%d_%H%M`
Mark Wagner
  • 18,019
  • 2
  • 32
  • 47