LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 11-29-2009, 11:24 AM   #1
furryspider
Member
 
Registered: Feb 2004
Location: by the Bavarian Sea
Distribution: Slackware 14.1
Posts: 56

Rep: Reputation: 15
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 02:03 PM. Reason: set the solved prefix
 
Old 11-29-2009, 11:46 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Not tested so only to illustrate the method
Code:
#!/bin/bash

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

find . -type f -print0 | while IFS= read -r -d '' filepath
do
    path="${filepath%/*}"
    fn="${filepath##*/}"
    ext="${fn##*.}"
    fn_noext="${fn%.$ext}"
    case "$fn_noext" in
        *[0-9][0-9][0-9][0-9][0-9] )
            fn_noext_new="${fn_noext:((${#fn_noext}-5))}"
            filepath_new="$path/$fn_noext_new.$ext"
            if [[ ! -e "$filepath_new" ]]; then
                echo mv "$filepath" "$filepath_new"  # Remove echo after testing
            else
                echo "New filepath '$filepath_new' already exists. '$filepath' not renamed" >&2
            fi
            ;;
        * )
            echo "'$filepath' does not end in 5 digits before extension; not renamed" >&2
    esac    
done
 
1 members found this post helpful.
Old 11-29-2009, 01:55 PM   #3
furryspider
Member
 
Registered: Feb 2004
Location: by the Bavarian Sea
Distribution: Slackware 14.1
Posts: 56

Original Poster
Rep: Reputation: 15
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!
 
  


Reply


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 07:56 AM
LXer: Rename multiple files to another extension in Linux LXer Syndicated Linux News 0 08-19-2007 04:10 PM
How to rename multiple files with the same extension rogueeve Linux - Newbie 3 05-18-2007 02:54 AM
Remove files w/ extension txt recursively spiri Linux - General 4 12-14-2005 04:52 AM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 02:16 PM


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

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