Removing 10 Characters of Filename in Linux Removing 10 Characters of Filename in Linux shell shell

Removing 10 Characters of Filename in Linux


Try using the rename command. It allows you to rename files based on a regular expression:

The following line should work out for you:

rename 's/_\d+(\.[a-z0-9A-Z]+)$/$1/' *

The following changes will occur:

aarondyne_kh2_13thstruggle_or_1250556383.mus renamed as aarondyne_kh2_13thstruggle_or.musaarondyne_kh2_darknessofunknow_1250556659.mp3 renamed as aarondyne_kh2_darknessofunknow.mp3

You can check the actions rename will do via specifying the -n flag, like this:

rename -n 's/_\d+(\.[a-z0-9A-Z]+)$/$1/' *

For more information on how to use rename simply open the manpage via: man rename


Not the prettiest, but very simple:

echo "$filename" | sed -e 's!\(.*\)...........\(\.[^.]*\)!\1\2!'

You'll still need to write the rest of the script, but it's pretty simple.


find . -type f -exec sh -c 'mv {} `echo -n {} | sed  -E -e "s/[^/]{10}(\\.[^\\.]+)?$/\\1/"`' ";"