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
精彩评论