gpt4 book ai didi

linux - 使用 bash 脚本批量重命名 FTP 服务器上的文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:31:58 24 4
gpt4 key购买 nike

我编写了一个简短的 bash 脚本来将文件上传到 FTP 服务器。我必须使用 FTP...我无法控制远程服务器。上传位按预期工作,所以现在我想集成一些代码来重命名(移动)“实时”目录中的现有文件,然后再将新文件上传到它。重命名命令不允许使用通配符或任何批处理,因此根据我的阅读,我需要循环执行此命令。

这是脚本。

#!/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 首先运行。只是没有文件可以运行 awk,因为 ls 还没有写入临时文件 ($DIRLIST)。

我可以让整个过程在一个脚本中运行吗?如果可以,怎么做?我可以运行两个脚本,但不想这样做。

更新

以下是完美的,但需要登录、退出,然后再登录:

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

关于linux - 使用 bash 脚本批量重命名 FTP 服务器上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25713967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com