List recursively all files on sftp

前端 未结 2 677
故里飘歌
故里飘歌 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.

    0 讨论(0)
  • 2020-12-19 05:45

    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.

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

    sshfs helps me a lot, may give you help, too.

    mkdir localdir
    sshfs user@host:/dir localdir
    cd localdir
    find . -name '*'
    
    0 讨论(0)
提交回复
热议问题