2

I know I can run ls * on the ftp server and get everything, but there's too much for my terminal (hundreds of pages so far). using ls * > myfile isn't an option because I don't have permission to write. So I need a way to do it from the terminal, something like: ftp ftp://site.com

I've read the man pages but don't want to take a risk here. I don't have personal permission to modify this server but the user I've been given may have write permissions.

I need to export this to any local format so I can review it over time, as there is a lot of output.

3

Apparently you can pipe a command directly to ftp:

echo "ls *" | ftp hostname > ls.txt

Make sure to quote ls * somehow or to disable globbing otherwise * will expand in the current shell.

  • Thank you so much! instead of "ls *" I used "ls -R" and indeed I have a full index of the server now. – TenLeftFingers Jun 23 '15 at 20:47
2

A good option might be to pipe the output into less:

ls * | less

Then you should be able to happily scroll through the pages of output without needing any file writing permissions.

  • While that would work, the output is so large that I need a way to export it so I can reference it regularly. I'll update my question so that that's clearer. Thank you! – TenLeftFingers Jun 22 '15 at 21:18
  • I'm guessing that you can't mount an external drive either. Is there a way to use an FTP client like Filezilla and capture the directory listing from that? – Arronical Jun 23 '15 at 8:47
  • interesting Arronical! I used Nautilus to connect to the FTP server and from there, right-click -> Open in Terminal (available in the Ubuntu Store). from there I possibly would be able to use the command in your answer if I redirected it to /home/tenleftfingers, but I'm not sure so don't want to risk it :) I didn't find anything in FileZilla to do this. – TenLeftFingers Jun 24 '15 at 17:56
2

What about:

 ncftpls -R ftp://site.com

Nice command, designed for exactly this purpose

1

i've just made a python script for this purpose: https://gist.github.com/eadmaster/55d1508bd17780ee1e93c15609bb69af

#!/usr/bin/env python

# code derived from https://gist.githubusercontent.com/flibbertigibbet/8165881/raw/7f580b6735ad1f03c6e6060cd1c8571048afad6a/recurse_ftp.py

from ftplib import FTP
from time import sleep
import os
import sys

my_dirs = []  # global
curdir = ''   # global

PROGRAM_NAME = os.path.basename(sys.argv[0])

if len(sys.argv) <= 1 or sys.argv[1].startswith("-"):
  print("usage: " + PROGRAM_NAME + " SERVER_ADDR [SERVER_ROOT_DIR]")
  sys.exit(1)
# else
SERVER_ADDR = sys.argv[1]

SERVER_ROOT_PATH = "/"
if len(sys.argv) >= 3:
  SERVER_ROOT_PATH = sys.argv[2]

def get_dirs(ln):
  global my_dirs
  cols = ln.split(' ')
  objname = cols[len(cols)-1] # file or directory name
  if ln.startswith('d'):
    my_dirs.append(objname)
  else:
    print(curdir + "/" + objname)

def check_dir(adir):
  global my_dirs
  global curdir
  my_dirs = []
  gotdirs = [] # local
  try:
    curdir = ftp.pwd()
    sys.stderr.write(PROGRAM_NAME + ": going to change to directory " + adir + " from " + curdir + "\n")
    ftp.cwd(adir)
    curdir = ftp.pwd()
    ftp.retrlines('LIST', get_dirs)
  except:
    return
  print(curdir + "/")
  gotdirs = my_dirs
  #print("found in " + adir + " directories:")
  sleep(0.5)
  for subdir in gotdirs:
    if(subdir.startswith(".")):
      continue
    my_dirs = []
    check_dir(subdir) # recurse  
  # end for
  ftp.cwd('..') # back up a directory when done here
# end of check_dir


# main
try:
  ftp = FTP(SERVER_ADDR)
  ftp.login()
  check_dir(SERVER_ROOT_PATH) # directory to start in

except:
  import logging
  logging.exception("")
  ftp.quit()

ftp.quit()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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