开发者

How to use 'find' to return parent directory

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


one way of many:

find /   -name 'myfile' -type f -exec dirname {} \;


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'


If you want to execute something in the directory of the found file you may want to use -execdir action.

find /home/ -name 'myfile' -type f -print -execdir chmod -c 700 . \;


find /home/ -name 'myfile' -type f|awk -f"/" '{print $(NF-1), "/",$NF}'


find /home/ -name 'myfile' -type f | rev | cut -d "/" -f2- | rev | sort -u


cd /home; find . -name 'myfile' -type f | sed "s/\/[^/]*$//" | cut -d "/" -f2- | sort -u
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜