【问题标题】:Sorting find command by filename按文件名排序查找命令
【发布时间】:2018-01-22 15:27:05
【问题描述】:

我希望仅按文件名而不是整个路径的字母顺序对 find 命令的输出进行排序。

假设我有一堆文本文件:

./d/meanwhile/in/b.txt
./meanwhile/c.txt
./z/path/with/more/a.txt
./d/mored/dddd/d.txt

我正在寻找输出:

./z/path/with/more/a.txt
./d/meanwhile/in/b.txt
./meanwhile/c.txt
./d/mored/dddd/d.txt

我试过了:

find . -type f -name '*.txt' -print0 | sort -t
find . -name '*.txt' -exec ls -ltu {} \; | sort -n
find . -name '*.txt' | sort -n

...在其他排列中。

【问题讨论】:

    标签: linux find alphabetical


    【解决方案1】:

    直接的方法是在两列中打印每个文件(记录)——文件名和路径——由一些确保永远不会出现在文件名中的字符分隔(-printf '%f\t%p\n'),然后仅对第一列的输出进行排序(sort -k1),然后剥离第一列(cut -d$'\t' -f2):

    find . -type f -name '*.txt' -printf '%f\t%p\n' | sort -k1 | cut -d$'\t' -f2
    

    请注意,这里我们使用\t(制表符)和@​​987654326@(换行符)作为字段和记录分隔符,假设它们不会作为任何文件名的一部分出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-19
      相关资源
      最近更新 更多