Bash:按内容对 'find' 中的文件进行排序

标签 bash shell sorting find piping

我有这条线:

find -maxdepth 1 -type f -iname '*key*'  -not -name '*~'

我想提取所有返回文件的内容(应该是文本)并将其通过管道传输到 sort 中以按字母顺序排序。我已经尝试将上述行的输出直接传送到 sort 中,但这会导致文件名而不是它们的内容被排序。我是否需要将 find 的输出转换为一个数组,然后通过 sort 对其进行处理?

[edit] 我想要的输出是排序后的内容。

最佳答案

为了完整起见,这里还有几种方法:

  1. find -maxdepth 1 -type f -iname '*key*' -not -name '*~' -exec cat {}\; |排序
  2. find -maxdepth 1 -type f -iname '*key*' -not -name '*~' | xargs 猫 |排序
  3. cat $(find -maxdepth 1 -type f -iname '*key*' -not -name '*~') |排序

关于Bash:按内容对 'find' 中的文件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18158365/

相关文章:

bash - 在 Unix 上 grep 和 cut 之后在每行之前回显文本

javascript - 排序javascript的元素

c - Linux/bash 中程序返回值的有效范围是多少?

linux - Linux中如何在用户注销时自动运行脚本?

bash - 当我尝试打印数组的特定位置时替换错误

linux - Bash 脚本 - Bash 脚本 - 编辑文件中文本的行

arrays - Swift Map 排序闭包

ios - 对包含 NSString 对象的 NSMutableArray 进行排序

json - Cloudformation echo json 环境变量

javascript - 更漂亮的 shell 脚本格式