Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-29-2009, 10:24 AM   #1
Registered: Feb 2004
Location: by the Bavarian Sea
Distribution: Slackware 14.2
Posts: 64

Rep: Reputation: 18
how to rename files recursively, only keeping last x digits plus extension

Hello everybody,

sorry if this has been answered (directly or indirectly) before, but I'm not really a programmer and after reading a lot of threads and man pages today with still no luck, I guess it's time to ask.

My problem is this:
I have a number of directories, all containing files of different name lenghts, including letters, numbers and possibly spaces. I want to recursively rename all of these files, so that only the _last_ 5 digits (not counting the extension) remain. In other words: I want to cut off all but the last 5 digits and not touch the extension.

I've tried to read up on tr, rename (perl version), sed, cut etc. and browsed through some threads here, but so far couldn't quite figure out how to do it.

If someone could point me to the right (standard) CLI tools and syntax, I'd heavily appreciate it! Thank you!

Last edited by furryspider; 11-29-2009 at 01:03 PM. Reason: set the solved prefix
Old 11-29-2009, 10:46 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Not tested so only to illustrate the method

set -xv  # Comment out after debugging
set -o nounset

find . -type f -print0 | while IFS= read -r -d '' filepath
    case "$fn_noext" in
        *[0-9][0-9][0-9][0-9][0-9] )
            if [[ ! -e "$filepath_new" ]]; then
                echo mv "$filepath" "$filepath_new"  # Remove echo after testing
                echo "New filepath '$filepath_new' already exists. '$filepath' not renamed" >&2
        * )
            echo "'$filepath' does not end in 5 digits before extension; not renamed" >&2
1 members found this post helpful.
Old 11-29-2009, 12:55 PM   #3
Registered: Feb 2004
Location: by the Bavarian Sea
Distribution: Slackware 14.2
Posts: 64

Original Poster
Rep: Reputation: 18
That is quite an impressive illustration, sir! It works like a charm!
Thanks a lot!

I'll have to do some reading before I fully understand your script. Seems it's mainly using pure bash functionality to 'parse' the filenames and put the desired parts back together. I don't think I would have found this solution by myself, so again, thanks for your help!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
rename multiple files with random extension ltodd2 Linux - General 12 03-25-2009 06:56 AM
LXer: Rename multiple files to another extension in Linux LXer Syndicated Linux News 0 08-19-2007 03:10 PM
How to rename multiple files with the same extension rogueeve Linux - Newbie 3 05-18-2007 01:54 AM
Remove files w/ extension txt recursively spiri Linux - General 4 12-14-2005 03:52 AM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:40 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration