45

For a task of mine I need to list all the files in a tree (a directory, all its subdirs, all subdirs of those, etc.).

I'd prefer to see them in Nautilus or Krusader, but a command-line solution is interesting as well (in this case I will need files full names, sizes and modification times to be listed).

1
  • 1
    This question has an awful lot of views. Perhaps you could accept the answer you found most helpful, to save others some time @Ivan? Jul 12, 2018 at 20:20

10 Answers 10

42

tree will be very convenient for you.

sudo apt-get install tree

using tree filepathto list the files.

29
ls -alR

That's probably the simplest method. I'm just hacking out a find script to give you a touch more control.

1
  • 9
    Or just ls -R if you don't want all the details.
    – Matthew
    Nov 30, 2010 at 5:09
12
find /path/ -printf "%TY-%Tm-%Td\t%s\t%p\n"

You can play with the printf formatting as much as you like. This gives you a great opportunity to get things formatted the way you need them, which is invaluable if you're using the output in another application.

More: http://linux.about.com/od/commands/l/blcmdl1_find.htm

For better readability, you can pipe it all through the column command and it will automagically resize things so they line up.

find /path/ -printf "%TY-%Tm-%Td\t%s\t%p\n" | column -t
0
6

ls is the standard command to list files in Ubuntu and other Linux and Unix operating systems. ls is particularly useful to learn because you will find it installed on every Unix system you ever meet. By default running this displays only the files in the current directory.

However the -R 'flag' is the recursive option (note the capital R, not r) which will show you all the sub-directories as well.

You asked for "details" too - for this you want the -l flag (that's a lowercase L, not the number one). Be aware this gives you file permissions information as well as file size, time/date info and file name.

If you want to also show hidden files/folders (the equivalent of Ctrl+H in Nautilus) then add the -a 'all' flag.

You can merge flags together, to give you something like:

ls -lR

If you run this on any decent sized folder you will find this produces a huge long output that scrolls down your screen very fast. To get around this, you can 'pipe' the output of ls through a program called less (the name is a parody of the similar more which was around first but has more features).

ls -lR | less

This will allow you to use the up/down arrow keys, alongside PageUp/Down to go through the output at a more comfortable speed.

4

As Oli answered, find will allow you to search an entire directory tree:

find /path/ -printf "%TY-%Tm-%Td\t%s\t%p\n"

# Where %TY will display the mod. time year as 4 digits
#       %Tm will display the mod. time month as 2 digits
#       %Td will display the mod. time day as 2 digits
#       %s displays the file size in bytes
#       %p displays the full path name of the file

You may also want to use the -type f option to limit the results to just files. If you want to match a file pattern, you want the -name or -iname options (case sensitive, and case insensitive matching, respectively). Take a read through find's man page - there are a substantial amount of options that you can use to narrow/refine your search.

And just as an aside, if you are expecting to have multiple screenfuls of data get thrown back at you, remember to pipe your results through less.

@Oli : +1 I just learned something new as well - column. Hadn't used that before.

0

Using Krusader:

  1. Use Search (Ctl-S).
  2. Check off "search in subdirectories".
  3. Click "Search".
  4. When search is finished, click "Feed to listbox".

This will allow you to process the whole lot by dragging to the other panel, etc.

0

How about a Nautilus script?

#!/bin/bash
#
# AUTHOR:       (c) 2013 Glutanimate (https://askubuntu.com/users/81372)
# NAME:         DirTree 0.1
# DEPENDENCIES: zenity tree  (install with sudo apt-get install zenity tree)
# LICENSE:      GNU GPL v3 (http://www.gnu.org/licenses/gpl.html)
#
# DESCRIPTION:  Creates a directory tree at the current location. If you want you
#               can filter the output by a pattern (e.g. *.avi to only include avi
#               files).

TITLE=DirTree

# Get working directory
WORKINGDIR="`python -c 'import gio,sys; print(gio.File(sys.argv[1]).get_path())' $NAUTILUS_SCRIPT_CURRENT_URI`"

# Time and date
TIME=$(date +"%Y-%m-%d_%H%M%S")

# Filter pattern
zenity --question --title "$TITLE" --text "Do you want to filter by a specific file pattern?"

if [ "$?" = "1" ]
  then
      customtree="tree"
  else
      PATTERN=$(zenity --entry --title "$TITLE" --text="Please enter a file pattern (e.g. *.avi)")

         if [ -z "$PATTERN" ]
           then
               exit
           else
               customtree="tree -P $PATTERN"               
         fi

fi

# Directory tree
$customtree "$WORKINGDIR" > "$WORKINGDIR/directorytree_$TIME.txt"

Installation instructions: How can I install a Nautilus script?

0

I have created a program specially for this - Directory Snapshot.
It recursively iterates through a directory and stores the names and sizes of all the files and folders it encounters in neatly-structured HTML files, which have an organization similar to that of the input directory.
In other words, this can be thought of as a hyperlinked version of the dir /s or tree /f commands.

2
  • You wrote dir and tree with DOS-style options, so I have to ask, will this work on Ubuntu?
    – wjandrea
    Dec 28, 2017 at 22:32
  • Yes, I have actually scheduled it to run on my Ubuntu installation every weekend. Although, it is built using C++ and Boost, so it'll be a bit tough to compile and create a binary. That is why I am in the process of porting it to Python. Anyways, I can give you the precompiled binary if you want. Dec 29, 2017 at 8:27
0

Nautilus

  1. Enable expanding subfolders in list view
    • For 14.04 and 16.04, see here
    • For 17.10 and 18.04, see here
  2. Click on a folder and press asterisk (* on the numpad, or Shift+8) to expand one level of subdirectories. Each time you press asterisk, it will expand the next level.

This also works in Nemo.

-1

you can put these in your .bashrc file

function _get_tree(){ ls -alR | while read LINE; do   echo $LINE | grep ":$" > /dev/null;   if [ $? -eq 0 ];   then     VAR=$(echo $LINE | grep ":$"| sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/' | tee /dev/tty);   fi;   echo $LINE | grep "^-" > /dev/null;   if [ $? -eq 0 ];   then     size=${#VAR};     for i in $(eval echo "{1..$size}"); do echo -n ' '; done;     echo -n '..';     echo $LINE | cut -d ' ' -f9;   fi; done; };

alias get_tree='_get_tree'

Now, you can use get_tree command inside any directory and it will display the entire hierarchy.

Syntax:

$ get_tree

Sample Output:

   .

    ..test.sh

   |-2

      ..123.log

   |---3

   |-----4

   |-5

   |---6

Hope, This Helps !!

2
  • Not going to help unless you explain what you are doing..
    – Ron
    Jul 22, 2015 at 8:04
  • -1 Poor code formatting, not enough explanation. And why do you make an alias?
    – wjandrea
    Dec 28, 2017 at 22:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .