6 Answers
6

On Debian, Ubuntu and derivatives, if you enter man rename at a command prompt, you'll get the manual page for a rename utility that allows arbitrary Perl-like regular expression commands to be used in the renaming.

For example, this will shorten (by truncating) all files in the current directory to a length of five:

rename 's/^(.{5}).*/$1/' *

It works by capturing the first five characters then using that in the substitution, removing the rest.

Another example is to shorten filenames (sans extension) to five characters wile preserving the extension:

rename 's/^(.{5}).*(\..*)$/$1$2/' *

It captures two groups, the first being the first five character, the second being the extension. This would turn myCarefullyCraftedDocument.pdf into myCar.pdf.

Warning: Use this carefully, preferably on a copy of your directory, or by taking backups first. You have been warned!! At a bare minimum, use rename -n first, which will show you what would be done without actually doing it.

will this get rid of the prefix as in PDF? and what would happen to files with the same name once shorten?
–
GrimlockzFeb 29 '12 at 13:01

That particular example will, yes. You can write a regex to leave the extension alone. I'll update the answer. It also protects against overwriting files that already exist, so later renames will fail.
–
paxdiabloFeb 29 '12 at 13:04

thanks for your help on this - I'm very thankful. I do have one slight issue if some of the pdfs will be renamed to 5 lets say what would happen to the PDF's it tries to rename with the same name? Is they a way we can just add a unique number onto all the ones with this is? see below ename -n 's/^(.{5}).*(\..*)$/$1$2/' * Annexes 123114345234525.pdf renamed as Annex.pdf Annexes 123114432452352.pdf renamed as Annex.pdf
–
GrimlockzFeb 29 '12 at 13:14

@Grimlockz, I don't think you can put that sort of intelligence into the rename regex itself. What you may need to do is write a script to wrap rename with that sort of intelligence, and that's better asked as a separate question so you'll get more people responding than just I.
–
paxdiabloFeb 29 '12 at 13:19

@Grimlockz This is why you don't want to do everything in a regex. You'll be happier and better off writing a script that uses regexes.
–
Spencer RathbunFeb 29 '12 at 13:26

If you need to recurse into subdirectories, call a shell script from find -depth -exec …. Or use zsh instead of bash; in zsh, **/*(od) expands to all the files under the current directory, recursively, with more deeply nested files first.

Shell/python/perl scripting for this kind of thing is just fine, but it seems like a gui app would be best for you, so I would recommend pyRenamer, which can do exactly what you want, among many other things (simple common operations via presets [getting rid of spaces, truncating filename from one point to another], simple search/replace, complicated regex, previewer, etc).

Your distribution might even have it in their software repository (Fedora does).

The -b flag on move creates a backup of files that would be deleted or overwritten. By changing the glob pattern in the for loop you can adjust what files it runs on, using * by itself to run on all files.

If you need to experiment with renaming files and happen to have KDE libraries installed on your system (or don't mind adding them), check out krename.

It allows you to do quite a few simple renaming tasks directly with its tools and also allows regular expressions for more complicated situations.

It shows you a preview of the renamed files as you build the renaming conditions. It also allows you to rename the same files again which is helpful if you want to do something complicated where doing it in stages would reduce the complexity of each stage to something you can figure out quickly.