You can use a program like tree to print a directory structure in the form of a tree. If you do not have tree installed or you want to get the similar output without tree program then you can combine find and sed commands to get tree command like output.
Example:
Install tree:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
## Linux RHEL/CetsOS sudo yum install tree -y ## Linux Ubuntu/Debian OS sudo apt-get install tree -y ## Linux fedora OS sudo dnf install tree ## Linux openSUSE OS sudo zypper in tree ## Windows OS ## Install Chocolety if not already installed ## Open a command propmt as admin and execute below command @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin" choco install tree ## MAC OS ## Install Homebrew if not already installed /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install tree |
Create some files and directories:
1 2 3 4 |
## Create some files and directories mkdir -p mydir1/{mydir21/mydir211,mydir11/mydir111} echo "hello" > mydir1/mydir11/mydir111/myfile1111 echo "world" > mydir1/myfile21 |
Print directory structure in the form of a tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
## Uisng tree tree mydir1 ## returns ## mydir1 ## ├── mydir11 ## │ └── mydir111 ## │ └── myfile1111 ## ├── mydir21 ## │ └── mydir211 ## └── myfile21 ## Using native commands like find and sed find mydir1 | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/" ## returns ## mydir1 ## |-mydir11 ## | |-mydir111 ## | | |-myfile1111 ## |-mydir21 ## | |-mydir211 ## |-myfile21 rm -r my* |