简体   繁体   English

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

[英]Change extension of file using shell script

How to change extension of all *.dat files in a directory to *.txt.如何将目录中所有 *.dat 文件的扩展名更改为 *.txt。 Shell script should take the directory name as an argument. Shell 脚本应将目录名称作为参数。 Can take multiple directories as arguments.可以取多个目录为 arguments。 Print the log of command result in appending mode with date and timestamp.使用日期和时间戳以附加模式打印命令结果的日志。

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/\1html/'` ; done

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

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

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

so ==> 所以==>

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

Bash can do all of the heavy lifting such as extracting the extension and tagging on a new one. Bash可以完成所有繁重的工作,例如提取扩展和标记新的扩展。 For example: 例如:

for file in $1/*.dat ; do mv "$file" "${file%.*}.txt" ; done
#!/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 退出参数有效性检查

Simple script: 简单的脚本:

#!/bin/bash

if [ $# -lt 2 ] then
    echo "Usage `basename $0` <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 $@ ie command-line arguments passed. 传递的$@ ie命令行参数默认循环第一个for循环。

Follow Pben's solution, if your filename contains blank space, you should use double quotation marks to the variable like the following: 按照Pben的解决方案,如果你的文件名包含空格,你应该对变量使用双引号,如下所示:

#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;

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. 然后执行'mv'命令。 Here the '.temp' folder is used to hide the process from user, in GUI. 这里'.temp'文件夹用于在GUI中隐藏用户进程。

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

The output files are saved inside the '.temp' folder,later the '.temp' folder is removed. 输出文件保存在'.temp'文件夹中,稍后删除'.temp'文件夹。

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. 我的方案是尝试创建一个具有原始名称的文件,但附加了日期,同时将扩展名从.xslx更改为.csv。 This is what worked for me: 这对我有用:

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

So, for all the .dat files in a directory (without the date addition), you could run something like this: 因此,对于目录中的所有.dat文件(没有添加日期),您可以运行以下内容:

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

From the above, this section of code just removed the extension: 从上面可以看出,这段代码只删除了扩展名:

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

And this section changes the .dat to .txt: 此部分将.dat更改为.txt:

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

And by bumping them next to each other, it concatenates the two outputs into the filename. 通过将它们彼此相邻碰撞,它将两个输出连接成文件名。 It's like doing this: 就像这样做:

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

Though, I did use STDOUT instead of the 'mv' command. 虽然,我确实使用STDOUT而不是'mv'命令。

按照命令将文件扩展名.c更改为.h

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

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

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

change js to cjs extension files recursively:将 js 递归地更改为 cjs 扩展文件:

cd dist # where you place your .js
for file in $(find . -type f -name "*.js"); do mv "$file" "${file%.*}.cjs"; done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM