Linux command to find a directory and go there
Try
cd $(dirname$(find /path -name hello.txt | head -n 1))
or
cd $(find /path -name hello.txt | head -n 1 | xargs dirname)
You'll need to provide a path
to search, *
in your above wouldn't work as the shell would expand it.
EDIT and if you have spaces in your filenames
cd $(find /home -name 'he llo.txt' -print0 -quit | xargs -0 dirname)
and if you have spaces in your directory names too
cd "$(find /path -name 'hello.txt' -print0 -quit | xargs -0 dirname)"
Instead of finding all and head -1
, just use -quit
option to make find
command stop after the first hello.txt
file was found:
$ cd $(dirname $(find /path -name hello.txt -print -quit))