Answer the question
In order to leave comments, you need to log in
How to count the number of files on a remote ftp in Bash?
I can't write a script in Bash or Shell that would go to ftp and count the number of files on a remote ftp in a certain directory and all subdirectories of this folder. With authorization it is clear, I can not figure out how to make a count in all subdirectories.
Answer the question
In order to leave comments, you need to log in
Don't be foolish, but bash (and other sh) can't access ftp. You need to use an external program that can do this, and this is where the dependencies on the version of your OS begin, what is installed and what can be installed, etc. etc. Refine your question.
btw, if it is possible to login via ssh instead of ftp, then the issue is easily solved via
find /path/to/dir -type f | wc -l
It's much more efficient to do the counting on the side where these files are, instead of sending a list of files. In addition, the ftp protocol dir command does not do a recursive listing, so such commands will have to be run as many as there are subdirectories, these are additional brakes.
Ftp protocol understands ls -R, but with wc -l, it's a bummer.
Look in help what commands are allowed to you.
{ ftp -inv SERVER << EOF
user USER PASSWORD
cd DIRECTORY
ls -R
bye
EOF
} | sed '/^[d-]/!d;s/^\(.\).*/\1/' | sort | uniq -c | sed 's/-/files/;s/d/directories/'
It seems to work, file for yourself if something is wrong.
#!/bin/bash
HOST="IP"
USER="xxxx"
#PASSWD="1234"
CONTENT="$(ftp -nv <<EOF
open $HOST
user $USER
ls -R
bye
EOF)"
echo -n " Number of remote files on $HOST: „
echo “$CONTENT”|egrep -v “user|ls|^d|bye” | wc -l
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question