使用sed在目录中的文件名中删除特定的文件扩展名
问题描述:
如何在使用sed的目录中的文件名中删除特定的文件扩展名。例如像我有一个目录中的文件,使用sed在目录中的文件名中删除特定的文件扩展名
file1.txt
file2.txt
file3.cpp
然后我想删除与扩展名为.txt的文件的文件扩展名,所以结果是,
file1
file2
file3.cpp
谢谢!
答
您可以使用rename
:
rename 's/\.txt$//' *
这么说的话会从匹配的文件删除.txt
扩展。
答
如果你真的想用SED,这应该工作:
for file in *.txt ; do mv $file `echo $file | sed 's/\(.*\)\.txt/\1/'` ; done
http://superuser.com/questions/252239/how-can-i-batch-rename-files-in-bash和http://stackoverflow.com/questions/602706/batch-renaming-with-bash – zamnuts
如果你已经有一个名为“file1”的文件呢? –