根据管道的输出更改目录

mus*_*fa 4 shell bash xargs cd-command

我想使用过滤器转到目录

例如有一个名为的文件 this-is-awsome

ls | grep this-is-awsome | xargs cd
Run Code Online (Sandbox Code Playgroud)

如何进入带有过滤器的目录?

Jos*_* R. 9

索尔顿的评论解释了这个问题。以下是一些解决方案:

cd "$(ls | grep this)"
Run Code Online (Sandbox Code Playgroud)

这可能不是那么好,所有关于解析ls应用到它的输出的常见警告。

稍微好一点的版本(假设为 GNU find):

cd "$(find -maxdepth 1 -type d -name '*this*')"
Run Code Online (Sandbox Code Playgroud)

如果您使用 Bash,还有另一个(可能更好)解决方案:

shopt -s nullglob
cd *this*/
Run Code Online (Sandbox Code Playgroud)