2017-02-07 6 views
-1

一連のファイルからファイル名の2番目の部分を削除します。私はすべてをたい私は次のようになり、ファイルのセットを持つ(約100)一連のフォルダ持つフォルダ

Folder 1: 
species_2136.dbf 
species_2136.lyr 
species_2136.prj 
species_2136.sbn 
species_2136.sbx 
species_2136.shp 
species_2136.shp.xml 
species_2136.shx 

Folder 2: 
species_136524.dbf 
species_136524.lyr 
species_136524.prj 
species_136524.sbn 
species_136524.sbx 
species_136524.shp 
species_136524.shp.xml 
species_136524.shx 

を名前はspecies.extとなります。どのようにすべてのフォルダ内のすべてのファイルからこのように見えるように_####を削除できますか? Perlの名前の変更(スタンドアロンコマンド)で

Folder 1: 
species.dbf 
species.lyr 
species.prj 
species.sbn 
species.sbx 
species.shp 
species.shp.xml 
species.shx 

Folder 2: 
species.dbf 
species.lyr 
species.prj 
species.sbn 
species.sbx 
species.shp 
species.shp.xml 
species.shx 

答えて

1

for file in ./{folder1,folder2}/* 
do 
    mv "$file" "${file%_*}"."${file#*.}" 
done 

(または)単一

for file in ./{folder1,folder2}/*; do mv "$file" "${file%_*}"."${file#*.}"; done 

ループとしても行うことができます

としてライン、

for file in ./folder1/* ./folder2/*; do mv "$file" "${file%_*}"."${file#*.}"; done 
1

rename -n 's/_[0-9]+//' "Folder "*/species* 

すべてが正常に見える場合は、オプション-nを削除します。 bash parameter expansion

0

あなたのファイル名に

species_2136.dbf 
species_2136.lyr 
species_2136.prj 
species_2136.sbn 
species_2136.sbx 
species_2136.shp 
species_2136.shp.xml 
species_2136.shx 

renameコマンドで非常に簡単で簡単です。まず、あなたのフォルダに移動し、この試してみてください。ここでは
rename -n 's/_.*?\./\./'

-nはノーアクション用で、ちょうどあなたが
トリッキーな部分への出力を示し、この正規表現です:_.*?\.、それは_から.にすべてを一致させます一度。それらを単一のドット.で置き換えてください。

証明

$ cat your-list-of-file | rename -n 's/_.*?\./\./' 
rename(species_2136.dbf, species.dbf) 
rename(species_2136.lyr, species.lyr)  
rename(species_2136.prj, species.prj) 
rename(species_2136.sbn, species.sbn) 
rename(species_2136.sbx, species.sbx) 
rename(species_2136.shp, species.shp) 
rename(species_2136.shp.xml, species.shp.xml) 
rename(species_2136.shx, species.shx) 
関連する問題