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.
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.
echo ls -l | sftp server | grep -v '^sftp' | wc -l
If you want to count the files in a directory the directory path should be put after the ls -l command like
echo ls -l /my/directory/ | sftp server | grep -v '^sftp' | wc -l
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.
I would use sftp batch file. Create a file called batchfile and enter "ls -l" in it. Then run
sftp -b batchfile user@sftpHost | wc -l