2015-05-28 34 views
7

Mi piacerebbe scrivere script bash per elencare in modo ricorsivo tutti i file (con percorsi completi) su sftp e interagire con i percorsi localmente successivamente (quindi solo cosa per cui è necessario sftp è ottenere i percorsi). Sfortunatamente "ls -R" non funziona lì.Elenca in modo ricorsivo tutti i file su sftp

Qualsiasi idea di come fare con un po 'POC base sarebbe molto apprezzato

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 
+1

Hai provato il comando 'find'? – 123

+0

Scusa, ho dimenticato di dire che non è disponibile ... –

+0

Perché hai così pochi comandi disponibili? Questo elenco manca di buildh bash ?? – 123

risposta

2

Questo script ricorsivo fa il lavoro:

#!/bin/bash 
# 

[email protected] 
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 "." 

URL riempire con i dati del server SFTP.

+0

Hesitantly +1 - forse indica esplicitamente che questo aprirà una connessione 'sftp' per ogni directory. – tripleee

0

Scansione l'intero Internet e trova un ottimo strumento sshfs. Montare la struttura della directory remota tramite SSHFS. SSHFS è un filesystem remoto che utilizza il protocollo SFTP per accedere ai file remoti.

Una volta montato il filesystem, è possibile utilizzare tutti i consueti comandi senza doversi preoccupare che i file siano effettivamente remoti.

sshfs mi aiuta molto, può darti anche aiuto.

mkdir localdir 
sshfs [email protected]:/dir localdir 
cd localdir 
find . -name '*'