在一个目录中,我有一些*.html文件。我想把它们都重命名为*.txt
我该怎么做呢?我使用bash shell。
在一个目录中,我有一些*.html文件。我想把它们都重命名为*.txt
我该怎么做呢?我使用bash shell。
当前回答
这为我工作在OSX从.txt到.txt_bak
find . -name '*.txt' -exec sh -c 'mv "$0" "${0%.txt}.txt_bak"' {} \;
其他回答
在Linux或window git bash或window的wsl中,尝试以下命令,只需一行代码就可以更改当前目录或子目录甚至其子目录中的每个文件的扩展名
find . -depth -name "*.html" -exec sh -c 'mv "$1" "${1%.html}.txt"' _ {} \;
很好很简单!
find . -iname *.html -exec mv {} "$(basename {} .html).text" \;
试试这个
rename .html .txt *.html
用法:
rename [find] [replace_with] [criteria]
命令mmv似乎对大量文件(每秒数万个)非常有效地完成这项任务。例如,要将所有.xml文件重命名为.html文件,使用以下命令:
mmv ";*.xml" "#1#2.html"
的;将匹配路径,*将匹配文件名,这些在替换名称中被称为#1和#2。
基于exec或管道的答案要么太慢,要么在大量文件上失败。
这里有一个解决方案,使用AWK。确保文件存在于工作目录中。否则,cd到html文件所在的目录,然后执行以下命令:
for i in $(ls | grep .html); do j=$(echo $i | grep -oh "^\w*." | awk '{print $1"txt"}'); mv $i $j; done