0

Im looking to use the rename command to edit file names. More specifically, Im trying to isolate specific pieces to change, or in some cases, the whole name.

For example:

Lets say I have three directories… (test-file-1, example2, third)

If I wanted to change "test-file-1" to "file 1"
I know I could userename 's/test-file-1/file 1/' *

How could I set a wild card so I dont have to give the explicit file name to be changed?

I've tried rename 's/tes*/file 1/' *to no avail

Similarly, I would like to know if it's possible to change the whole file name using wildcards.

I've tried rename 's/^*/file 1/' test*

Im not entirely sure if I can use the second asterisk to match the files to be processed or not but the same question applies to that as well.

    1 Answer 1

    0

    You are not using regular expressions correctly. tes* means te with any number of ss following it, so test-file-1 will be renamed to file 1t-file-1:

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

    Similarly, ^* will match the empty string appearing at the start, so in effect it's like ^, but with a an inifinite loop:

    $ 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 

    You should, instead, use .* - . matches all characters except the newline, usually:

    $ 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 

    Naturally I'd expect this last command to create problems.

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.