在一个目录中,我有一些*.html文件。我想把它们都重命名为*.txt
我该怎么做呢?我使用bash shell。
在一个目录中,我有一些*.html文件。我想把它们都重命名为*.txt
我该怎么做呢?我使用bash shell。
当前回答
最简单的方法是使用重命名。ul它出现在大多数Linux发行版中
重命名。ul -o -v [oldFileExtension] [newFileExtension][搜索要应用的文件的表达式]
rename.ul -o -v .oldext .newext *.oldext
选项:
-o:不覆盖预先存在的. newnext
- v:详细
-n:演练
其他回答
对于Ubuntu用户:
rename 's/\.html$/\.txt/' *.html
最简单的方法是使用重命名。ul它出现在大多数Linux发行版中
重命名。ul -o -v [oldFileExtension] [newFileExtension][搜索要应用的文件的表达式]
rename.ul -o -v .oldext .newext *.oldext
选项:
-o:不覆盖预先存在的. newnext
- v:详细
-n:演练
你也可以在Bash中创建一个函数,将它添加到。bashrc或其他文件中,然后在任何你想使用的地方使用它。
change-ext() {
for file in *.$1; do mv "$file" "$(basename "$file" .$1).$2"; done
}
用法:
change-ext css scss
源代码的功能:https://stackoverflow.com/a/1224786/6732111
我用。bashrc写了这段代码
alias find-ext='read -p "Path (dot for current): " p_path; read -p "Ext (unpunctured): " p_ext1; find $p_path -type f -name "*."$p_ext1'
alias rename-ext='read -p "Path (dot for current): " p_path; read -p "Ext (unpunctured): " p_ext1; read -p "Change by ext. (unpunctured): " p_ext2; echo -en "\nFound files:\n"; find $p_path -type f -name "*.$p_ext1"; find $p_path -type f -name "*.$p_ext1" -exec sh -c '\''mv "$1" "${1%.'\''$p_ext1'\''}.'\''$p_ext2'\''" '\'' _ {} \;; echo -en "\nChanged Files:\n"; find $p_path -type f -name "*.$p_ext2";'
在"/home/<user>/example-files"这样的文件夹中:
/home/<用户> /示例文件: 中 file2.txt file3.pdf file4.csv
命令的行为是这样的:
~$ find-text
Path (dot for current): example-files/
Ext (unpunctured): txt
example-files/file1.txt
example-files/file2.txt
~$ rename-text
Path (dot for current): ./example-files
Ext (unpunctured): txt
Change by ext. (unpunctured): mp3
Found files:
./example-files/file1.txt
./example-files/file1.txt
Changed Files:
./example-files/file1.mp3
./example-files/file1.mp3
~$
这为我工作在OSX从.txt到.txt_bak
find . -name '*.txt' -exec sh -c 'mv "$0" "${0%.txt}.txt_bak"' {} \;