簡體   English   中英

使用bash腳本在FTP服務器上批量重命名文件

[英]Batch rename files on FTP server using bash script

我寫了一個簡短的bash腳本,將文件上傳到FTP服務器。 我必須使用FTP ...我無法控制遠程服務器。 上載位按預期方式工作,因此現在我想集成一些代碼,以便在將新文件上載之前將其重命名(移動)在“ Live”目錄中。 重命名命令不允許使用通配符或任何批處理,因此根據我所讀的內容,我需要對此進行循環。

這是腳本。

#!/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.$$

我正在登錄,將ls的結果發送到一個臨時文件,該文件具有以下內容:

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

然后運行awk,這給了我這個:

FILENAME1.tif
FILENAME2.tif

我現在使用的方法存在的問題是,我正在構建FTP命令並最后運行它們,因此awk首先運行。 因為ls尚未寫入temp($ DIRLIST)文件,所以只有沒有文件可以運行awk。

我可以讓整個過程在一個腳本中運行嗎?如果可以,怎么辦? 我可以運行兩個腳本,但不願意。

UPDATE

以下工作完美,但需要先登錄后注銷,然后再登錄:

#!/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

這就是我的意思,將mput放在awk上方,以便在連接文件時重命名命中之前就存在

#!/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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM