[英]one liner in bash (using perl or awk) to change extension of multiple files?
在bash shell中重命名一堆文件的最簡單方法是什么? 要將當前目錄中的每個*.ext
文件重命名為*.otherext
? 我對使用perl或awk的解決方案持開放態度,而不必是純粹的bash。 有任何想法嗎?
要清楚,這意味着:
mv a.ext a.otherext
mv b.ext b.otherext
...
etc. for all *.ext
有幾種方法可以做到這一點。 有一個用Perl編寫的rename
程序:
rename 's/\.ext\z/.otherext/' *.ext
但是,還有另一個(不兼容的) rename
程序,您必須為此執行以下操作:
rename .ext .otherext *.ext
還有一個名為mmv
的程序:
mmv '*.ext' '#1.otherext'
使用普通bash:
for i in *.ext; do mv -- "$i" "${i%.ext}.otherext"; done
使用純Perl:
perl -we 'for my $old (glob "*.ext") { (my $new = $old) =~ s/\.ext\z/.otherext/; rename $old, $new or warn "$old -> $new: $!\n"; }'
由於您詢問的是Python版本的外觀,因此我想為后代添加它。
#!/usr/bin/python
from glob import glob
from os import rename
for f in glob("*.ext"):
rename(f, f[:-3] + "otherext")
單行版本(外觀不太好):
python -c "import glob,os;[os.rename(f, f[:-3] + \"otherext\") for f in glob.glob(\"*.ext\")]"
python單一代碼:
python -c "import shutil, glob; [shutil.move(i,i.replace('.txt','.ext')) for i in glob.glob('*.txt')]"
利用'和'代替轉義字符和替換功能
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.