how to rename multiple files by replacing string in file name? this string contains a "#"

  • https://serverfault.com/questions/70939/how-to-replace-a-text-string-in-multiple-files-in-linux

    https://serverfault.com/questions/228733/how-to-rename-multiple-files-by-replacing-word-in-file-name

    https://serverfault.com/questions/212153/replace-string-in-files-with-certain-file-extension

    https://serverfault.com/questions/33158/searching-a-number-of-files-for-a-string-in-linux

    These mentioned articles have all answered my question. However none of them work for me. I suspect it is because the string I am trying to replace has a # in it. Is there a special way to address this?

    I have image file that had an é replaced by #U00a9 during a site migration. These look like this:

    Lucky-#U00a9NBC-80x60.jpg
    Lucky-#U00a9NBC-125x125.jpg
    Lucky-#U00a9NBC-150x150.jpg
    Lucky-#U00a9NBC-250x250.jpg
    Lucky-#U00a9NBC-282x232.jpg
    Lucky-#U00a9NBC-300x150.jpg
    Lucky-#U00a9NBC-300x200.jpg
    Lucky-#U00a9NBC-300x250.jpg
    Lucky-#U00a9NBC-360x240.jpg
    Lucky-#U00a9NBC-400x250.jpg
    Lucky-#U00a9NBC-430x270.jpg
    Lucky-#U00a9NBC-480x240.jpg
    Lucky-#U00a9NBC-600x240.jpg
    Lucky-#U00a9NBC-600x250.jpg
    Lucky-#U00a9NBC.jpg
    

    and I want to change it to something like this:

    Lucky-safeNBC-80x60.jpg
    Lucky-safeNBC-125x125.jpg
    Lucky-safeNBC-150x150.jpg
    Lucky-safeNBC-250x250.jpg
    Lucky-safeNBC-282x232.jpg
    Lucky-safeNBC-300x150.jpg
    Lucky-safeNBC-300x200.jpg
    Lucky-safeNBC-300x250.jpg
    Lucky-safeNBC-360x240.jpg
    Lucky-safeNBC-400x250.jpg
    Lucky-safeNBC-430x270.jpg
    Lucky-safeNBC-480x240.jpg
    Lucky-safeNBC-600x240.jpg
    Lucky-safeNBC-600x250.jpg
    Lucky-safeNBC.jpg
    

    UPDATE:

    These examples all start with "LU00a9ucky but here are many images with different names. I am simply targeting the "#U00a9" portion of the string to replace with "safe".

    So what have you actually tried? I see that you have linked to a few questions and say they failed, but how did they fail? IMO the best example uses the `rename` command. I suspect your rename would be as simple as `rename -n 's/#/safeNBC/' *.jpg`.

    I tried `rename -n 's/#U00a9/safe/' *.jpg` and the command was accepted but no changes occurred.

    Sure, as you would have seen from the documentation you surely reviewed, the `-n` is the `no act` option. Which lets you see if it works before you actually use it. Did the output on the screen show the potential new names correctly?

    I apologize I copied and pasted your example without paying full attention, I did the rename command without the -n. I believe @DTK address the problem, I was not escaping the #.

    For arch (rename from utils-linux 2.34, check using rename -V) you can do: `rename expression replacment file` e.g. to replace the string foo with bar for all files you do `rename 'foo' 'bar' *`

  • Correct answer

    6 years ago

    This is not hard, simply make sure to escape the octothorpe (#) in the name by prepending a reverse-slash (\).

    find . -type f -name 'Lucky-*' | while read FILE ; do
        newfile="$(echo ${FILE} |sed -e 's/\\#U00a9/safe/')" ;
        mv "${FILE}" "${newfile}" ;
    done 
    

    Your explanation makes sense, escaping the `#` sounds like what I need. I do not see a backslash in your example. Should it look like this: `s/\#U00a9/safe/`

    @LeonFrancisShelhamer good catch. It swallowed the backslash. I'll modify.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM