3

What would be the best/easiest way for a non-techie like me to get my headless Ubuntu media server to generate a file containing a list of files X-levels deep from a specific folder and back that list up without actually backing up the files themselves? Ideally (but by no means required) it would be a nicely formatted HTML page... but only if there's some existing tool that does that sort of thing. I'm not expecting the answerer to write extensive code for me.

I have Crashplan installed but I don't want to use bandwidth moving terabytes of non-critical files to the Cloud. But in the rare event something happens, I want to at least see a list of what was there so I can see what was lost and I can pick and choose what to replace.

I hope the answer is complete, as I'm by no means a command line guru... and it does need to be command line because it is headless.

6

You can use tree. It might not be preinstalled, so get it using

sudo apt-get install tree

Then you can get a list of your files and their directory structure by simply running

$ tree test/
test/
├── dir1
│   ├── file1
│   ├── file2
│   └── file3
├── dir2
│   ├── file1
│   ├── file2
│   └── file3
└── dir3
    ├── file1
    ├── file2
    └── file3

You redirect the output to a file like this:

tree test/ > directory-list.txt

You can limit recursion depth using the -L option, e.g. tree -L 3 test/.

If you want a fancy HTML page with FTP links to each file, tree has that built in!

tree -H /ftp-root test/ > directory-list.html

You need to specify a base link though which will control where all the file hyperlinks point to, as described in the manpage (man tree):

   -H baseHREF
          Turn  on  HTML output, including HTTP references. Useful for ftp
          sites.  baseHREF gives the base ftp  location  when  using  HTML
          output.  That  is,  the local directory may be `/local/ftp/pub',
          but  it  must   be   referenced   as   `ftp://hostname.organiza‐
          tion.domain/pub'  (baseHREF  should be `ftp://hostname.organiza‐
          tion.domain'). Hint: don't use ANSI lines with this option,  and
          don't give more than one directory in the directory list. If you
          wish to use colors via CCS style-sheet, use  the  -C  option  in
          addition to this option to force color output.

The resulting file opened in a webbrowser looks like this:

enter image description here

  • Exactly what I needed! – bcsteeve Sep 25 '16 at 23:47
3

find is your friend here.

Assuming you want to backup all filenames, upto 4 levels deep, starting from /foo/bar directory, into a file backup_filenames.txt:

find /foo/bar -maxdepth 4 -type f >backup_filename.txt

To consider only filenames of a certain extension e.g. .txt, use -name:

find /foo/bar -maxdepth 4 -type f -name '*.txt' >backup_filename.txt

Case insensitively (-iname):

find /foo/bar -maxdepth 4 -type f -iname '*.txt' >backup_filename.txt

Modify the parameters to meet your need. Also check man find.

  • Thanks! And then I'd set up a cron job to do that periodically, right? – bcsteeve Sep 25 '16 at 23:14
  • @bcsteeve Yes, you absolutely can. Note that, > truncates the file so make any necessary modifications on the backup filename logic, if needed. – heemayl Sep 25 '16 at 23:16
  • If you want HTML output, you can use something like awk to format the output from find: find /foo/bar -maxdepth 4 -type f | awk 'BEGIN {print "<html><body><pre>"} {printf "%s<br>", $0} END {print "</pre></body></html>"}' > backup_filename.html – Johnny Sep 26 '16 at 3:15

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.