Sorry for the question. I searched the internet a lot, but couldn't find an answer which worked (though there were a lot of suggestions).

So, I want to rename A LOT of files. Writing all file names is not an option. The files are named like this:


the beginning is always the same and also the .IMA The long number is not the same for all files. I have to change the 039 into 036_2, for all files. The rest should stay the same.

I tried all of the following commands (plus several others), but it didn't do anything:

cd my_directory

rename 's/\w039_2\w/\w036_2\w/g' * 

rename ?039_2? ?036_2? ?0_39_2?.IMA

rename 's/?039_2?/?036_2?/' ?039_2?.IMA

rename 's/\w039_2\w/\w036_2\w/' ?039_2?.IMA

Perl is installed at the linux system (but I'm not sure how to call it... because if I open a perl terminal it doesn't do anything).

Can you help me?

for file in * ; do mv $file $(echo $file | sed 's/_039/_036_2') ; done


for file in * ; do _______ ; done

bash will perform shell expansion on * to list every file in the working directory. For each of these files, this command structure will set the variable file to be equal to the file name and run the command between ; do and ; done

mv $file _______

mv is the traditional way to rename files in unix/linux. It will rename file to _______

echo $file | sed 's/_039/_036_2'

echo $file | sed will simply send "file" to the stdin of sed, where sed will substitute "_039" for "_036_2"

Altogether: for every file in the current directory, rename the file, substituting "_039" for "_036_2"

  • Or mv "$file" "${file/_039/_036_2}" – Joseph R. Sep 13 '14 at 9:20

Your regexp is messed up, here is one that should work.

rename 's/HAUPT_SIA_039[.]\(.*\)/HAUPT_SIA_039_2.\1/g' HAUPT_SIA*.IMA

You are using the correct tool, no need for find unless you have sub-directories.

Note about regexp in sed and rename.

The format is sXsearch_patternXreplace_patternXoptions (X can be any character). searchPattern is what you are looking to replace, there for what already exists, not what you are replacing with.

-w is a single word, and can only be in search_pattern.

  • Thank you all for your answers. Unfortunately I still have problems (as you see, I don't know shell, but I have to do the renaming for another task which I will do after that). So, when I try your suggestion it doesn't do anything. The 'rename' does exist. I checked with 'info rename'. – nooooob Jul 22 '14 at 11:02
  • check less $(which rename) it should be written by Robin Barker, Larry Wall's – ctrl-alt-delor Jul 22 '14 at 12:33

find . -name "*039*" | rename 's/039/036_2/g'

  • Thank you! I tried it, but the shell says: "Aufrug: rename von nach Dateien..." and doesn't do anything. I think the stuff the shell sais can be translated as "Call: rename from to data" – nooooob Jul 22 '14 at 11:46

Loop all files with the "HAUPT_..." prefix, then rename using shell substring ${f:15} returns the string from the 15th char of the file name, appended to the HAUPT_... prefix.

for f in "HAUPT_SIA_039*" ; do  mv $f "HAUPT_SIA_036_2${f:15}"; done

EDIT - It appears that the directory with all the files also contains sub-directories with the same HAUPT... prefix. In that case use the find command to first filter only the files and not directories.

find . -type f -name "HAUPT_SIA_039*" | while read f; do mv $f "HAUPT_SIA_036_2${f:15}"; done
  • Thank you! Unfortunately the shell complaints that "HAUPT_SIA_036_2" is no directory. Should I move the files in a new directory and delete the old one? – nooooob Jul 22 '14 at 11:48
  • Yes, that would be one way to fix the problem (as long as it's just the files that are moved), or see my edit. – suspectus Jul 22 '14 at 22:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.