8

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 4

17

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
  • Sorry if I am missing something, when I tried to run your suggested command I've got this message : "Substitution replacement not terminated at (eval 1) line 1.". Did I do something wrong? May 27, 2016 at 8:07
  • @surya_darmana there was supposed to be a / after $2. The example output has it correct. I have fixed it.
    – muru
    May 27, 2016 at 8:10
  • Thank you very much, the command is run perfectly, you made my day :D May 27, 2016 at 8:13
  • 1
    This holds for the rename that comes with perl (e.g. on Ubuntu), not the one that comes with util-linux (e.g. on Fedora, CentOS, Cygwin). May 27, 2016 at 18:41
5

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
  • 4
    Use globbing (*) instead of ls (for which you wouldn't need the -1 anyway), and quote the variables. ("${FILE:0:10}.jpg").
    – Kevin
    May 27, 2016 at 16:40
  • I prefer using ls, but +1 for the quoting! (error of distraction).
    – Antonio
    May 28, 2016 at 12:35
  • It isn't a matter of preference, using ls will break on files with spaces in the name. Globbing won't.
    – Kevin
    May 28, 2016 at 16:30
  • Great, @Kevin! I never thought about IFS. I'm "old", I'm yet naming my files with underscore instead of spaces. :-) I'll edit my answer.
    – Antonio
    May 28, 2016 at 21:15
2

If you use zsh:

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

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

autoload zmv
2
  • Will this keep the extensions?
    – muru
    May 27, 2016 at 16:59
  • @muru no, I didn't notice that particular requirement. Updated with a version that does.
    – Kevin
    May 27, 2016 at 17:01
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .