在一个目录中,我有一些*.html文件。我想把它们都重命名为*.txt
我该怎么做呢?我使用bash shell。
在一个目录中,我有一些*.html文件。我想把它们都重命名为*.txt
我该怎么做呢?我使用bash shell。
当前回答
这是我发现的在OSX和Linux上工作的最灵活的解决方案,它也可以很好地与git一起工作!
找到。-name "*.js" -exec bash -c 'mv "$1" "${1%.js}"Tsx ' - '{}' \;
对于git:
找到。-name“* . js - c“git bash -exec mv“1美元${1%。js的”。tsx“-”{}“\”
其他回答
我用。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
~$
有关更好的解决方案(只有bash功能,而不是外部调用),请参阅其他答案之一。
下面的操作会做,但不需要系统有重命名程序(尽管你经常在系统上有这个程序):
for file in *.html; do
mv "$file" "$(basename "$file" .html).txt"
done
编辑:正如评论中指出的那样,这对于没有适当引用的空格的文件名(现在添加到上面)不起作用。当你只在你自己的文件中工作时,你知道文件名中没有空格,这是可以工作的,但是当你写一些可能在以后被重用的东西时,不要跳过适当的引用。
如果使用bash,则不需要使用sed、basename、rename、expr等外部命令。
for file in *.html
do
mv "$file" "${file%.html}.txt"
done
对于Ubuntu用户:
rename 's/\.html$/\.txt/' *.html
重命名当前目录和子目录下所有文件的扩展名,不需要任何其他包(仅使用shell脚本):
在当前目录下创建shell脚本rename.sh,代码如下: #!/bin/bash 在$(查找。- name“* 1美元”);做 Mv "$file" "${file%$1}$2" 完成 通过./rename.sh .old .new运行它。 例如:./rename.sh .html .txt