Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-12-2012
Registered User
 
Join Date: Jul 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Regular Expression Question

Hello,

I'm trying to rename a bunch of files that were named incorrectly. I know a little about regular expressions but I'm not very good at them.
Here is the image of the file names:



I'm trying to change the 20111116 at the beginning to 20101116 for all the files.
Also I want the format of the other files that do not contain the IMG_#### to be of the format YYYYMMDD Race #- name.jpg They are all correct in this folder but the other folders are different.

The command I used was

Code:
find . -name **20111116*.jpg | sed 's/\(^.*.*\)20111116\(.*.jpg\)$/mv "&" "\120101116\2"/' | sh

It worked for the first three files as seen in the picture however i ended up getting a find: 20111116 Race 1- Susumna 2.jpg: unknown option error.

Is there any experts on regular expressions that could lend me a hand?

Thank you!

Last edited by nastyn8; 07-12-2012 at 01:34 PM..
Sponsored Links
    #2  
Old 07-12-2012
alister alister is offline Forum Advisor  
Registered User
 
Join Date: Dec 2009
Posts: 2,600
Thanks: 123
Thanked 717 Times in 600 Posts
You need to quote the pattern argument to -name, otherwise the shell expands it to many filenames if there are matches in the current directory. This garbles the find command since it expects a single word after -name.

In the directory listing in your image, the extension case varies; some are .jpg and some are .JPG. Neither your find command (what I see of it) nor your sed will handle the uppercase variant.

While there's nothing at all wrong with piping sed output into sh, it can be unreliable if there are odd filenames in the input. For example, a leading dash would probably confuse mv, so it's a good idea to use -- to disable option processing. Further, a double-quote in a filename, which is allowed and not unheard off in song/movie/image names, would unbalance the quotes and break the sh script.

If possible, I try to avoid that idiom (unless I am confident that the data is sane).

The following is a different approach to renaming all jpg/JPG files that begin with 20111116 in a tree rooted at the current directory (test on copies or backup your data before running, or change the mv command to echo until you are confident it will work correctly):


Code:
find . -type f -name '20111116*.[jJ][pP][gG]' -exec sh -c '
    for file; do
        dir=${file%/*} base=${file##*/}
        base=2010${base#2011}
        mv -- "$file" "$dir/$base"
    done
' sh {} +

Regards and welcome to the forum,
Alister

Last edited by alister; 07-12-2012 at 01:44 PM..
The Following User Says Thank You to alister For This Useful Post:
nastyn8 (07-12-2012)
Sponsored Links
    #3  
Old 07-12-2012
Registered User
 
Join Date: Jul 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
That worked perfectly. Thank you so much you are a life saver!

---------- Post updated at 01:33 PM ---------- Previous update was at 12:56 PM ----------

Sorry I have another quick question. If a file name is "20111129 Race 2_CC Dancer.jpg" for example and I wanted it to be "20101129 Race 2- CC Dancer". Instead of the underscore I need a dash and a space. Is there something I could add to this code? Or would I need to use a completely new code?

Thanks again
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: How to read from a file, do regular expression and then replace the found regular expression jessy83 Programming 1 12-05-2011 03:10 PM
a question about a regular expression ksgreen UNIX for Dummies Questions & Answers 4 11-16-2010 04:32 PM
Question on Regular Expression Katkota UNIX for Dummies Questions & Answers 14 05-18-2008 05:11 PM
Regular Expression question Katkota UNIX for Dummies Questions & Answers 6 05-14-2008 03:23 PM
Regular expression question umen Shell Programming and Scripting 7 11-21-2007 08:45 PM



All times are GMT -4. The time now is 09:28 AM.