LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   rename command and wild cards (https://www.linuxquestions.org/questions/linux-newbie-8/rename-command-and-wild-cards-750661/)

dkrysak 08-27-2009 11:17 AM

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

fordeck 08-27-2009 12:34 PM

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

rnturn 08-27-2009 12:54 PM

Quote:

Originally Posted by fordeck (Post 3659752)
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

w1k0 08-27-2009 01:09 PM

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

Tinkster 08-27-2009 01:15 PM

Why summon perl if sed does the same thing?
;}

Code:

sed 's/\(.*\)//'

w1k0 08-27-2009 01:20 PM

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
!
#
%
&
'
(
)
,
;
=
[
]

w1k0 08-27-2009 01:24 PM

Quote:

Originally Posted by Tinkster (Post 3659808)
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

Tinkster 08-27-2009 01:37 PM

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.

w1k0 08-27-2009 02:06 PM

Quote:

Originally Posted by Tinkster (Post 3659832)
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


All times are GMT -5. The time now is 07:08 AM.