List recursively all files on sftp

前端 未结 2 684
故里飘歌
故里飘歌 2020-12-19 05:25

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 ge

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 05:42

    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.

提交回复
热议问题