D
D
Dmitry Kuzmin2012-06-04 09:45:44
linux
Dmitry Kuzmin, 2012-06-04 09:45:44

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

5 answer(s)
Z
zuborg, 2012-06-04
@zuborg

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.

P
pesich, 2012-06-04
@pesich

Ftp protocol understands ls -R, but with wc -l, it's a bummer.
Look in help what commands are allowed to you.

@
@sledopit, 2012-06-04
_

{ 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/'

P
pesich, 2012-06-04
@pesich

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

T
tgz, 2012-06-05
@tgz

Most ftp does not allow recursive ls.
And sometimes there are files like ls-lR.gz

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question