SFTP: return number of files in remote directory?

前端 未结 4 1453
谎友^
谎友^ 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 12:13

    Use a batch file to run commands remotely and get the data back to work with in bash:

    Make your batch file called mybatch.txt with these sftp commands:

    cd your_directory/your_sub_directory
    ls -l
    

    Save it out and give it 777 permissions.

    chmod 777 mybatch.txt
    

    Then run it like this:

    sftp your_username@your_server.com < mybatch.txt
    

    It will prompt you for the password, enter it.

    Then you get the output dumped to bash terminal. So you can pipe that to wc -l like this:

    sftp your_user@your_server.com < mybatch.txt | wc -l
    Connecting to your_server.com...
    your_user@your_server.com's password:
    8842
    

    The 8842 is the number of lines returned by ls -l in that directory.

    Instead of piping it to wc, you could dump it to a file for parsing to determine how many files/folders.

提交回复
热议问题