Linux命令find一个目录并去那里

我想find保持一个特定的文件和CD在那里的目录。 例如

find * -name hello.txt 

输出:Documents / Projects / hello.txt

 cd Documents/Projects 

如何pipe理这些命令? 谢谢!

尝试

 cd $(dirname$(find /path -name hello.txt | head -n 1)) 

要么

 cd $(find /path -name hello.txt | head -n 1 | xargs dirname) 

你需要提供一个searchpath*在你的上面将不会工作,因为shell会扩展它。

编辑,如果你有你的文件名中的空格

 cd $(find /home -name 'he llo.txt' -print0 -quit | xargs -0 dirname) 

如果你的目录名中也有空格

  cd "$(find /path -name 'hello.txt' -print0 -quit | xargs -0 dirname)" 

find第一个hello.txt文件后,不要find所有的head -1 ,只要使用-quit选项使find命令停止:

 $ cd $(dirname $(find /path -name hello.txt -print -quit))