How to LS a Tree in Unix
Shape the Unix LS command line output when browsing files and directories using command line switches that change the data presented on the screen to include the information you require. However, certain views, such as a directory tree view, require the help of other command line tools in addition to the LS command line switches. Use the Sed stream editor from the Unix command line to shape LS utility command line output so that directory tree structure is presented on the screen.
Things You'll Need
- Path to the directory that is the top of the directory path for which you want to view the directory tree
Instructions
-
-
1
Open a command line terminal console.
-
2
Enter "cd /path_to_top_directory/" on the command line, and press the "Enter" key. Type "pwd" at the command prompt, and tap the "Enter" key. View the command line output, and confirm that the directory name displayed is the name of the directory at the top of the directory tree you wish to view.
-
-
3
Type "ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'" at the command prompt, and press the "Enter" key. The directory tree will display in the command line output.
-
1
References
- The Geek Stuff: Unix LS Command: 15 Practical Examples
- Computer Hope: Linux and Unix LS Command Help
- Grymoire: Sed -- An Introduction and Tutorial
- Unix Craft: How to Use Grep Command in Linux/Unix
- Computer Hope: Linux and Unix Grep Command
- Center Key Software: Unix Tree / Linux Tree -- Display Structure of Directory Hierarchy
- Photo Credit Comstock Images/Comstock/Getty Images