====== Ubuntu - Bash - Scripts - tree ====== **tree** recursively searches in each directory and finds all files, which are then displayed in a hierarchical file tree, thus providing global inspection of files and their nested depth structure. ---- ===== Usage ===== tree [dir] ---- Example call: tree $HOME ---- ===== Code ===== #!/bin/bash # # @ (#) tree v1.0 Visual display of a file tree # output to a file tree structure screen # if [ "$#" -gt 1] then echo "tree: wrong arg count"> & 2 echo "usage: tree [dir]"> & 2 exit 2 fi if [ "$#" -eq 1] then if [! -d $1] then echo "$0: $1 not a directory"> & 2 echo "usage: tree [dir]"> & 2 exit 2 fi fi find $ {1: -.} -print | sort | sed -e "1p" -e "1d" \ -e "s | [^ /] * / | / | g" \ -e "s | [^ * / | / |" \ -e "s | / \ ([^ /] * \) $ | \ 1 |" ---- ===== Example Usage ===== This uses an implicit directory (the current directory) as the beginning of the file tree. tree ---- Print the tree listing for each file of the entire system. tree / ---- This shows the tree-format for all other users system (assuming that all user directories are the same directory, such as /usr/*). tree $HOME / .. ---- ===== Test ===== Consider the example of a directory structure. Let the root directory to /tmp with two directories: a and b. The catalog is a subdirectories log aa, which contains the file1 file directory and b, respectively, subdirectory bb, containing file file2. The **find** command will give a printout like this: find /tmp -print result: /tmp /tmp/a /tmp/a/aa /tmp/a/aa/file1 /tmp/b /tmp/b/bb /tmp/b/bb/file2 As can be seen from this listing, the files a and aa are directories and file file1 is located at the bottom of the file tree. ---- Now compare this result with the results of the **tree** script. tree /tmp result: /tmp /a //aa ///file1 /b //bb ///file2