Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I find all subfolders containing a given file?

For example, in a Minecraft server installation, the different worlds are stored in subfolders with arbitrary names. To identify that a given folder represents a world, I have to look for a level.dat inside it. Is there a command that finds all level.dat files and returns the folders that contain them?

share|improve this question
add comment

5 Answers

up vote 7 down vote accepted

Simply find your file and return the dirname:

find -name 'level.dat' -exec dirname {} \;
share|improve this answer
add comment

Or, find your files and extract the directory names in bulk:

find starting_directory –name level.dat –print | sed 's@/level.dat$@@'

share|improve this answer
add comment
    find ~ -type f -name "level.dat" -printf '%h\n'

if there may be more then one file with this name, then pipe to uniq

share|improve this answer
add comment

Or implement your find yourself ;)

myfind(){ shopt -s nullglob; [[ -f $1 ]] && pwd; for i in */ ; do ( cd $i;myfind $1 ) done }
myfind level.dat
share|improve this answer
add comment

You can use find to print only files that pass a certain test. I like this approach:

find . -type d -execdir test -f {}/level.dat \; -print

This finds all directories, that contains a file named level.dat.

share|improve this answer
add comment

Your Answer

 
discard

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.