名前変更コマンドの範囲を定義し、ファイル名全体を変更します。

名前変更コマンドの範囲を定義し、ファイル名全体を変更します。

renameこのコマンドを使用してファイル名を編集したいと思います。より具体的に言えば、特定の部分を別々に変更したり、場合によっては完全な名前を変更したいと思います。

たとえば、

3つのディレクトリがあるとしましょう...(test-file-1、example2、third)

「test-file-1」を「file 1」に変更するには
私は使用できることを知っています rename 's/test-file-1/file 1/' *

変更するファイル名を明示的に指定する必要がないように、ワイルドカードをどのように設定しますか?

頑張ったrename 's/tes*/file 1/' * 役に立たない

同様に、ワイルドカードを使用してファイル名全体を変更できるかどうかを知りたいです。

頑張ったrename 's/^*/file 1/' test*

処理中のファイルと一致させるために2番目のアスタリスクを使用できるかどうかはわかりませんが、その質問にも同じ質問が適用されます。

ベストアンサー1

正規表現を正しく使用していません。tes*後ろにはte任意の数の s が付くことを意味するので、名前は次のように変更されます。stest-file-1file 1t-file-1

$ rename -n 's/tes*/file 1/' *
test-file-1 renamed as file 1t-file-1

同様に、^*空の文字列の開始項目が一致するため、事実上似ています^が、無限ループがあります。

$ rename -n 's/^*/file 1/' *  
^* matches null string many times in regex; marked by <-- HERE in m/^* <-- HERE / at (eval 1) line 1.
example2 renamed as file 1example2
^* matches null string many times in regex; marked by <-- HERE in m/^* <-- HERE / at (eval 2) line 1.
test-file-1 renamed as file 1test-file-1
^* matches null string many times in regex; marked by <-- HERE in m/^* <-- HERE / at (eval 3) line 1.
third renamed as file 1third

代わりに、改行を除くすべての文字を一致させるには、.*-を使用する必要があります。通常は次のようになります。.

$ rename -n 's/tes.*/file 1/' *
test-file-1 renamed as file 1

$ rename -n 's/.*/file 1/' *      
example2 renamed as file 1
test-file-1 renamed as file 1
third renamed as file 1

もちろん、最後のコマンドが問題を引き起こすと予想しました。

おすすめ記事