When trying to get to know a new system I really like the ‘tree‘ command. It gives me a fast and nice overview of the filesystem of the application. Sometimes I work on systems that does not have this great tool available and for those occasions I made this bash script:
#!/bin/bash olddir=$PWD; declare -i dirdepth=0; function listfiles { cd "$1"; for file in * do for ((i=0; $i < $dirdepth; i++)) do ##Tab between each level printf "\t"; done ## Print directories with brackets ([directory]) if [ -d "$file" ] then printf "\1[$file]\n"; else printf "$file\e[0m\n"; fi ##Work our way thru the system recursively if [ -d "$file" ] then dirdepth=$dirdepth+1; listfiles "$file"; cd ..; fi done ##Done with this directory - moving on to next file let dirdepth=$dirdepth-1; } listfiles "$1"; ##Go back to where we started cd $olddir; unset i dirdepth;
To use the script just do the following:
- Create a new file called 'tree.sh' (or whatever you like)
- Paste the code into the file and save
- Make the file executable
- Run the file: . tree.sh
This script is tested on OSX 10.6.8, Red Hat Enterprise Linux AS release 3 (Taroon Update 9)
ā My script works: and is significantly shorter:
#!/bin/bash
pwd=$(pwd)
echo Tree of: $pwd
find $pwd -print | sed -e “s;$pwd;\.;g;s;[^/]*\/;|__;g;s;__|; |;g”
#very simple script. REALLY!
echo ‘|__end tree’
yep, I agree. This is a very simple solution. Thank you for sharing it!
line 4: syntax error near unexpected token `|’
Copy and paste, so what’s wrong with:
find $pwd -print | sed -e ās;$pwd;\.;g;s;[^/]*\/;|__;g;s;__|; |;gā
Quite often when you copy and paste from web pages some characters get translated to your own charset. This could lead to some characters look like they are correct but the underlying ASCII code is different. Try to input all the characters by hand and not via copy-paste if you experience any problems with the code
Bonjour ;
It seems not working with directory with a space in the name.
Never noticed that but you are of course correct. I usually do not work with such systems so I’ll leave it as it is for now. If you want to post a solution you are most welcome to do so š