Index

Subject : Re: LUG: Rename command question

From : jpgoel@ncsu.[redacted]

Date : Sat, 20 Mar 2010 05:08:22 -0400

Parent


Add "g" to the end, like so:

rename 's/ /-/g' *

The "g" stands for "global" and means to replace all matches, not just the
first one. (also works when using ":s" in vi to do a replace.)

Cheers,

Jay

On Sat, March 20, 2010 5:02 am, Daniel Underwood wrote:
> Folks,
>
> Suppose I have a file with multiple spaces in the filename, e.g., "my
> homework submission.txt" and I wish to replace each occurrence of a
> "space" with a "dash". The command
>
> $ rename 's/ /-/' *
>
> results in changing the filename to "my-homework submission.txt".
> Notice that only the first occurrence of a "space" is replaced. This,
> of course, is not what I want to do.
>
> So my question is how can I use <rename> to substitute for *all*
> occurrences of the matched expression? I'd prefer to avoid using a
> script such as the following:
>
> for f in *; do mv "$f" `echo $f | tr ' ' '-'`; done ;
>
> Also, why should the <tr> command replace all occurrences but the
> <rename> command only replaces the first occurrence?
>
> TIA!
> --
> Daniel Underwood
> North Carolina State University
> Graduate Student - Operations Research
> email: daniel.underwood@ncsu.[redacted]
> phone: XXX.302.3291
> web: http://www4.ncsu.edu/~djunderw/
>
>



Replies :