Linux命令找到一个目录并去那里

我想找到保存某个文件和CD的目录.例如
find * -name hello.txt

输出:Documents / Projects / hello.txt

cd Documents/Projects

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

解决方法

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

要么

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

你需要提供一个搜索路径,*在你的上面不会工作,因为shell会扩展它.

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

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

如果你的目录名也有空格

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

相关文章

1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...
1 删除0字节文件 find -type f -size 0 -exec rm -rf {} ...
## 步骤 1:安装必要的软件包 首先,需要确保系统已安装 `dh...