Use find command to select directories with a file type

Posted on 2013-09-12
Medium Priority
Last Modified: 2013-09-13
I am using

find $dir -type d|grep -i $word

to retrieve a list of directories, case-insesnsitive, that contain word "TEST" in it's path.

Now I'd like to modify it to only retrieve those directories that contain files of type .txt

how can i accomplish that?
Question by:YZlat
LVL 68

Accepted Solution

woolmilkporc earned 1200 total points
ID: 39488374
Like this:

find $dir -type d|grep -i $word |while read DIR
    if ls $DIR/*.txt >/dev/null 2>&1
         echo $DIR
LVL 85

Expert Comment

ID: 39488418
perl -MFile::Find -le 'find {wanted=>sub{print if -d && /'$word'/ && <$_/*.txt>},no_chdir=>1},shift;' $dir

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question