在sftp上递归列出所有文件

Rad*_*ber 7 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
Run Code Online (Sandbox Code Playgroud)

pas*_*qui 6

这个递归脚本完成以下工作:

#!/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 "."
Run Code Online (Sandbox Code Playgroud)

使用 sftp 服务器数据填充 URL。

  • 犹豫地+1——也许明确指出这将为每个目录打开一个“sftp”连接。 (2认同)

lut*_*act 6

我浏览了整个互联网并找到了一个很棒的工具sshfs。通过SSHFS挂载远程目录树。SSHFS 是一种使用 SFTP 协议访问远程文件的远程文件系统。

一旦安装了文件系统,您就可以使用所有常用命令,而不必关心文件实际上是远程的。

sshfs对我有很大帮助,也可能会给你带来帮助。

mkdir localdir
sshfs user@host:/dir localdir
cd localdir
find . -name '*'
Run Code Online (Sandbox Code Playgroud)