I have the following commands. Wherever the .user.log
file is present, we need to print the parent directories (i.e hht
and wee1
.) How can this be done?
$ cd /nfs//office/ && find . -name '.user.log'
./hht/info/.user.log
./wee1/info/.user.log
Am I missing something here. Surely all this regex and/or looping is not necessary, a one-liner will do the job. Also "for foo in $()" solutions will fail when there are spaces in the path names.
Just use dirname twice with xargs, to get parent's parent...
# make test case
mkdir -p /nfs/office/hht/info
mkdir -p /nfs/office/wee1/info
touch /nfs/office/hht/info/.user.log
touch /nfs/office/wee1/info/.user.log
# parent's parent approach
cd /nfs//office/ && find . -name '.user.log' | xargs -I{} dirname {} | xargs -I{} dirname {}
# alternative, have find print parent directory, so dirname only needed once...
cd /nfs//office/ && find . -name ".user.log" -printf "%h\n" | xargs -I{} dirname {}
Produces
./hht
./wee1
You can do this easily with the formatting options of the -printf
action of find
(see man find
).
cd /nfs//office/ && find . -name '.user.log' -printf "%h\n"
./hht/info
./wee1/info
From the man page:
%h\n
will print path for each file on a new line.
Please note that -printf
is GNU-only. It won't work on macOS (a BSD system).
for file in $(find /nfs/office -name .user.log -print)
do
parent=$(dirname $(dirname $file))
echo $parent
done
EDIT: Sorry missed that you want the grandparent directory.
@trojanfoe has the right idea; this is just a way to get it to work safely with any filename, and pretty much any command within the loop:
while IFS= read -r -d '' -u 9
do
echo "$(dirname -- "$(dirname -- "$REPLY")")"
done 9< <( find "/nfs/office/" -name '.user.log' -print0 )
If you want it to echo only the unique names:
while IFS= read -r -d '' -u 9
do
echo "$(dirname -- "$(dirname -- "$REPLY")")"
done 9< <( find "/nfs/office/" -name '.user.log' -print0 ) | sort -u
find /nfs/office -name '.user.log' | while read line
do
echo $line | awk -F/ '{print $(NF-1),$NF}'
done
You could do something like this:
cd /nfs/office/ && find . -name '.user.log' | xargs -n1 -I{} expr match {} '\(\.\(\/[^/]*\/\)\?\)'
where the xargs
uses expr match
to extract the part that starts with .
until the first match of directory between slash characters (/dir/
).
An alternative version using sed
would be as follows:
cd /nfs/office/ && find . -name 'file.txt' | sed -r 's|(\./([^/]*/)?).*|\1|'
find -type f -exec bash -c 'echo "${1%/*}"' bash {} \;