How to show all sub-directories beloging to a directory with find in Linux

Saturday, 27th November 2010

I’ve recently had to write a shell script for which I was required to list all the subdirectories that belongs to a certain directory to do so I had to execute the following simple command line;

find . -type d -ls|awk '{ print $11 }'

Before executing the above linux find command you will need to go to the directory in which you’d like to list all the subdirectories.

cd /some/directory/

Now the list of all sub-directories will appear. This command is very useful if it’s combined with a for bash cycle in my script I used the sub-directories find with the following for bash loop;

for f in $(find . -type d -ls|awk '{ print $11 }'); do
# place here something to do with each of the element of the subdirectory list
done

In my case I used it to download some pictures from a remote website to which I had no access but had the general structure of it, so within the loop I used the wget to fetch my missing pictures after extracting the pictures names from the mysql database.
Still the usage of the above loop and find command can be combined and probably be useful for many other admin tasks to achieve.

Share this on:

Download PDFDownload PDF

Tags:

Leave a Reply

CommentLuv badge