Suppose I have a file structure:
$ cd /home/Desktop $ ls -d */ Abc/ Qwe/ Zxc/ Rty/ $
Now I want to iterate through every directory and see if they have any ".pdf" files. Could some one please tell what is the best way to do this?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
There are several different ways to do this, depending on what you want to do with the information.
If you want a list of directories which directly contain
dirname */*.pdf | sort | uniq
If you want a list of directories which contain
find . -name '*.pdf' | cut -d/ -f2 | sort | uniq
or, if you want to search only certain subdirectories,
find Abc Qwe Zxc Rty -name '*.pdf' | cut -d/ -f1 | sort | uniq
If you want a list of all the
find . -name '*.pdf'
If you want a count of
dirname */*.pdf | sort | uniq -c
or to include files in subdirectories
find . -name '*.pdf' | cut -d/ -f2 | sort | uniq -c
(you can tell the difference is simply adding the
-c option to