LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-27-2009, 11:17 AM   #1
dkrysak
LQ Newbie
 
Registered: Sep 2004
Distribution: centos
Posts: 25

Rep: Reputation: 15
rename command and wild cards


Hi there, I am using hte rename command to clean up a bunch of files. I know you should be able to use regex with the rename command, but it seems to ignore them.

Most of the time I am using something like:

rename 'something' 'some' *

Now lets say I have files named:

someFile(2009).ext
otherFile(2008).ext
File(2006).ext
newFile(somthing).ext

How could I strip away anything in brackets?
To me something like this would make sense:

rename '(*)' '' *.ext

I just mostly want to know if there is a way to use a wild card...

Thanks in advance.

D
 
Old 08-27-2009, 12:34 PM   #2
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
Some body correct me if I'm wrong, but are parentheses considered valid characters in the context of a filename? Perhaps you could give us a better example of the before and after results you are trying to accomplish.

Regards,

Fordeck
 
Old 08-27-2009, 12:54 PM   #3
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS, Raspian
Posts: 1,137

Rep: Reputation: 65
Quote:
Originally Posted by fordeck View Post
Some body correct me if I'm wrong, but are parentheses considered valid characters in the context of a filename?
When in doubt, escape.

Let's say you're collecting some data from your system. You could, I suppose, create a file named:

vmstat 10 8640 > statistics(20090827).data

and if you want to be sure the shell doesn't do something unexpected with parentheses in the filename you could modify that command to be:

vmstat 10 8640 > statistics\(20090827\).data

Call me lazy but, personally, I think you're better off not creating filenames with special characters so you don't have to remember to escape them when manipulating the files. That goes for spaces in filenames, too. (Remember, the underscore is your friend.) Otherwise you end up having to do more work figuring out how to use those files.

--
RT
 
Old 08-27-2009, 01:09 PM   #4
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,252

Rep: Reputation: 205Reputation: 205Reputation: 205
Command rename traits characters in from and to strings as-is. So * is asterisk not any string of characters. Compare these three examples:

$ touch left\*right
$ ls
Code:
left*right
$ rename "*" "" *
$ ls
Code:
leftright
$ rename "t" "*" *
$ ls
Code:
lef*right
To rename your files you have to use more sophisticated method:

$ ls
Code:
File(2006).ext  newFile(somthing).ext  otherFile(2008).ext  someFile(2009).ext
$ for file in *.ext ; do mv $file `echo $file | perl -pe "s/\(.*\)//"` ; done
$ ls
Code:
File.ext  newFile.ext  otherFile.ext  someFile.ext

Last edited by w1k0; 08-27-2009 at 01:21 PM.
 
Old 08-27-2009, 01:15 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Why summon perl if sed does the same thing?
;}

Code:
sed 's/\(.*\)//'
 
Old 08-27-2009, 01:20 PM   #6
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,252

Rep: Reputation: 205Reputation: 205Reputation: 205
For the best compatibility it's good to use only those characters in file's names:

A-Z
a-z
0-9
_
-
+
.

That character is hard coded in Linux kernel so it's impossible to use it in file's names:

/

Some characters are acceptable in Linux but impossible to use in Windows:

*
?
\
|
:
"

I use in my MP3 collection file's names these additional characters:

space
!
#
%
&
'
(
)
,
;
=
[
]
 
Old 08-27-2009, 01:24 PM   #7
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,252

Rep: Reputation: 205Reputation: 205Reputation: 205
Quote:
Originally Posted by Tinkster View Post
Why summon perl if sed does the same thing?
;}

Code:
sed 's/\(.*\)//'
Not in my case:

$ ls
Code:
File(2006).ext  newFile(somthing).ext  otherFile(2008).ext  someFile(2009).ext
$ for file in *.ext ; do mv $file `echo $file | sed "s/\(.*\)//"` ; done
Code:
mv: missing destination file operand after `File(2006).ext'
Try `mv --help' for more information.
mv: missing destination file operand after `newFile(somthing).ext'
Try `mv --help' for more information.
mv: missing destination file operand after `otherFile(2008).ext'
Try `mv --help' for more information.
mv: missing destination file operand after `someFile(2009).ext'
Try `mv --help' for more information.
$ ls
Code:
File(2006).ext  newFile(somthing).ext  otherFile(2008).ext  someFile(2009).ext
 
Old 08-27-2009, 01:37 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
You're right ... I was mixing sed's syntax for regex and
extended regex. It's even simpler.
Code:
$ touch File\(2006\).ext  newFile\(somthing\).ext  otherFile\(2008\).ext  someFile\(2009\).ext$ ls -ltr                                                    total 0
-rw-r--r-- 1 andrej users 0 2009-08-28 06:34 someFile.ext
-rw-r--r-- 1 andrej users 0 2009-08-28 06:34 otherFile.ext
-rw-r--r-- 1 andrej users 0 2009-08-28 06:34 newFile.ext
-rw-r--r-- 1 andrej users 0 2009-08-28 06:34 File.ext
$ for file in *.ext ; do mv $file $(echo $file | sed 's/(.*)//') ; done
$ ls -ltr                                                    total 0
-rw-r--r-- 1 andrej users 0 2009-08-28 06:35 someFile.ext
-rw-r--r-- 1 andrej users 0 2009-08-28 06:35 otherFile.ext
-rw-r--r-- 1 andrej users 0 2009-08-28 06:35 newFile.ext
-rw-r--r-- 1 andrej users 0 2009-08-28 06:35 File.ext
The alternative was to use the first sed and a -r flag with it.

Last edited by Tinkster; 08-27-2009 at 01:38 PM.
 
Old 08-27-2009, 02:06 PM   #9
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,252

Rep: Reputation: 205Reputation: 205Reputation: 205
Quote:
Originally Posted by Tinkster View Post
You're right ... I was mixing sed's syntax for regex and extended regex.
I did the same mistake.

That command doesn't work:

for file in *.ext ; do mv $file `echo $file | sed "s/\(.*\)//"` ; done

That command works:

for file in *.ext ; do mv $file `echo $file | sed "s/(.*)//"` ; done
 
  


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
Need information about wild cards shridhar005 Linux - Newbie 1 08-27-2008 01:41 PM
how to do * wild cards in python donnied Programming 6 08-19-2008 09:26 PM
rename command does not work! wtn2die Linux - General 6 12-27-2006 11:00 AM
Wild Cards using the find command -- grrr hpladd Linux - Newbie 9 07-14-2005 05:18 PM
More Players Supporting Wild-Cards? chime Linux - General 0 12-01-2004 10:21 AM


All times are GMT -5. The time now is 02:33 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration