LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 10-27-2011, 09:57 AM   #1
mamu
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
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
 
Old 10-27-2011, 11:06 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
1 members found this post helpful.
Old 10-28-2011, 04:53 AM   #3
mamu
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
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!

Last edited by mamu; 10-28-2011 at 07:58 AM.
 
Old 10-28-2011, 04:10 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
You're welcome
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How do I Remote access a Linux machine from a other Linux/Windows machine unkn(0)wn Linux - Newbie 6 08-27-2011 07:35 AM
How to transfer files from a linux machine to a windows machine which are not in the same network Lakshmi Yallampalli Linux - Networking 6 07-16-2010 04:50 AM
How to run a script to a remote linux machine from a windows machine squirrel001 Linux - Newbie 5 05-04-2009 06:06 PM
how to use ftp to connect to remote machine vinaytp Linux - Newbie 11 04-29-2009 08:04 PM
How to Transfer files between Linux machine and Window Machine rktech83 Linux - Networking 7 03-14-2006 03:10 AM


All times are GMT -5. The time now is 11:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration