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

4 Answers

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.

4

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.

4

If you use zsh:

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

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

autoload zmv
2

If you want a GUI version, take a look at pyRenamer, a powerful bulk-renaming tool.

Logo for pyRenamer

pyRenamer has two great advantages. One is that it allows you do a "what if", in other words to see what would happen before you commit to the change.

The other advantage is explained nicely in its description:

You can rename files using patterns, search and replace, substitutions, insert or delete text, or even rename files manually.

You can also rename images using their EXIF tags and music using their internal tags.

To install, use Ubuntu Software Centre or your favourite software installer, or use the command line issue the following command:

sudo apt install pyrenamer

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like