同様の名前を持つ何千ものファイル名を変更する

同様の名前を持つ何千ものファイル名を変更する

私のファイル名を変更するためのbashスクリプトを作成しています。ファイルは同じフォルダにあり、すべて次のようになります。

1xxx_(文字列)=(文字列)=1234567890

今私が望むのは最後の1234567890だけを残すことです。デフォルトでは、=の最初の発生から2番目の発生までのすべての文字を削除します。

ベストアンサー1

シェルのパラメータ拡張機能を使用できます。

${parameter#word}
${parameter##word}
       Remove matching prefix pattern.  The word is expanded to produce
       a pattern just as in pathname expansion.  If the pattern matches
       the  beginning of the value of parameter, then the result of the
       expansion is the expanded value of parameter with  the  shortest
       matching  pattern  (the ``#'' case) or the longest matching pat‐
       tern (the ``##'' case) deleted.

そのように

for file in *; do echo mv -- "$file" "${file##*=}"; done

echo正しいことをしているような場合は削除してください)。


発生する可能性のある問題の1つは、プレフィックスを削除するとファイル名が一意でなくなる可能性があることです。-nまたは、オプションを使用して--no-clobberこれらのケース名の変更をスキップすることを選択できますmv

for file in *; do mv --no-clobber -- "$file" "${file##*=}"; done

または、-bまたはオプションを使用して--backup別のバックアップを作成します。最も簡単です。

for file in *; do mv --backup=numbered -- "$file" "${file##*=}"; done

これにより、区別サフィックスなどが追加されます.~1~.~2~

おすすめ記事