for file in /directory
do
mv $file $file+`date` # this creates a new file name with spaces, $file no
# longer points to a valid file
scp $file user@host:/ # $file is no longer here it is now $file Mon April 14...
ssh user@host "touch /$file.done" # what happens with duplicate file names
done
I don't know what your requirements are. I see what you are doing, so if you explain what you need not how to do it we can help.
Jim, here are my requirements:
transfer all files in directory to remote server after appending date with _YYYYMM.
When file is done create a file with the same name with extension .done on the remote server
I think the best way to find out is to write the code and try it on some test directory and file. Like /tmp/<user> testfile1, testfile2, etc. Then we can see if there is any issue.
# make an archive directory if it is not there
[[ ! -d /directory ]] && mkdir /directory/completed
cd /directory
for file in *
do
[[ -d $file ]] && continue # skip subdirectories
scp $file user@host:/
mv $file /directory/completed/$file_$(date "+%Y%m")
ssh user@host "touch /$file.done"
done
If you plan to run this script more than once on a given directory then:
You need to move the files out of /directory and off someplace where the
"for file in *" will not find them.
Jim that looks close to what I need, but I actually need to change the file name with the timestamp before i transfer it over to the remote server. I suppose I will need to create a "worker" directory to do the file renames
Also, with that code would the touched file on the remote server use the local scripts variable value of $file?
---------- Post updated at 04:48 PM ---------- Previous update was at 04:29 PM ----------
how does this look????
for file in /dir/*
do
mv $file /archive/$file_$(date "+%Y%m")
touch /archive/$file_$(date "+%Y%m").done
scp /archive/$file_$(date "+%Y%m") user@host:/
scp /archive/$file_$(date "+%Y%m").done user@host:/
done
---------- Post updated at 05:00 PM ---------- Previous update was at 04:48 PM ----------
So, the script above does everything as expected, but it doesn't put the filename at the front.
it only uses the timestamp for the name: 201004 201004.done