Linux:删除多个文件的文件扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4509485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:01:13  来源:igfitidea点击:

Linux: remove file extensions for multiple files

linuxbashrename

提问by rp101

I have many files with .txt extension. How to remove .txt extension for multiple files in linux?

我有很多扩展名为 .txt 的文件。如何在 linux 中删除多个文件的 .txt 扩展名?

I found that

我找到

rename .old .new *.old

substitutes .oldextension to the .new

替代.old扩展到.new

Also I want to do this for files in sub-folders.

我也想对子文件夹中的文件执行此操作。

采纳答案by thkala

renameis slightly dangerous, since according to its manual page:

rename有点危险,因为根据其手册页

rename will rename the specified files by replacing the firstoccurrence of...

重命名将通过替换第一次出现的...来重命名指定的文件

It will happily do the wrong thing with filenames like c.txt.parser.y.

它会很高兴地对像c.txt.parser.y.

Here's a solution using findand bash:

这是使用findand的解决方案bash

find -type f -name '*.txt' | while read f; do mv "$f" "${f%.txt}"; done

Keep in mind that this will break if a filename contains a newline (rare, but not impossible).

请记住,如果文件名包含换行符(很少见,但并非不可能),这将中断。

If you have GNU find, this is a more solid solution:

如果您找到 GNU,这是一个更可靠的解决方案:

find -type f -name '*.txt' -print0 | while read -d $'
for i in `find myfolder -type d`; do
  rename .old .new $i/*.old
done
' f; do mv "$f" "${f%.txt}"; done

回答by robert

You can explicitly pass in an empty string as an argument.

您可以显式传入一个空字符串作为参数。

rename .old '' *.old

rename .old '' *.old

And with subfolders, find . -type d -exec rename .old '' {}/*.old \;. {}is the substitute for the entry found with find, and \;terminates the arglist for the command given after -exec.

对于子文件夹,find . -type d -exec rename .old '' {}/*.old \;. {}是用 找到的条目的替代find,并\;终止在 之后给出的命令的 arglist -exec

回答by Konrad Garus

For subfolders:

对于子文件夹:

find ./ -name "*.old" -exec sh -c 'mv 
rename 's/\.txt$//' *.txt
`basename "
for file in *.old
      touch (basename "$file" .old).new
end
" .old`.new' '{}' \;

回答by Chris Cowan

I use this:

我用这个:

for f in ./**/*.old; do
    mv "${f}" "${f%.old}"
done

回答by Andrew S

The Perl version of rename can remove an extension as follows:

Perl 版本的 rename 可以删除扩展名,如下所示:

function chgext () {
    local srcext=".old"
    local dstext=""
    local dir="."

    [[ "$#" -ge 1 ]] && srcext=""
    [[ "$#" -gt 2 ]] && dstext="" dir="" || dir="${2:-.}"

    local bname=''
    for f in "${dir}"/**/*"${srcext}"; do
        bname="${f%${srcext}}"
        echo "${bname}{${srcext} → ${dstext}}"
        mv "${f}" "${bname}${dstext}"
    done
}

This could be combined with find in order to also do sub-folders.

这可以与 find 结合使用,以便也做子文件夹。

回答by Bruno Alano

In Fish, you can do

在鱼中,你可以做

chgext
chgext src
chgext src dir
chgext src dst dir

Where `src` is the extension to find (default: ".old")
      `dst` is the extension to replace with (default: "")
      `dir` is the directory to act on (default: ".")

回答by svvac

In case it helps, here's how I do it with zsh:

如果有帮助,以下是我使用 zsh 的方法:

for i in *;do mv ${i} ${i/%.pdf/} ;done

The ${x%pattern}construct in zsh removes the shortest occurence of patternat the end of $x. Here it is abstracted as a function:

${x%pattern}在zsh的结构去除的最短occurencepattern在年底$x。这里抽象为一个函数:

##代码##

Usage:

用法:

##代码##

回答by shahin aref

execute in bash linux

在 bash linux 中执行

##代码##