SFTP: return number of files in remote directory?

前端 未结 4 1486
谎友^
谎友^ 2021-01-13 11:34

I sent a batch of files to a remote server via SFTP. If it were a local directory I could do something like this ls -l | wc -l to get the total number of files.

4条回答
  •  醉酒成梦
    2021-01-13 11:57

    The easiest way I have found is to use the lftp client which supports a shell-like syntax to transfer the output of remote ftp commands to local processes.

    For example using the pipe character:

    lftp -c 'connect sftp://user_name:password@host_name/directory; ls -l | wc -l'
    

    This will make lftp spawn a local wc -l and give it the output of the remote ls -l ftp command on its stdin.

    Shell redirection syntax is also supported and will write directly to local files:

    lftp -c 'connect sftp://user_name:password@host_name/directory; ls -l >list.txt'
    

    Thus a file named list.txt containing the remote file listing will be created in the current folder on the local machine. Use >> to append instead.

    Works perfectly for me.

提交回复
热议问题