How to Rename Multiple Files With Their First 10 Characters?

I am having a problem to rename multiple files by replacing the name by their first 10 characters of their old name. I tried to find the solution in internet but I didn't find the answers.

Example:

Original File Names:

1208605001abAcd.jpg 
1201230111FbcAdee.jpg 
11512345714x611aaa.jpg 

What I want to achieve:

1208605001.jpg 
1201230111.jpg 
1151234571.jpg

You can try:

rename -n 's/(.{10}).*(\.jpg)$/$1$2/' *.jpg

Example:

$ rename -n 's/(.{10}).*(\.jpg)$/$1$2/' *.jpg
11512345714x611aaa.jpg -> 1151234571.jpg
1201230111FbcAdee.jpg -> 1201230111.jpg
1208605001abAcd.jpg -> 1208605001.jpg

The -n option only simulates the command, so that you can verify the changes. Run without it to actually make the changes.

The regex (.{10}).*(\.jpg) consists:

  • .{10} - any 10 characters, in a group (…), followed by
  • .* - any number of any characters followed by
  • \.jpg$ - the extension at the end ($) of the filename, in the second group

The replacement $1$2 is just the first group followed by the second.


You can do with only bash:

for FILE in *.jpg ; do mv "${FILE}" "${FILE:0:10}.jpg" ; done

With a little work you can get file extension and add automagically to the new name.


If you use zsh:

zmv '(*).(*)' '${1:0:10}.$2'

If it's not already done, you may need to first run:

autoload zmv