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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30504124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:03:53  来源:igfitidea点击:

List recursively all files on sftp

bashsftp

提问by Radek Feber

I'd like to write bash script to recursively list all files (with fullpaths) on sftp and interact with paths locally afterwards (so only thing for what sftp is needed is getting the paths). Unfortunately the "ls -R" doesn't work there.

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

Any idea how to do that with some basic POC would be really appreciated

任何想法如何使用一些基本的 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

回答by pasaba por aqui

This recursive script does the job:

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

#!/bin/bash 
#

[email protected]
TMPFILE=/tmp/ls.sftp

echo 'ls -1l' > $TMPFILE

function handle_dir {
  echo "======  ========="
  local dir=
  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 "."

fill URL with the sftp server data.

用 sftp 服务器数据填充 URL。

回答by lutaoact

I scan the whole Internet and find a great tool sshfs. Mount the remote directory tree through SSHFS. SSHFS is a remote filesystem that uses the SFTP protocol to access remote files.

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

Once you've mounted the filesystem, you can use all the usual commands without having to care that the files are actually remote.

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

sshfshelps me a lot, may give you help, too.

sshfs帮了我很多,也可以帮到你。

mkdir localdir
sshfs user@host:/dir localdir
cd localdir
find . -name '*'