Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a folder with hundreds of subfolders. I need to locate recursively within the subfolders those that don't have certain files inside (for instance *ch.pdf or *.jpg) and export the results to a txt file. Thx in advance

share|improve this question
have no fear executing the below command, it cannot harm your files ;) – Pavel A Feb 5 '13 at 13:08
up vote 2 down vote accepted

Here you are:

find <folder> -type d -exec bash -c "ls {} | grep -i \".*ch\.pdf$\|.*\.jpg$\" > /dev/null || echo {}" \; > <target file>

Brief explanation:

  1. list all folders inside <folder> recursively
  2. for each folder found - list all files and check whether there are any that end with "ch.pdf" or ".jpg". We are not interested in these files, so redirect the output to /dev/null.
  3. If there were no such files found, print relative folder path with echo.
  4. redirect the results of find to <target file>
share|improve this answer
will try this tomorrow and reply back.just to check, there may be a problem with case sensitive files, probably there are some with capital letters. a -i or something somewhere at the command perhaps? thanks – Celso.Baia Feb 5 '13 at 18:57
@Celso.Baia yes, you are right. If different case letters are expected we need to add '-i' option to grep - fixed the command accordingly. – Pavel A Feb 5 '13 at 19:36
works as expected :) – Celso.Baia Feb 5 '13 at 22:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.