Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 'find' to return parent directory

Tags:

bash

I am using find to locate a file, but I want to only return the path to the parent directory of the file.

find /home/ -name 'myfile' -type f

That returns a list of the full path to all of the file matches, but I want to

like image 380
spyderman4g63 Avatar asked Nov 28 '22 07:11

spyderman4g63


2 Answers

one way of many:

find /   -name 'myfile' -type f -exec dirname {} \;
like image 50
jim mcnamara Avatar answered Feb 07 '23 15:02

jim mcnamara


The -printf action lets you extract lots of information about the file. '%h' is the directive to get the path part of the file name.

find /home/ -name 'myfile' -type f -printf '%h\n'

like image 21
Simon Avatar answered Feb 07 '23 15:02

Simon