SFTP: get back the number of files in a remote directory?
4 answers
Use a batch file to run commands remotely and return data to work in bash:
Create a batch file named mybatch.txt
with these sftp commands:
cd your_directory/your_sub_directory
ls -l
Save it and give it 777 permissions.
chmod 777 mybatch.txt
Then run it like this:
sftp your_username@your_server.com < mybatch.txt
He will prompt you for a password, enter it.
Then you will get the output dumped to the bash terminal. Therefore, you can pass this value wc -l
like this:
sftp your_user@your_server.com < mybatch.txt | wc -l
Connecting to your_server.com...
your_user@your_server.com password:
8842
8842 is the number of rows returned ls -l
in this directory.
Instead of linking it to wc, you can send it to a file for parsing to determine how many files / folders.
+1
source to share