4

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?

5 Answers 5

10

Simply find your file and return the dirname:

find -name 'level.dat' -exec dirname {} \;
2

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

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

1
    find ~ -type f -name "level.dat" -printf '%h\n'

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

1

Or implement your find yourself ;)

myfind(){ shopt -s nullglob; [[ -f $1 ]] && pwd; for i in */ ; do ( cd $i;myfind $1 ) done }
myfind level.dat
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .