単一のコマンドまたはスクリプトを使用してUnixで複数のファイルの名前を変更するには? [コピー]

単一のコマンドまたはスクリプトを使用してUnixで複数のファイルの名前を変更するには? [コピー]

次のファイルのリストがあります

aro_tty-mIF-45875564pmo_opt
aro_tty-mIF-45875664pmo_opt
aro_tty-mIF-45875964pmo_opt
aro_tty-mIF-45875514pmo_opt
aro_tty-mIF-45875524pmo_opt

名前を次に変更する必要があります。

aro_tty-mImpFRA-45875564pmo_opt
aro_tty-mImpFRA-45875664pmo_opt
aro_tty-mImpFRA-45875964pmo_opt
aro_tty-mImpFRA-45875514pmo_opt
aro_tty-mImpFRA-45875524pmo_opt

ベストアンサー1

ほとんどの標準シェルは、シェル変数で単純なテキスト置換を実行する方法を提供します。http://tldp.org/LDP/abs/html/parameter-substitution.html説明は次のとおりです。

${var/Pattern/Replacement}

First match of Pattern, within var replaced with Replacement.

したがって、このスクリプトを使用してすべての適切なファイルを繰り返し、各ファイルの名前を変更します。

for file in aro_tty-mIF-*_opt
do
    mv -i "${file}" "${file/-mIF-/-mImpFRA-}"
done

各名前変更操作を確認する機会を提供するために、-iオプションを追加しました。いつものように、広範な名前の変更または削除を実行する前に、すべてのファイルをバックアップする必要があります。

おすすめ記事