AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 930847
Accepted
Jos
Jos
Asked: 2017-07-01 14:34:10 +0800 CST2017-07-01 14:34:10 +0800 CST 2017-07-01 14:34:10 +0800 CST

大目录中最长的文件名

  • 772

在我的 Ubuntu 系统上的一个大目录(>140000 个文件和 >200 个子目录)中,我知道某处有两个文件的名称太长而无法复制到 Windows (NTFS) 文件夹。我试了一下,收到两条错误消息,但我没有注意文件在哪些子文件夹中。

如何找到名称最长的两个文件?

command-line
  • 2 2 个回答
  • 3923 Views

2 个回答

  • Voted
  1. Best Answer
    Ravexina
    2017-07-02T00:11:58+08:002017-07-02T00:11:58+08:00

    我猜@steeldriver 的解决方案是一个更好的选择,但这是我的替代解决方案,您可以使用命令组合来准确找到两个(或更多)最长的文件名。

    find . | awk 'function base(f){sub(".*/", "", f); return f;} \
    {print length(base($0)), $0}'| sort -nr | head -2
    

    输出将是:

    length ./path/to/file
    

    这里有一个真实的例子:

    42 ./path/to/this-file-got-42-character-right-here.txt
    31 ./path/to/this-file-got-31-character.txt
    

    笔记

    find为我们提供了该目录中所有文件的列表,例如:

    ./path/to/this-file-got-31-character.txt
    

    使用awk我们将文件长度添加到每行的开头(它正是文件长度而不是路径的长度):

    31 ./path/to/this-file-got-31-character.txt
    

    最后我们根据文件长度对其进行排序,并使用head.

    • 10
  2. steeldriver
    2017-07-02T06:22:30+08:002017-07-02T06:22:30+08:00

    根据评论,在这种情况下,您真正​​需要的是名称超过某个最大字符数的所有文件的列表 - 幸运的是,使用findregex相对容易:

    find $PWD -regextype posix-extended -regex '.*[^/]{255,}$'
    

    对于如此大量的文件和目录,您可能希望避免排序 - 而是让我们只保留最长和第二长文件名及其完整路径名的运行记录:

    find $PWD -printf '%p\0' | awk -v RS='\0' '
      {
        # get the length of the basename of the current filepath
        n = split($0,a,"/");
        currlen = length(a[n]);
    
        if (currlen > l[1]) {
          # bump the current longest to 2nd place
          l[2] = l[1]; p[2] = p[1];
          # store the new 1st place length and pathname
          l[1] = currlen; p[1] = $0;
        }
        else if (currlen > l[2]) {
          # store the new 2st place length and pathname
          l[2] = currlen; p[2] = $0;
        }
      }
    
      END {
          for (i in l) printf "(%d) %d : %s\n", i, l[i], p[i];
      }'
    

    或使用 GNU awk(支持 2D 数组)

    $ find $PWD -printf '%p\0' | gawk -v RS='\0' '
      {
        # get the length of the basename of the current filepath
        n = split($0,a,"/");
        currlen = length(a[n]);
    
        if (currlen > p[1][1]) {
          # bump the current longest to 2nd place
          p[2][1] = p[1][1]; p[2][2] = p[1][2];
          # store the new 1st place length and pathname
          p[1][1] = currlen; p[1][2] = $0;
        }
        else if (currlen > p[2][1]) {
          # store the new 2st place length and pathname
          p[2][1] = currlen; p[2][2] = $0;
        }
      }
    
      END {
          for (i in p[1]) printf "(%d) %d : %s\n", i, p[i][1], p[i][2];
      }'
    
    • 4

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve