bash - 如何通过管道查找路径的基本名称

标签 bash shell find basename

这不起作用:

find "$all_locks" -mindepth 1 -maxdepth 1 -type d | basename

显然 basename 无法从 stdin 读取 - 在任何情况下 basename 都需要至少一个参数。

最佳答案

要将命令应用于管道操作的每个结果,xargs是你的 friend 。正如我链接的手册页上所说的那样......

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input.



在这种情况下,这意味着它将从您的 find 中获取每个结果。命令并运行 basename <find result> ad nauseum,直到 find 完成其搜索。我相信你想要的看起来很像这样:
find "$all_locks" -mindepth 1 -maxdepth 1 -type d | xargs basename

关于bash - 如何通过管道查找路径的基本名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56511834/

相关文章:

bash - awk 提取列但仅从一行中提取

bash - 为组中的进程抑制 SIGINT

linux - 为 tar 文件创建新脚本返回错误

linux - 打包程序选项以避免在 shell 配置时发出警告

macos - 在 macOS 上使用查找时出现 "Illegal option"错误

linux - 如何在 Bash 中终止 TCP 端口 16969?

java - 我的 bash 脚本锁定文件,而 Java 需要读取它

c - 如何获取从 shell 调用的二进制文件的文本段的基地址?

javascript - 如何根据两个选择器查找元素?

java - 以某种模式在 Linux 中搜索 Java 文件