LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   FTP: Get count of files on a remote machine directory to the source Linux machine (http://www.linuxquestions.org/questions/linux-newbie-8/ftp-get-count-of-files-on-a-remote-machine-directory-to-the-source-linux-machine-910477/)

mamu 10-27-2011 09:57 AM

FTP: Get count of files on a remote machine directory to the source Linux machine
 
Hi,

I have a Linux machine (Source) and a remote machine's FTP share (Target) available to me. No SSH access available and won't be either, so its only FTP.
Now what I want to do from my Linux machine is to check for the number of files present on the FTP share and report back the status to me on the Linux machine. In short a shell script which I would put in 'cron' and which will:
1. Connect to the FTP share
2. Count the number of files present
3. Return back the file count to the script or echo it out

Is this possible and how? Your assistance here would be highly appreciated.

Thanks,

Mamu

druuna 10-27-2011 11:06 AM

Hi,

Yes, that is possible. Here's a rough example:
Code:

#!/bin/bash

HOST="host.goes.here"
USER="username"
PASSWD="password"

CONTENT="$(ftp -in $HOST <<-HERE
user $USER $PASSWD
ls
bye
HERE)"

echo -n "Number of remote files on $HOST: "
echo "$CONTENT" | egrep -v "user|ls|^d|bye" | wc -l

A few remarks on the above:
- The script shows the username and password, if at all possible use a client (like ncftp) that can do auto login. This way security sensitive info is kept out of the script.
- The script does not take sub-directories and its files into account.

Example:
Code:

# content of directory
ls -l
total 3098156
drwxr-x--- 2 wdh wdh    4096 Oct 27 17:54 XXX
-rw-r----- 1 wdh wdh    1591 Oct 27 14:25 test1
-rw-r----- 1 wdh wdh    9827 Oct 27 14:25 test2

# run script
$ ./ftp.count.files.sh
Number of remote files on hostname: 2

Hope this helps.

mamu 10-28-2011 04:53 AM

Ohh yes it certainly helps...I did similar kind of stuff for taking output out to shell for SQL...had never tried it with FTP though. Thanks a lot!

druuna 10-28-2011 04:10 PM

You're welcome :)


All times are GMT -5. The time now is 09:20 PM.