LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 05-13-2004, 05:18 PM   #1
mattn
LQ Newbie
 
Registered: Dec 2003
Location: New Haven
Distribution: Ubuntu 8.04
Posts: 11

Rep: Reputation: 0
wildcards with the "rename" command


I have a directory of files in the format:

abcdefg_##_1
abcdefg_##_2
abcdefg_##_3

... etc.

Where the pound sign is a random character. I want to rename these files to

abcdefg_1
abcdefg_2
abcdefg_3

and found the "rename" command, but when I can't figure out how to tell the command that the ##'s are wildcard spots. When I type the following:

rename abcdefg_** abcdefg *

nothing happens.

similarly, when I type:

rename abcdefg_?? abcdefg *

nothing happens.

I have consulted the man page for "rename" but it wasn't much help.

Any help would be appreciated!

Thanks!
 
Old 05-13-2004, 05:39 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
rename is a perl based thing, so you need to know some perl to use it, normal wildcards won't work. ( I don't any perl, sorry )
 
Old 05-13-2004, 06:59 PM   #3
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
how about using sed? i think it can do what you want, altho i don't know the command (its naturally a hard to use, but extremely useful editor)
 
Old 05-13-2004, 07:43 PM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Code:
for file in [[:alpha:]][[:alpha:]][[:alpha:]][[:alpha:]][[:alpha:]][[:alpha:]][[:alpha:]]_??_[1-3]; do 
   mv $file ${file:0:8}${file##*_}
done
This is short enough to be entered on one line. If the directory contains only the files you described then the for test could be simpler: for file in *_??_[123]; do mov $file ${file:0:8}${file##*_}; done

The first part of the destination filename, '${file:0:8}', truncates the name to 8 letters. The second part, '${file##*_}', removes all characters preceding the last underscore '_'.

Usually there are several ways to accomplish the same task. One gotcha in bash is when the filenames contain white-space characters. If that is possible, then remember to put the variables inside double quotes: mv "${file}" ...
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
rename file with "mv" not working for folder? Niceman2005 Linux - Newbie 2 03-13-2005 09:34 PM
Tiny Sofa 2.0 - I thought "halt", "reboot" were only root command ?? sorcerer Linux - Distributions 1 08-21-2004 03:28 PM
"Edit and rename swaret conf" or whatever. PROBLEM! Kjetil4455 Slackware 8 04-14-2004 03:00 PM
"segmentation error" when issuing "useradd" command through terminal with RH 9.0 kaihuang Linux - General 0 10-21-2003 11:47 PM


All times are GMT -5. The time now is 12:44 AM.

Main Menu
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