tree has a
-d option to "
List directories only.". However, I cannot seem to find an option to "List files only." I have looked through the man page, but I cannot seem to find an option for listing only files.
As others have said in the comments, listing only non-directories doesn't exactly mesh with the purpose of the
However, listing only the files in the current directory is not unusual if you're like me and prefer to use a customized
ls (and maybe you've even aliased
tree with your preferred flags and arguments).
Leveraging a combination of
grep will get you what you want:
$ tree -F . ├── file1.txt ├── file5.txt ├── parent_dir1/ │ ├── child_dir/ │ ├── file2.txt │ └── file3.txt └── parent_dir2/ └── child_dir2/ └── file4.txt 4 directories, 4 files $ tree -FL 1 | grep -v /$ . ├── file1.txt ├── file5.txt 2 directories, 1 file
The 'F' flag causes
tree to append a '/' to directories, and the 'v' flag to
grep inverts the given pattern, which matches all lines ending in '/'.
You'll notice that even the summary at the end is still applicable, despite the fact that the directories aren't actually being displayed.
Once you go more than one level down, the tree structure dominates and things get a bit weird.
$ tree -FL 3 | grep -v /$ . ├── file1.txt ├── file5.txt │ ├── file2.txt │ └── file3.txt └── file4.txt 4 directories, 4 files
Also, if the sort of broken tree output bothers you (i.e. the fact that some files are prefaced by the '├─' character instead of the '└─' character, and the multi-level version just seems to trail off...), you can fix this by sorting the output, directories first.
$ tree --dirsfirst -FL 1 | grep -v /$ . ├── file1.txt └── file5.txt 2 directories, 1 file $ tree --dirsfirst -FL 3 | grep -v /$ . │ ├── file2.txt │ └── file3.txt │ └── file4.txt ├── file1.txt └── file5.txt 4 directories, 5 files
I think this actually makes the node-less tree structure more readable, as well.
Consider the following command:
tree -ifF /usr/java/default/jre/lib/images | grep -v '/$'
/usr/java/default/jre/lib/images /usr/java/default/jre/lib/images/cursors/cursors.properties /usr/java/default/jre/lib/images/cursors/invalid32x32.gif /usr/java/default/jre/lib/images/cursors/motif_CopyDrop32x32.gif /usr/java/default/jre/lib/images/cursors/motif_CopyNoDrop32x32.gif /usr/java/default/jre/lib/images/cursors/motif_LinkDrop32x32.gif /usr/java/default/jre/lib/images/cursors/motif_LinkNoDrop32x32.gif /usr/java/default/jre/lib/images/cursors/motif_MoveDrop32x32.gif /usr/java/default/jre/lib/images/cursors/motif_MoveNoDrop32x32.gif /usr/java/default/jre/lib/images/icons/sun-java_HighContrastInverse.png /usr/java/default/jre/lib/images/icons/sun-java_HighContrast.png /usr/java/default/jre/lib/images/icons/sun-java_LowContrast.png /usr/java/default/jre/lib/images/icons/sun-java.png 2 directories, 12 files
-f arguments cause
tree to output full paths on each line, rather than indenting. The
-F argument causes it to append an
/ to directory names, which are filtered out by the inverted grep (
grep -v '/$').
I often use a variant of this to help me impatiently watch the contents of an output directory (and its subdirectories) for activity while running some other command...
watch exec "tree -ifFsD /var/local/myapp/data | grep -v '/$'"
old thread, but as -F now(?) appends different characters to show file types:
-F Append a
/' for directories, a=' for socket files, a
*' for executable files, a>' for doors (Solaris) and a `|' for FIFO's, as per ls -F
the command could become:
watch exec "tree -ifFsD /var/local/myapp/data | grep '[[:alnum:]]$\|\*'"