-   Linux - General (
-   -   find a folder containing a specific file (

amicke 04-20-2009 09:36 AM

find a folder containing a specific file

I have tried in different ways of doing this but can't figure it out.

How should the bash-command find be typed to show folders containing a specific file?
I only want a folder to be shown once. So it looks trough a folder find a given file, return it, then continue with next folder. This is because one folder may have lot's of these files and I would only like to run the -execdir once on each folder.



druuna 04-20-2009 09:53 AM


Maybe not the most elegant and there are probably other ways, but this should work:

find . -type f -name "YOUR_FILENAME_HERE" | sed 's%\(.*/\).*%\1%' | sort -u

Find gives back all the files, the sed command strips the file from the dir structure, the sort command sorts them (duh...) and shows only unique output.

Hope this helps.

colucix 04-20-2009 09:57 AM

Something like extract the directory name (path) and remove duplicate entries? You can try something like this:

find /path/to/search/dir -name file\* -exec dirname {} \; | sort -u
Edit: beaten by druuna ;)

amicke 04-20-2009 10:04 AM

Thanks for the answers! But I run an apple TV and do not have the sort command.

However I have installed unrar and figured out a ugly but working solution.


find . -name *.rar -execdir unrar x {} -o- \;

All times are GMT -5. The time now is 04:06 AM.