SCP and then touch .done file

All, I am looking to make a script and wanted to see if anyone could help out.

The script will go through the directory, put a timestamp, transfer it and then create a touch $file.done script

HEre is my initial idea, but I don't think it will work properly. Anyone able to help me refine it to do what I need it to do?

for file in /directory
          do
           mv $file $file+`date`
           scp $file user@host:/
           ssh user@host 'touch /$file.done
           done
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.

Thanks.

I think this is what you want:

# 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

Any ideas why?

Most likely, it is looking for a variable named $file_

Try using ${file}_ instead.

I put that in and now get this error

mv: cannot move `./dir/test' to `/archive/.scripts/test_201004': No such file or directory

It is adding the original directory name to the destination location name for some reason on the move and touch

May be you can try something like this:

for file in /dir/*
 do
           myfile=`basename $file`
           mv ${myfile} /archive/${myfile}_$(date "+%Y%m")
           
           ...
done

That did the trick!

Glad to be able to help a neighbor!

Ok, I have one last piece that needs to be done.

They now want the month to show as the last month. So, how could I run the date -1 for month.

Got my own solution: date -d 'month ago' "%Y%m"

---------- Post updated at 03:37 PM ---------- Previous update was at 11:01 AM ----------

Jim, what exactly does the [[ -d $file ]] & continue do to skip subdirectories?