Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have already seen How to perform GREP on FTP? - that question implies that a grep in the remote file contents is needed; and as such, the answer is that the ftp protocol does not support that (download of all remote files locally is needed, before a search in their contents can be initiated).

I'm looking for something a bit more "simple" - I'd just like to grep/search for remote filenames; I'd imagine this requires that the remote directory tree is somehow downloaded. How could I do this through the command line?

share|improve this question

2 Answers 2

Here is one way - using GNU ftpsync.

Note that the download links on http://www.gnu.org/software/ftpsync/ seem to be broken as of now; the page you'd need is http://savannah.gnu.org/projects/ftpsync ... It seems that I got this via git, probably through the address:

git clone git://git.sv.gnu.org/ftpsync.git

Note that ftpsync is a Perl script, in the src/ directory of this repo. (Note also there is a different tool, ftpsync.py in Python, found at ftpsync2d - Google Project Hosting)

Anyways, the trick is simply to try to sync the remote FTP tree to an empty local directory, while having set ftpsync to info mode (-i) (so no actual download actions are performed), and in verbose mode. Note though, verbose mode (-v) seems to be buggy (I get even less output to stdout than if I don't use -v) ; so below I've used -d for debug mode, which seems to work as advertised.

Ultimately, I do this:

$ mkdir /tmp/test
$ cd /tmp/test
$ /path/to/ftpsync-git/src/ftpsync -d -i ftp://user:password@ftp.mysite.com/www .   2>&1 | tee _ftpsync_.log

Building remote tree ftp://user@ftp.mysite.com/www
Detecting if passive needed... Passive
Logging in as user.
Sync file /tmp/Q44CbrEGUU => Q44CbrEGUU
Localtime before 1393782144, Remote 1393782120, after 1393782144
Clock sync offset: 0h00m00s
Dir:  in www
Mod:1327878000 Size:737 .htaccess
Mod:1237503600 Size:241 .hiddenfile
Dir: stuff in www
...
Mod:1353452400 Size:4812800     somefile

Building local tree of .
Dir: 
Mod: Size:      
Died at /path/to/ftpsync-git/src/ftpsync line 1011.

After this process is done, you should be having a list of all files and directories under www/ on remote server, locally in _ftpsync_.log - thus later you can grep through this file, to search for remote filenames and subdirectory names.

Well - would still like to hear if there are alternative tools for doing this...

Hope this helps someone,
Cheers!

share|improve this answer

On UNIX sytems usually Perl is already installed and Net::FTP is a Perl CORE module, e.g. is also installed. Then you could do without additional software (the grep is in the last line):

perl -MNet::FTP -e '
    $ftp = Net::FTP->new(q[ftp.example.com]) or die $!; 
    $ftp->login(q[ftp],q[user@host]) or die $ftp->message; 
    $ftp->cwd(q[pub/whatever]) or die $ftp->message;
    print "$_\n" for grep { /pattern/ } $ftp->ls
'
share|improve this answer
    
Neat, wasn't aware of that - many thanks, @SteffenUllrich; cheers! –  sdaau Mar 2 at 19:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.