bash - 递归列出sftp上的所有文件

标签 bash sftp

我想编写 bash 脚本以递归地列出 sftp 上的所有文件(具有完整路径),然后在本地与路径交互(因此唯一需要 sftp 的是获取路径)。不幸的是,“ls -R”在那里不起作用。

任何关于如何使用一些基本的 POC 做到这一点的想法将不胜感激

Available commands:

bye                                Quit sftp

cd path                            Change remote directory to 'path'

chgrp grp path                     Change group of file 'path' to 'grp'

chmod mode path                    Change permissions of file 'path' to 'mode'

chown own path                     Change owner of file 'path' to 'own'
df [-hi] [path]                    Display statistics for current directory or
                               filesystem containing 'path'
exit                               Quit sftp
get [-Ppr] remote [local]          Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-Ppr] local [remote]          Upload file
pwd                                Display remote working directory
quit                               Quit sftp
rename oldpath newpath             Rename remote file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

最佳答案

这个递归脚本完成了这项工作:

#!/bin/bash 
#

URL=user@XXX.XXX.XXX.XXX
TMPFILE=/tmp/ls.sftp

echo 'ls -1l' > $TMPFILE

function handle_dir {
  echo "====== $1 ========="
  local dir=$1
  sftp -b $TMPFILE "$URL:$dir" | tail -n +2 | while read info; do
    echo "$info"
    if egrep -q '^d' <<< $info; then
       info=$(echo $info)
       subdir=$(cut -d ' ' -f9- <<< $info)
       handle_dir "$dir/$subdir"
    fi
  done
}

handle_dir "."

用 sftp 服务器数据填充 URL。

关于bash - 递归列出sftp上的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30504124/

相关文章:

encoding - 使用 PhpStorm/WinSCP 和 SFTP 的文件名编码错误

java - 上传和下载文件时使用内存泄漏(可能是因为使用 ChannelSftp.get( "file_name") )?

bash - 使用 shell 脚本将二进制文件读取为字节数组或 16 或 32 位

bash - 当我中断 ssh 本身时,如何让 ssh 杀死远程进程?

linux - 计算多个文件中的重复项

python - 尝试使用 Python SFTP 文件时定义传输模式

java - 如何使用 jsch 使用 base64 编码将文件传输到 sftp

linux - 如何通过 SFTP 将数据传输到远程文件而不将数据存储在 Linux Bash 的本地文件中?

bash -\n 在 Go 中写入文件时转义

bash - docker 撰写命令 : Invalid interpolation format for "command" option in service