简体   繁体   中英

Batch rename files on FTP server using bash script

I've written a short bash script to upload files to an FTP server. I have to use FTP... I've no control over the remote server. The upload bit works as expected so now I'd like to integrate some code to rename (move) existing files in the "Live" directory before uploading new files to it. The rename command doesn't allow for wildcards or any batch processing so from what I've read I need to loop this.

Here's the script.

#!/bin/bash

cd $UPLOADS
echo "open $SERVER
user $NAME $PASSWORD
binary
cd Live
ls > /tmp/$DIRLIST" > /tmp/ftp.$$
awk -F" " 'NR>2 {print $9} ' /tmp/$DIRLIST > /tmp/$MOD
cat /tmp/$MOD | while read tif  
do
echo "rename $tif ../Done/$tif" >> /tmp/ftp.$$
done
echo "mput *.tif
quit" >> /tmp/ftp.$$
ftp -pin < /tmp/ftp.$$
rm /tmp/ftp.$$

I'm logging in, sending the results of ls to a temp file which gives me the following:

drwxrwxrwx   1 user     group           0 Sep  7 08:27 .
drwxrwxrwx   1 user     group           0 Sep  7 08:27 ..
-rw-rw-rw-   1 user     group     6506940 Sep  7 11:07 FILENAME1.tif
-rw-rw-rw-   1 user     group     6506940 Sep  6 10:21 FILENAME2.tif

Then run awk, which gives me this:

FILENAME1.tif
FILENAME2.tif

The problem with the way I've got it now is I'm building the FTP commands and running them last, so awk runs first. Only there's no file to run awk on because ls hasn't written the temp ($DIRLIST) file yet.

Can I get this entire process to run in one script and if so, how? I could run two scripts but prefer not to.

UPDATE

The following works perfect, but requires logging in, out, then back in:

#!/bin/bash

# log in once to write current list of files to /tmp/$DIRLIST
ftp -pin $SERVER <<END_SCRIPT
user $NAME $PASSWORD
cd Live
ls > /tmp/$DIRLIST
quit
END_SCRIPT

# skip first two lines(. and ..) get a clean list of files
awk -F" " 'NR>2 {print $9} ' /tmp/$DIRLIST > /tmp/$MOD

# log back in rename existing files and upload new files
cd $UPLOADS
echo "open $SERVER
user $NAME $PASSWORD
binary
cd Live" > /tmp/ftp.$$
cat /tmp/$MOD | while read tif
do
echo "rename $tif ../Done/$tif" >> /tmp/ftp.$$
done
echo "mput *.tif
quit" >> /tmp/ftp.$$
ftp -pin < /tmp/ftp.$$

#cleanup
rm /tmp/ftp.$$
rm /tmp/$DIRLIST
rm /tmp/$MOD

Here is what I meant, put the mput above the awk so when you connect the files are there before the rename hits

#!/bin/bash

# log in once to write current list of files to /tmp/$DIRLIST
ftp -pin $SERVER <<END_SCRIPT
user $NAME $PASSWORD
cd Live
ls > /tmp/$DIRLIST 
quit
END_SCRIPT

# skip first two lines(. and ..) get a clean list of files
awk -F" " 'NR>2 {print $9} ' /tmp/$DIRLIST > /tmp/$MOD

# log back in rename existing files and upload new files
cd $UPLOADS
echo "open $SERVER
user $NAME $PASSWORD
binary
cd Live" > /tmp/ftp.$$
echo "mput *.tif
quit" >> /tmp/ftp.$$
cat /tmp/$MOD | while read tif
do
echo "rename $tif ../Done/$tif" >> /tmp/ftp.$$
done
ftp -pin < /tmp/ftp.$$

#cleanup
rm /tmp/ftp.$$
rm /tmp/$DIRLIST
rm /tmp/$MOD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM