I have a parent folder named "parent_folder" with a lot of subfolders, in these subfolders is a file named "foo.mp4".
I can find these files easily by doing this:
mymacbook:parent_folder username$ find ./ -name "foo.mp4" -exec echo {} \;
Now that returns the path of each file, relative to parent_folder/
./path/to/foo.mp4
How can i return just the path, without the filename?
Best Answer
With
GNU
find:With other
find
s, provided directory names don't contain newline characters:Or:
though that means running one
dirname
command per file.If you need to run a command on that
path
, you can do (standard syntax):Though in this case, you may be able to use
-execdir
(a BSD extension also available in GNUfind
), whichchdir()
s to the file's directory:Beware though that while the GNU implementation of
find
will expand{}
to./filename
here, BSD ones expand tofilename
. It's OK here as the filename is passed as argument to an option and is alwaysfeatured.mp4
anyway, but for other usages you may have to take into account that the file name may start with-
or+
(and be understood as an option by the command) or contain=
(and be understood as a variable assignment by awk for instance), or other characters causing this kind of problem withperl -p/n
(not all of them fixed by GNUfind
's./
prefix though in that case), etc, which you may have to take into account.