Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

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
up vote 7 down vote accepted

Simply find your file and return the dirname:

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

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
    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

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

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

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.