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
    • 最新
    • 标签
主页 / server / 问题 / 47969
Accepted
Peter Smit
Peter Smit
Asked: 2009-07-31 03:38:33 +0800 CST2009-07-31 03:38:33 +0800 CST 2009-07-31 03:38:33 +0800 CST

如何使用基本的 unix 工具从路径名中剪切扩展名

  • 772

我需要收集文件列表并将其放入文本文件中。但是,文件不应包含其扩展名。

我现在没有删除文件扩展名的命令:

ls -1 /a/dir/*/dir/* > textfile

所有选定文件的扩展名为 .[az]{3}

(我需要这个的原因是因为我需要一个唯一的基本名称列表,并且unique会在这一步之后解决这个问题)

linux bash
  • 7 7 个回答
  • 9125 Views

7 个回答

  • Voted
  1. penkoad
    2009-07-31T04:10:59+08:002009-07-31T04:10:59+08:00

    类似的东西会列出当前目录中的文件,删除第一个点之后的所有内容。

    find . -maxdepth 1 -type f | xargs -iZ basename Z | cut -d"." -f1 > /tmp/resultfile

    最终会出现隐藏文件问题,导致结果文件中出现空白行。Basename 在这里只是为了摆脱查找结果中以文件名开头的“./”。

    • 5
  2. Best Answer
    Kyle Brandt
    2009-07-31T04:12:12+08:002009-07-31T04:12:12+08:00

    我总是喜欢包含一个仅限 bash 的解决方案:

    for file in /a/dir/*/dir/*; do
        echo "${file%.*}" >> ~/file_list
    done
    

    并使用可选的 if 语句将文件限制为您请求的扩展名:

    shopt -s extglob
    for file in /a/dir/*/dir/*; do
        if [[ "$file" =~ \.[a-zA-Z]{3}$ ]]; then
            echo "${file%.*}" >> ~/file_list
        fi
    done
    

    在这个类似的服务器故障问题中,我解释了“${file%.*}”参数扩展。

    • 4
  3. Russell Heilling
    2009-07-31T03:44:32+08:002009-07-31T03:44:32+08:00

    怎么样:

    ls -1 /a/dir/*/dir/* | sed -e 's/\.[a-zA-Z0-9]\{3\}$//'
    

    应该做你想做的。

    • 3
  4. asdmin
    2009-07-31T09:52:45+08:002009-07-31T09:52:45+08:00

    它比人们想象的要简单得多:

    asd@locutus:~$ IZE=file.avi
    asd@locutus:~$ echo $IZE
    文件.avi
    asd@locutus:~$ echo ${IZE%%.???}
    文件
    asd@locutus:~$
    

    所以以这种方式:

    ls -1 /a/dir/*/dir/* | 在读取 VAR 时;做回声 ${VAR%.???}; 完毕
    

    你可以在do和done之间做任何你想做的事情,echo只是一个例子。

    man bash(参数扩展):

       ${parameter%%word}
              The word is expanded to produce a pattern just as in pathname  expansion.
              If the pattern matches a trailing portion of the expanded value of param‐
              eter, then the result of the expansion is the expanded value of parameter
              with the shortest matching pattern (the ‘‘%’’ case) or the longest match‐
              ing pattern (the ‘‘%%’’ case) deleted.  If parameter is @ or *, the  pat‐
              tern  removal  operation is applied to each positional parameter in turn,
              and the expansion is the resultant list.  If parameter is an array  vari‐
              able subscripted with @ or *, the pattern removal operation is applied to
              each member of the array in turn, and  the  expansion  is  the  resultant
              list.
    

    • 2
  5. afternoon
    2011-03-02T04:33:32+08:002011-03-02T04:33:32+08:00

    一个更通用的答案,找到任何长度的扩展:

    find . -type f | sed 's/^.*\.\([^.]*\)$/\1/'
    

    然后我使用以下命令获取目录中的扩展名列表:

    find . -type f | sed 's/^.*\.\([^.]*\)$/\1/' | sort | uniq -c | sort -n
    
    • 1
  6. Aschen
    2014-12-02T02:50:50+08:002014-12-02T02:50:50+08:00

    您也可以像这样使用 cut :

    for file in /a/dir/*/dir/* ; do
      echo \`echo $file | cut -d"." -f 1` >> textfile;
    done
    

    仅当您没有任何带有“。”的文件或目录时,它才会起作用。在名字里

    • 1
  7. nik
    2009-07-31T03:50:03+08:002009-07-31T03:50:03+08:00

    您已经给出了扩展的明确定义,
    但是,标签linux并bash-scripting建议了一个 unix 环境。
    除非您在 Windows 上使用 Cygwin 执行此操作。
    在那种情况下,这些点可能无关紧要......

    • 确保您的“分机”中没有数字(例如md5)
    • 另外,少于 3 个字符的扩展名呢?( script.sh)
    • 带有大写字符的扩展名呢?
    • 0

相关问题

  • 多操作系统环境的首选电子邮件客户端

  • 你最喜欢的 Linux 发行版是什么?[关闭]

  • 更改 PHP 的默认配置设置?

  • 保护新的 Ubuntu 服务器 [关闭]

  • (软)Ubuntu 7.10 上的 RAID 6,我应该迁移到 8.10 吗?

Sidebar

Stats

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

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve