Unix: Print Directory Tree Structure

The tree command has been around in Windows for a very long time to print out the directory structure but this is still not available in most of the Unix variants. Most of the new Linux distributions do seem to have the tree command available.

If the tree command is not available, you may try to run the following one-liner to simulate the tree command.

$ find ./ -type d | awk -F "/" '{ ld=0x2500; lt=0x251c; ll=0x2502; for (i=1; i<=NF-2; i++){printf "%c    ",ll} printf "%c%c %s\n",lt,ld,$NF }'

If the directories are being printed out without the lines appearing in the above command, try the alternative command below.

$ find ./ -type d | awk -F "/" '{ ld="-"; lt="+"; ll="|"; for (i=1; i<=NF-2; i++){printf "%c    ",ll} printf "%c%c %s\n",lt,ld,$NF }'

The output may not give the exact directory structure output as the native tree command, but it does get the job done.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react, vuejs); plays_on(xbox, ps4); linux_desktop_user(true); }

« Previous Article