Linux 使用 shell 脚本更改文件扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9590935/
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-06 05:01:05  来源:igfitidea点击:

Change extension of file using shell script

linuxshellunix

提问by Mainak

How to change extension of all *.dat files in a directory to *.txt. Shell script should take the directory name as an argument. Can take multiple directories as arguments. Print the log of command result in appending mode with date and timestamp.

如何将目录中所有 *.dat 文件的扩展名更改为 *.txt。Shell 脚本应将目录名称作为参数。可以将多个目录作为参数。以带有日期和时间戳的附加模式打印命令结果的日志。

回答by Pben

Batch File Rename By File Extension in Unix

在 Unix 中按文件扩展名批量重命名文件

# change .htm files to .html
for file in *.htm ; do mv $file `echo $file | sed 's/\(.*\.\)htm/html/'` ; done

# change .html files to .htm
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/htm/'` ; done

#change .html files to .shtml
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/shtml/'` ; done

#change .html files to php
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/php/'` ; done

so ==>

所以==>

# change .dat files to .txt
for file in *.dat ; do mv $file `echo $file | sed 's/\(.*\.\)dat /txt/'` ; done

回答by Alex

#!/bin/bash
for d in $*; do
    for f in $(ls $d/*.dat); do
        echo $(date) $(mv -v $f ${f%.dat}.txt)
    done
done

Output redirection should be done by the shell when running the script

运行脚本时,输出重定向应该由 shell 完成

Leaving out argument validity checks

省略参数有效性检查

回答by mtk

Simple script:

简单的脚本:

#!/bin/bash

if [ $# -lt 2 ] then
    echo "Usage `basename 
#remove the space in file name
#example file name:19-014-0100.mp3 .mp3
#result file name:19-014-0100.mp3
$ for file in *.mp3 ; 
    do target=`echo "$file" | sed 's/ //g'`;
    echo "$target"; 
    mv "$file" "$target"; 
done;

#remove the duplicate file extension in file name
#example file name:19-014-0100.mp3.mp3
#result file name:19-014-0100.mp3
$ for file in *.mp3 ; 
    do target=`echo "$file" | sed 's/\.mp3\.mp3$/.mp3/g'`;
    echo "$target"; 
    mv "$file" "$target"; 
done;
` <any number of directories space separated>" exit 85 # exit status for wrong number of arguments. fi for directories do for files in $(ls $directories/*.dat); do echo $(date) $(mv -v $files ${files%.dat}.txt) done done

The first for loop by default loops on the $@i.e. command-line arguments passed.

默认情况下,第一个 for 循环在$@传递的ie 命令行参数上循环。

回答by Amitabha

Follow Pben's solution, if your filename contains blank space, you should use double quotation marks to the variable like the following:

按照Pben的解决方案,如果您的文件名包含空格,您应该对变量使用双引号,如下所示:

#!/bin/sh
if [ $# -ne 3 ] 
then
    echo "Usage:  ./script folder current_extension modify_extension"
    exit
fi
mkdir .temp
find  -name "*." > .temp/output_1 && sed "s///" .temp/output_1 > .temp/output_2 && sed -e "s/[ \t]/\\ /g" .temp/output_2 > .temp/output_3
while read line
do
    mv -v "$line""" "$line"""
done < .temp/output_3
rm -rf .temp

回答by Imran_Sri Lanka

Script, first finds the names of the given extensions. It removes the extension from names. Then adds backslash() for identification of terminal.

脚本,首先找到给定扩展的名称。它从名称中删除扩展名。然后添加反斜杠()用于识别终端。

Then the 'mv' command executed. Here the '.temp' folder is used to hide the process from user, in GUI.

然后执行'mv'命令。这里的“.temp”文件夹用于在 GUI 中对用户隐藏进程。

csvname=`echo $xlsx |sed 's/\.xlsx//'`"-$now"`echo $xlsx | sed 's/\(.*\.\)xlsx/\.csv/'`

The output files are saved inside the '.temp' folder,later the '.temp' folder is removed.

输出文件保存在“.temp”文件夹中,稍后删除“.temp”文件夹。

回答by William Cates

The top voted answer didn't really work for me. I may have been doing something wrong. My scenario was trying to create a file with the original name, but with the date appended to it, along with changing the extension from .xslx to .csv. This is what worked for me:

投票最高的答案对我来说并没有真正的效果。我可能做错了什么。我的方案是尝试使用原始名称创建一个文件,但附加了日期,并将扩展名从 .xslx 更改为 .csv。这对我有用:

for i in *.dat
do mv $i `echo $i |sed 's/\.dat//'``echo $i | sed 's/\(.*\.\)dat/\.txt/'`
done

So, for all the .dat files in a directory (without the date addition), you could run something like this:

因此,对于目录中的所有 .dat 文件(不添加日期),您可以运行以下内容:

echo $i |sed 's/\.dat//'

From the above, this section of code just removed the extension:

从上面来看,这部分代码只是删除了扩展名:

echo $i | sed 's/\(.*\.\)dat/\.txt/'

And this section changes the .dat to .txt:

这部分将 .dat 更改为 .txt:

mv [filename][.dat] [filename] + [.txt]

And by bumping them next to each other, it concatenates the two outputs into the filename. It's like doing this:

通过将它们彼此相邻,它将两个输出连接到文件名中。就像这样做:

find . -depth -name "*.c" -exec sh -c 'dname=$(dirname {}) && fname=$(basename {} .c) && mv {} $dname/$fname.h' ";"

Though, I did use STDOUT instead of the 'mv' command.

不过,我确实使用了 STDOUT 而不是 'mv' 命令。

回答by vinoth kumar

Following command to change file extention .cto .h

以下命令将文件扩展名更改.c.h

find . -name "*.html*" -exec rename -v 's/\.html$/\.epub/i' {} \;

回答by eugene

To rename (changing extention) all my html files on epub files I use this command line :

要重命名(更改扩展名)我在 epub 文件上的所有 html 文件,我使用以下命令行:

for file in /*.dat ; do mv "$file" "${file%.*}.txt" ; done

回答by Aki Korhonen

Bash can do all of the heavy lifting such as extracting the extension and tagging on a new one. For example:

Bash 可以完成所有繁重的工作,例如提取扩展名和标记新的扩展名。例如:

##代码##