【问题标题】:Sorting file names by length of the file name按文件名长度对文件名排序
【发布时间】:2012-04-09 17:41:29
【问题描述】:

ls 显示目录中可用的文件。我希望根据文件名的长度显示文件名。

任何帮助将不胜感激。 提前致谢

【问题讨论】:

    标签: unix sorting


    【解决方案1】:

    你可以使用

    ls --color=never --indicator-style=none | awk '{print length, $0}' |
    sort -n | cut -d" " -f2-
    

    要查看它的实际效果,请创建一些文件

    % touch a ab abc
    

    还有一些目录

    % mkdir d de def
    

    普通 ls 命令的输出

    % ls
    a  ab  abc  d/  de/  def/
    

    建议命令的输出

    % ls --color=never --indicator-style=none | awk '{print length, $0}' |
    sort -n | cut -d" " -f2-
    a
    d
    ab
    de
    abc
    def
    

    【讨论】:

      【解决方案2】:

      TL;DR

      命令:

      find . -maxdepth 1 -type f -print0 | sed 's#\./.*/\([^/]\+\)\./$#\1#g' | tr '\n' '/' | perl -F'/\0/' -ape '$_=join("\n", sort { length($b) <=> length($a) } @F)' | sed 's#/#/\\n/#g'
      

      更易于阅读的替代命令版本:

      find . -maxdepth 1 -type f -print0 | \
      sed 's#\./.*/\([^/]\+\)\./$#\1#g' | tr '\n' '/' | \
      perl -F'/\0/' -ape \
        '$_=join("\n", sort { length($b) <=> length($a) } @F)' | \
      sed 's#/#/\\n/#g'
      

      Not Parsing ls 输出 AND 基准测试

      这里有很好的答案。但是,如果有人想听从not to parse the output of ls 的建议,这里有一些完成工作的方法。 这将特别注意文件名中有空格的情况。 我将在这里对所有内容以及 paring-ls 示例进行基准测试。(希望我很快就明白了。)我放了一堆在过去 25 年左右从不同地方下载的有点随机的文件名——一开始是 73 个。所有 73 个都是“普通”文件名,只有字母数字字符、下划线、点和连字符。我将添加 2 个我现在制作的(以显示某些问题)。

      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ mkdir ../dir_w_fnames__spaces
      
      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ cp ./* ../dir_w_fnames__spaces/
      
      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ cd ../dir_w_fnames__spaces/
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces
      $ touch "just one file with a really long filename that can throw off some counts bla so there"
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces
      $ mkdir ../dir_w_fnames__spaces_and_newlines
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces
      $ cp ./* ../dir_w_fnames__spaces_and_newlines/
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces
      $ cd ../dir_w_fnames__spaces_and_newlines/
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ touch $'w\nlf.aa'
      

      这个,即文件名,

      w
      lf.aa
      

      代表 with linefeed - 我这样做是为了更容易看到问题。我不知道为什么我选择.aa 作为文件扩展名,除了它使这个文件名长度在排序中很容易看到。

      现在,我要回到orig_dir_73 目录; 相信我,这个目录只包含文件。我们将使用可靠的方法来获取文件的数量。

      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ du --inodes
      74      .
      
      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ # The 74th inode is for the current directory, '.'; we have 73 files
      

      还有一种更可靠的方法,它不依赖于只有文件的目录,也不需要你记住额外的'.' inode。我只是浏览了man 页面,做了一些研究,做了一些实验。这个命令是

      awk -F"\0" '{print NF-1}' < <(find . -maxdepth 1 -type f -print0) | awk '{sum+=$1}END{print sum}'
      

      或者,以更易读的方式,

      awk -F"\0" '{print NF-1}' < \
        <(find . -maxdepth 1 -type f -print0) | \
          awk '{sum+=$1}END{print sum}'
      

      让我们看看我们有多少个文件

      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ awk -F"\0" '{print NF-1}' < \
        <(find . -maxdepth 1 -type f -print0) | \
          awk '{sum+=$1}END{print sum}'
      73
      
      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ cd ../dir_w_fnames__spaces
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces
      $ awk -F"\0" '{print NF-1}' < \
        <(find . -maxdepth 1 -type f -print0) | \
          awk '{sum+=$1}END{print sum}'
      74
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces
      $ cd ../dir_w_fnames__spaces_and_newlines/
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ awk -F"\0" '{print NF-1}' < \
        <(find . -maxdepth 1 -type f -print0) | \
          awk '{sum+=$1}END{print sum}'
      75
      

      (请参阅 [ 1 ] 了解详细信息以及导致此处出现命令的先前解决方案的边缘情况。)

      我将在这些目录之间来回切换;请确保您注意路径 - 我不会记录每个开关。


      * 即使使用奇怪的文件名(包含空格、换行符等)也可以使用

      1a。 Perl à la @tchrist 与添加

      使用 find 带空分隔符。修改文件名中的换行符。

      命令:

      find . -maxdepth 1 -type f -print0 | sed 's#\./.*/\([^/]\+\)\./$#\1#g' | tr '\n' '/' | perl -F'/\0/' -ape '$_=join("\n", sort { length($b) <=> length($a) } @F)' | sed 's#/#/\\n/#g'
      

      更易于阅读的替代命令版本:

      find . -maxdepth 1 -type f -print0 | \
      sed 's#\./.*/\([^/]\+\)\./$#\1#g' | tr '\n' '/' | \
      perl -F'/\0/' -ape \
        '$_=join("\n", sort { length($b) <=> length($a) } @F)' | \
      sed 's#/#/\\n/#g'
      

      我将实际显示部分排序结果以表明以下命令有效。我还将展示我如何检查奇怪的文件名是否没有破坏任何内容。

      请注意,如果想要整个排序列表(希望它不是sordid 列表),通常不会使用headtail。我正在使用这些命令进行演示。

      首先,“正常”文件名。

      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ find . -maxdepth 1 -type f -print0 | \
      sed 's#\./.*/\([^/]\+\)\./$#\1#g' | tr '\n' '/' | \
      perl -F'/\0/' -ape \
        '$_=join("\n", sort { length($b) <=> length($a) } @F)' | \
      sed 's#/#/\\n/#g' | head -n 5
      68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f776174747061642d6d656469612d736572766963652f53746f7279496d6167652f71526c586e654345744a365939773d3d2d3435383139353437362e313464633462356336326266656365303439363432373931333139382e676966.txt
      oinwrxK2ea1sfp6m8o49255f679496d6167652f71526c586e654345744a365939773d3d2d343538b3e0.csv
      79496d6167652f71526c586e654345744a365939773d3d2d343538sfp6m8o1m53hlwlfja.dat
      83dfee2e0f8560dbd2a681a5a40225fd260d3b428b962dcfb75d17e43a5fdec9_1.txt
      17f09d51d6280fb8393d5f321f344f616c461a57a8b9cf9cc3099f906b567c992.txt
      
      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ find . -maxdepth 1 -type f -print0 | \
      sed 's#\./.*/\([^/]\+\)\./$#\1#g' | tr '\n' '/' | \
      perl -F'/\0/' -ape \
        '$_=join("\n", sort { length($b) <=> length($a) } @F)' | \
      sed 's#/#/\\n/#g' | tail -n 5
      137.csv
      13.csv
      o6.dat
      3.csv
      a.dat
      
      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ # No spaces in fnames, so...
      
      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ find . -maxdepth 1 -type f | wc -l
      73
      
      • 适用于普通文件名

      下一步:空格

      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces
      $ find . -maxdepth 1 -type f -print0 | \
      sed 's#\./.*/\([^/]\+\)\./$#\1#g' | tr '\n' '/' | \
      perl -F'/\0/' -ape \
        '$_=join("\n", sort { length($b) <=> length($a) } @F)' | \
      sed 's#/#/\\n/#g' | head -n 5
      68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f776174747061642d6d656469612d736572766963652f53746f7279496d6167652f71526c586e654345744a365939773d3d2d3435383139353437362e313464633462356336326266656365303439363432373931333139382e676966.txt
      oinwrxK2ea1sfp6m8o49255f679496d6167652f71526c586e654345744a365939773d3d2d343538b3e0.csv
      just one file with a really long filename that can throw off some counts bla so there
      79496d6167652f71526c586e654345744a365939773d3d2d343538sfp6m8o1m53hlwlfja.dat
      83dfee2e0f8560dbd2a681a5a40225fd260d3b428b962dcfb75d17e43a5fdec9_1.txt
      
      • 适用于包含空格的文件名

      下一步:换行

      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ find . -maxdepth 1 -type f -print0 | \
      sed 's#\./.*/\([^/]\+\)\./$#\1#g' | tr '\n' '/' | \
      perl -F'/\0/' -ape \
        '$_=join("\n", sort { length($b) <=> length($a) } @F)' | \
      sed 's#/#/\\n/#g' | tail -8
      Lk3f.png
      LOqU.txt
      137.csv
      w/\n/lf.aa
      13.csv
      o6.dat
      3.csv
      a.dat
      

      如果您愿意,也可以稍微更改此命令,使文件名带有“已评估”的换行符。

      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ find . -maxdepth 1 -type f -print0 | \
      sed 's#\./.*/\([^/]\+\)\./$#\1#g' | tr '\n' '/' | \
      perl -F'/\0/' -ape \
        '$_=join("\n", sort { length($b) <=> length($a) } @F)' | \
      sed 's#/#\n#g' | tail -8
      LOqU.txt
      137.csv
      w
      lf.aa
      13.csv
      o6.dat
      3.csv
      a.dat
      

      在任何一种情况下,您都会知道,由于我们一直在做的事情,列表已排序,即使它看起来不是这样。

      (未按文件名长度排序的视觉效果)

      ********
      ********
      *******
      **********       <-- Visual Problem
      *****
      *****
      ****
      ****
      

      ********
      *******
      *                <-- Visual
      ****             <-- Problems
      *****
      *****
      ****
      ****
      
      • 适用于包含换行符的文件名

      * 2a。非常接近,但不将换行文件名放在一起 - à la @cpasm

      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ for i in *; do printf "%d\t%s\n" "${#i}" "$i"; done | sort -n | cut -f2- | head
      lf.aa
      3.csv
      a.dat
      13.csv
      o6.dat
      137.csv
      w
      1UG5.txt
      1uWj.txt
      2Ese.txt
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ for i in *; do printf "%d\t%s\n" "${#i}" "$i"; done | sort -n | cut -f2- | tail -5
      83dfee2e0f8560dbd2a681a5a40225fd260d3b428b962dcfb75d17e43a5fdec9_1.txt
      79496d6167652f71526c586e654345744a365939773d3d2d343538sfp6m8o1m53hlwlfja.dat
      just one file with a really long filename that can throw off some counts bla so there
      oinwrxK2ea1sfp6m8o49255f679496d6167652f71526c586e654345744a365939773d3d2d343538b3e0.csv
      68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f776174747061642d6d656469612d736572766963652f53746f7279496d6167652f71526c586e654345744a365939773d3d2d3435383139353437362e313464633462356336326266656365303439363432373931333139382e676966.txt
      

      请注意,对于 head 部分,w 中的

      w(\n)
      lf.aa
      

      对于 6 个字符长的文件名,它位于正确的排序位置。但是,lf.aa 不符合逻辑。


      * 不易损坏(只有 '\n' 和可能的命令字符可能是一个问题)

      1b。 Perl à la @tchrist with find,而不是 ls

      使用 find 带空分隔符和 xargs

      命令:

      find . -maxdepth 1 -type f -print0 | xargs -I'{}' -0 echo "{}" | sed 's#\./.*/\([^/]\+\)\./$#\1#g' | perl -e 'print sort { length($b) <=> length($a) } <>'
      

      更易于阅读的替代命令版本:

      find . -maxdepth 1 -type f -print0 | \
        xargs -I'{}' -0 \
          echo "{}" | sed 's#\./.*/\([^/]\+\)\./$#\1#g' | \
              perl -e 'print sort { length($b) <=> length($a) } <>'
      

      我们去吧。

      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ find . -maxdepth 1 -type f -print0 | \
        xargs -I'{}' -0 \
          echo "{}" | sed 's#\./.*/\([^/]\+\)\./$#\1#g' | \
            perl -e 'print sort { length($b) <=> length($a) } <>' | head -n 5
      68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f776174747061642d6d656469612d736572766963652f53746f7279496d6167652f71526c586e654345744a365939773d3d2d3435383139353437362e313464633462356336326266656365303439363432373931333139382e676966.txt
      oinwrxK2ea1sfp6m8o49255f679496d6167652f71526c586e654345744a365939773d3d2d343538b3e0.csv
      79496d6167652f71526c586e654345744a365939773d3d2d343538sfp6m8o1m53hlwlfja.dat
      83dfee2e0f8560dbd2a681a5a40225fd260d3b428b962dcfb75d17e43a5fdec9_1.txt
      17f09d51d6280fb8393d5f321f344f616c461a57a8b9cf9cc3099f906b567c992.txt
      
      bballdave025@MY-MACHINE /home/bballdave025/orig_dir_73
      $ find . -maxdepth 1 -type f -print0 | \
        xargs -I'{}' -0 \
          echo "{}" | sed 's#\./.*/\([^/]\+\)\./$#\1#g' | \
            perl -e 'print sort { length($b) <=> length($a) } <>' | tail -8
      IKlT.txt
      Lk3f.png
      LOqU.txt
      137.csv
      13.csv
      o6.dat
      3.csv
      a.dat
      
      • 适用于普通文件名
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces
      $ find . -maxdepth 1 -type f -print0 | \
        xargs -I'{}' -0 \
          echo "{}" | sed 's#\./.*/\([^/]\+\)\./$#\1#g' | \
            perl -e 'print sort { length($b) <=> length($a) } <>' | head -n 5
      68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f776174747061642d6d656469612d736572766963652f53746f7279496d6167652f71526c586e654345744a365939773d3d2d3435383139353437362e313464633462356336326266656365303439363432373931333139382e676966.txt
      oinwrxK2ea1sfp6m8o49255f679496d6167652f71526c586e654345744a365939773d3d2d343538b3e0.csv
      just one file with a really long filename that can throw off some counts bla so there
      79496d6167652f71526c586e654345744a365939773d3d2d343538sfp6m8o1m53hlwlfja.dat
      83dfee2e0f8560dbd2a681a5a40225fd260d3b428b962dcfb75d17e43a5fdec9_1.txt
      
      • 适用于包含空格的文件名
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ find . -maxdepth 1 -type f -print0 | \
        xargs -I'{}' -0 \
          echo "{}" | sed 's#\./.*/\([^/]\+\)\./$#\1#g' | \
            perl -e 'print sort { length($b) <=> length($a) } <>' | head -n 5
      68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f776174747061642d6d656469612d736572766963652f53746f7279496d6167652f71526c586e654345744a365939773d3d2d3435383139353437362e313464633462356336326266656365303439363432373931333139382e676966.txt
      oinwrxK2ea1sfp6m8o49255f679496d6167652f71526c586e654345744a365939773d3d2d343538b3e0.csv
      just one file with a really long filename that can throw off some counts bla so there
      79496d6167652f71526c586e654345744a365939773d3d2d343538sfp6m8o1m53hlwlfja.dat
      83dfee2e0f8560dbd2a681a5a40225fd260d3b428b962dcfb75d17e43a5fdec9_1.txt
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ find . -maxdepth 1 -type f -print0 | \
        xargs -I'{}' -0 \
          echo "{}" | sed 's#\./.*/\([^/]\+\)\./$#\1#g' | 
            perl -e 'print sort { length($b) <=> length($a) } <>' | tail -8
      LOqU.txt
      137.csv
      13.csv
      o6.dat
      3.csv
      a.dat
      lf.aa
      w
      

      警告

      • BREAKS 用于包含换行符的文件名

      1c。适用于普通文件名和带有空格的文件名,但可以使用包含换行符的文件名 - à la @tchrist

      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ ls | perl -e 'print sort { length($b) <=> length($a) } <>' | head -n 5
      68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f776174747061642d6d656469612d736572766963652f53746f7279496d6167652f71526c586e654345744a365939773d3d2d3435383139353437362e313464633462356336326266656365303439363432373931333139382e676966.txt
      oinwrxK2ea1sfp6m8o49255f679496d6167652f71526c586e654345744a365939773d3d2d343538b3e0.csv
      just one file with a really long filename that can throw off some counts bla so there
      79496d6167652f71526c586e654345744a365939773d3d2d343538sfp6m8o1m53hlwlfja.dat
      83dfee2e0f8560dbd2a681a5a40225fd260d3b428b962dcfb75d17e43a5fdec9_1.txt
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ ls | perl -e 'print sort { length($b) <=> length($a) } <>' | tail -8
      LOqU.txt
      137.csv
      13.csv
      o6.dat
      3.csv
      a.dat
      lf.aa
      w
      

      3a。适用于普通文件名和带有空格的文件名,但可以使用包含换行符的文件名 - à la @Peter_O

      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ ls | awk '{print length($0)"\t"$0}' | sort -n | cut --complement -f1 | head -n 8
      w
      3.csv
      a.dat
      lf.aa
      13.csv
      o6.dat
      137.csv
      1UG5.txt
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ ls | awk '{print length($0)"\t"$0}' | sort -n | cut --complement -f1 | tail -5
      83dfee2e0f8560dbd2a681a5a40225fd260d3b428b962dcfb75d17e43a5fdec9_1.txt
      79496d6167652f71526c586e654345744a365939773d3d2d343538sfp6m8o1m53hlwlfja.dat
      just one file with a really long filename that can throw off some counts bla so there
      oinwrxK2ea1sfp6m8o49255f679496d6167652f71526c586e654345744a365939773d3d2d343538b3e0.csv
      68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f776174747061642d6d656469612d736572766963652f53746f7279496d6167652f71526c586e654345744a365939773d3d2d3435383139353437362e313464633462356336326266656365303439363432373931333139382e676966.txt
      

      * 更容易破碎

      4a。适用于普通文件名 - à la @Raghuram

      这个版本可以被包含空格或换行符(或两者)的文件名破坏。

      我想补充一点,如果只是为了分析目的,我确实喜欢显示实际字符串长度。

      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ for i in `ls`; do LEN=`expr length $i`; echo $LEN $i; done | sort -n | head -n 20
      1 a
      1 w
      2 so
      3 bla
      3 can
      3 off
      3 one
      4 file
      4 just
      4 long
      4 some
      4 that
      4 with
      5 3.csv
      5 a.dat
      5 lf.aa
      5 there
      5 throw
      6 13.csv
      6 counts
      
      bballdave025@MY-MACHINE /home/bballdave025/dir_w_fnames__spaces_and_newlines
      $ for i in `ls`; do LEN=`expr length $i`; echo $LEN $i; done | sort -n | tail -5
      69 17f09d51d6280fb8393d5f321f344f616c461a57a8b9cf9cc3099f906b567c992.txt
      70 83dfee2e0f8560dbd2a681a5a40225fd260d3b428b962dcfb75d17e43a5fdec9_1.txt
      76 79496d6167652f71526c586e654345744a365939773d3d2d343538sfp6m8o1m53hlwlfja.dat
      87 oinwrxK2ea1sfp6m8o49255f679496d6167652f71526c586e654345744a365939773d3d2d343538b3e0.csv
      238 68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f776174747061642d6d656469612d736572766963652f53746f7279496d6167652f71526c586e654345744a365939773d3d2d3435383139353437362e313464633462356336326266656365303439363432373931333139382e676966.txt
      

      部分命令说明

      现在,我只需要注意,使用适用于所有人的 find 命令,我使用 '/' 代替换行符,因为它是文件名中唯一在 *NIX 上都是非法的字符和窗户。


      注意事项

      [ 1 ] 使用的命令,

      du --inodes --files0-from=<(find . -maxdepth 1 -type f -print0) | \
      awk '{sum+=int($1)}END{print sum}'
      

      在这种情况下会起作用,因为当有一个带有换行符的文件,因此在find 命令的输出中有一个“额外”行时,awkint 函数将评估为 0该链接的文本。具体来说,对于我们包含换行符的文件名,w\nlf.aa,即

      w
      lf.aa
      

      我们会得到

      $ awk '{print int($1)}' < <(echo "lf.aa")
      0
      

      如果您遇到文件名类似于

      的情况

      firstline\n3 and some other\n1\n2\texciting\n86stuff.jpg

      firstline
      3 and some other
      1
      2     exciting
      86stuff.jpg
      

      好吧,我猜是电脑打败了我。如果有人有解决方案,我很高兴听到。

      编辑我认为我对这个问题太深入了。从this SO answer 和实验,我得到了这个命令(我不明白所有的细节,但我已经很好地测试了它。)

      awk -F"\0" '{print NF-1}' < <(find . -maxdepth 1 -type f -print0) | awk '{sum+=$1}END{print sum}'
      

      更具可读性:

      awk -F"\0" '{print NF-1}' < \
        <(find . -maxdepth 1 -type f -print0) | \
          awk '{sum+=$1}END{print sum}'
      

      【讨论】:

      • perl -wE'say for sort { length $a &lt;=&gt; length $b } glob "*"'mapglob 之间加入grep { -f } 以仅包含普通文件。 (不是为了和你的学习竞争:)
      • @zdim 太棒了。这不与我的学习竞争,它促进了我的学习。我喜欢 SO,因为我一直在学习新事物。
      • 使用 find、xargs 和 perl。真是一团糟。
      【解决方案3】:
      for i in *; do printf "%d\t%s\n" "${#i}" "$i"; done | sort -n | cut -f2-
      

      【讨论】:

      • 不错!不需要perl。适用于带空格的文件名。
      【解决方案4】:

      最简单的方法就是:

      $ ls | perl -e 'print sort { length($b) <=> length($a) } <>'
      

      【讨论】:

      • Raghuram 上面的回答在 bash shell 中的 FreeBSD 上对我不起作用,但这个回答对我有用。必须爱perl。谢谢。
      【解决方案5】:

      制作测试文件:

      mkdir -p test; cd test 
      touch short-file-name  medium-file-name  loooong-file-name
      

      脚本:

      ls |awk '{print length($0)"\t"$0}' |sort -n |cut --complement -f1
      

      输出:

      short-file-name
      medium-file-name
      loooong-file-name
      

      【讨论】:

      • 前两个解决方案工作正常。感谢大家的回复。
      • ls|awk '{print length($0)"\t"$0}' |sort -n|cut -f2 适用于 BusyBox v1.01
      【解决方案6】:

      你可以这样做

      for i in `ls`; do LEN=`expr length $i`; echo $LEN $i; done | sort -n
      

      【讨论】:

      • 我认为它是根据文件大小排序,我希望它根据文件名的长度进行排序
      猜你喜欢
      • 2022-01-27
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      相关资源
      最近更新 更多