递归列出SFTP上的所有文件 [英] List recursively all files on sftp

查看:498
本文介绍了递归列出SFTP上的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对SFTP写bash脚本递归列出所有文件(具有fullpaths),并与当地的路径之后交互(因此只对所需要的东西SFTP越来越路径)。不幸的是,LS -R这里是行不通的。

不知道如何做一些基本的POC将pciated真的AP $ P $

 可用的命令:再见退出SFTPCD路径更改远程目录到路径执行chgrp文件'路径'的GRP路径更改组GRP文件'路径'的CHMOD模式路径更改权限为模式CHOWN自己的文件'路径'来的路径所有者更改为自己
DF [-hi] [路径]显示当前目录的统计数据或
                               含有文件系统路径
退出退出SFTP
得到[-Ppr]远程[本地]下载文件
帮助显示此帮助文本
液晶路径更改本地目录路径
LLS [LS-选项[路径]]显示本地目录列表
lmkdir路径创建本地目录
LN [-s] oldpath的新路径Link远程文件(符号链接-s)
lpwd打印本地工作目录
LS [-1afhlnrSt] [路径]显示远程目录列表
lumask的umask设置本地的umask为'的umask
MKDIR路径创建远程目录
进度表进度切换显示
把[-Ppr]本地[远程]上传文件
pwd显示远程工作目录
退出退出SFTP
重命名oldpath的新路径重命名远程文件
RM路径删除远程文件
命令rmdir路径删除远程目录
符号链接oldpath的新路径符号连接远程文件
版本显示SFTP版本
!命令在本地执行的shell'命令'
!逃逸到本地shell
?同义词求助


解决方案

本递归脚本做这项工作:

 #!/斌/庆典
#URL=user@XXX.XXX.XXX.XXX
TMPFILE =的/ tmp / ls.sftp回声LS -1L'> $ TMPFILE功能handle_dir {
  回声====== $ 1 =========
  本地目录= $ 1
  SFTP -b $ TMPFILE$网址:$目录|尾-n +2 |而读取信息;做
    回声$信息
    如果egrep的-q'^ D'<<< $信息;然后
       信息= $(回声$资讯)
       子目录= $(切-d''-f9-<<< $信息)
       handle_dir$ DIR / $子目录
    科幻
  DONE
}handle_dir。

填写网址与SFTP服务器的数据。

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.

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

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

解决方案

This recursive script does the job:

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

fill URL with the sftp server data.

这篇关于递归列出SFTP上的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆