linux-admin.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* renaming multiple files
@ 2004-11-22 15:57 Fabio Zyserman
  2004-11-22 16:42 ` harry_b
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Fabio Zyserman @ 2004-11-22 15:57 UTC (permalink / raw)
  To: linux-admin

Hi all,

I have some files in a directory, called
prefix-file1
prefix-file2
etc

I want to rename them to
file1
file2
etc

How can I do it with a bash command?

Many thanks in advance,

Fabio Zyserman

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming multiple files
  2004-11-22 15:57 renaming multiple files Fabio Zyserman
@ 2004-11-22 16:42 ` harry_b
  2004-11-22 18:03 ` urgrue
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: harry_b @ 2004-11-22 16:42 UTC (permalink / raw)
  To: zyserman; +Cc: linux-admin

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Fabia,

try something like this:

for f in prefix-*; do n=`echo $f | sed -e 's/prefix-//'`; echo $f $n; done

Make sure you use backticks for the n= part of the line!

When you replace 'echo' with 'mv' the files get renamed. :-)

Have fun!

Harry


- --On Monday, November 22, 2004 12:57:40 -0300 Fabio Zyserman 
<zyserman@fcaglp.unlp.edu.ar> wrote:

> Hi all,
>
> I have some files in a directory, called
> prefix-file1
> prefix-file2
> etc
>
> I want to rename them to
> file1
> file2
> etc
>
> How can I do it with a bash command?

- --

1024D/40F14012 18F3 736A 4080 303C E61E  2E72 7E05 1F6E 40F1 4012

- -----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GIT/S dx s: a C++ ULS++++$ P+++ L+++$ !E W++ N+ o? K? !w !O !M
V PS+ PE Y? PGP+++ t+ 5-- X+ R+ !tv b++ DI++ D+ G e* h r++ y++
- ------END GEEK CODE BLOCK------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBohcSfgUfbkDxQBIRAhFkAJ9+2zjDXYqe3jfiEihAcBBfabxrgACfT1Uc
dLphj9Bv4wr3Md9+JhspsAI=
=FWw3
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming multiple files
  2004-11-22 15:57 renaming multiple files Fabio Zyserman
  2004-11-22 16:42 ` harry_b
@ 2004-11-22 18:03 ` urgrue
       [not found] ` <2005107FDBE38F10D101E700@192.168.1.247>
  2004-11-23  1:30 ` Glynn Clements
  3 siblings, 0 replies; 9+ messages in thread
From: urgrue @ 2004-11-22 18:03 UTC (permalink / raw)
  To: Fabio Zyserman; +Cc: linux-admin

Using the command "mmv":
mmv "prefix-*" "#1"

This would strip "prefix-" from the beginning of each file.



On Mon, Nov 22, 2004 at 12:57:40PM -0300, Fabio Zyserman wrote:
> Hi all,
> 
> I have some files in a directory, called
> prefix-file1
> prefix-file2
> etc
> 
> I want to rename them to
> file1
> file2
> etc
> 
> How can I do it with a bash command?
> 
> Many thanks in advance,
> 
> Fabio Zyserman
> -
> To unsubscribe from this list: send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming multiple files
       [not found] ` <2005107FDBE38F10D101E700@192.168.1.247>
@ 2004-11-22 18:30   ` Helge Pettersen
  0 siblings, 0 replies; 9+ messages in thread
From: Helge Pettersen @ 2004-11-22 18:30 UTC (permalink / raw)
  To: linux-admin; +Cc: zyserman

This is also possible to do with the rename command.
As stated in the help file: rename <from> <to> <files>.
So in your case this would be: rename "prefix-" "" prefix*
In my opinion, a lot easier than using a for loop. :-)

On Mon, 22 Nov 2004 17:42:58 +0100, harry_b@mm.st <harry_b@mm.st> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Hi Fabia,
> 
> try something like this:
> 
> for f in prefix-*; do n=`echo $f | sed -e 's/prefix-//'`; echo $f $n; done
> 
> Make sure you use backticks for the n= part of the line!
> 
> When you replace 'echo' with 'mv' the files get renamed. :-)
> 
> Have fun!
> 
> Harry
> 
> - --On Monday, November 22, 2004 12:57:40 -0300 Fabio Zyserman
> 
> 
> <zyserman@fcaglp.unlp.edu.ar> wrote:
> 
> > Hi all,
> >
> > I have some files in a directory, called
> > prefix-file1
> > prefix-file2
> > etc
> >
> > I want to rename them to
> > file1
> > file2
> > etc
> >
> > How can I do it with a bash command?
> 
> - --
> 
> 1024D/40F14012 18F3 736A 4080 303C E61E  2E72 7E05 1F6E 40F1 4012
> 
> - -----BEGIN GEEK CODE BLOCK-----
> Version: 3.12
> GIT/S dx s: a C++ ULS++++$ P+++ L+++$ !E W++ N+ o? K? !w !O !M
> V PS+ PE Y? PGP+++ t+ 5-- X+ R+ !tv b++ DI++ D+ G e* h r++ y++
> - ------END GEEK CODE BLOCK------
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.5 (GNU/Linux)
> 
> iD8DBQFBohcSfgUfbkDxQBIRAhFkAJ9+2zjDXYqe3jfiEihAcBBfabxrgACfT1Uc
> dLphj9Bv4wr3Md9+JhspsAI=
> =FWw3
> -----END PGP SIGNATURE-----
> 
> 
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming multiple files
  2004-11-22 15:57 renaming multiple files Fabio Zyserman
                   ` (2 preceding siblings ...)
       [not found] ` <2005107FDBE38F10D101E700@192.168.1.247>
@ 2004-11-23  1:30 ` Glynn Clements
  2004-12-08  1:05   ` Jeff Woods
  3 siblings, 1 reply; 9+ messages in thread
From: Glynn Clements @ 2004-11-23  1:30 UTC (permalink / raw)
  To: zyserman; +Cc: linux-admin


Fabio Zyserman wrote:

> I have some files in a directory, called
> prefix-file1
> prefix-file2
> etc
> 
> I want to rename them to
> file1
> file2
> etc
> 
> How can I do it with a bash command?

	for file in prefix-* ; do
	    mv "$file" "${file##prefix-}"
	done

Various utilities exist to perform this task, e.g. rename, mmv, but
none are standard (rename is from util-linux, so it's likely to exist
on most Linux systems, but not other Unices).

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming multiple files
  2004-11-23  1:30 ` Glynn Clements
@ 2004-12-08  1:05   ` Jeff Woods
  2004-12-09 19:09     ` Glynn Clements
  0 siblings, 1 reply; 9+ messages in thread
From: Jeff Woods @ 2004-12-08  1:05 UTC (permalink / raw)
  To: Glynn Clements; +Cc: zyserman, linux-admin

At 11/23/2004 01:30 AM +0000, Glynn Clements wrote:
>         for file in prefix-* ; do
>             mv "$file" "${file##prefix-}"
>         done

Almost correct.
What happens if one of the files is named "prefix-prefix-1"?
What about "prefix-"?

I suggest:

for file in prefix-?*
do
mv "$file" "${file#prefix-}"
done

--
Jeff Woods
"Errors creep into everything, and the only way to expunge them is to have 
any bit of work reviewed by a few others." -- Wirt Atmar, 10-27-2001
"The great thing about Open Source software is that you can have any color 
screen of death that you want." -- Gavin Scott, 08-22-2000 



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming multiple files
  2004-12-08  1:05   ` Jeff Woods
@ 2004-12-09 19:09     ` Glynn Clements
  2004-12-09 21:02       ` Jeff Woods
  0 siblings, 1 reply; 9+ messages in thread
From: Glynn Clements @ 2004-12-09 19:09 UTC (permalink / raw)
  To: Jeff Woods; +Cc: zyserman, linux-admin


Jeff Woods wrote:

> >         for file in prefix-* ; do
> >             mv "$file" "${file##prefix-}"
> >         done
> 
> Almost correct.
> What happens if one of the files is named "prefix-prefix-1"?

It gets renamed to prefix-1, which is what I would expect.

> What about "prefix-"?
> 
> I suggest:
> 
> for file in prefix-?*
> do
> mv "$file" "${file#prefix-}"
> done

Huh?

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming multiple files
  2004-12-09 19:09     ` Glynn Clements
@ 2004-12-09 21:02       ` Jeff Woods
  2004-12-10 15:17         ` Glynn Clements
  0 siblings, 1 reply; 9+ messages in thread
From: Jeff Woods @ 2004-12-09 21:02 UTC (permalink / raw)
  To: Glynn Clements; +Cc: zyserman, linux-admin

At 12/9/2004 07:09 PM +0000, Glynn Clements wrote:
>Jeff Woods wrote:
>>>         for file in prefix-* ; do
>>>              mv "$file" "${file##prefix-}"
>>>         done

>>What happens if one of the files is named "prefix-prefix-1"?
>
>It gets renamed to prefix-1, which is what I would expect.

No.  Because you used two of "##" it removes all instances of "prefix1" 
from the front of the variable and the resulting filename would be "1".

>>What about "prefix-"?
>>
>>I suggest:
>>
>>for file in prefix-?*
>>do
>>mv "$file" "${file#prefix-}"
>>done
>
>Huh?

I added "?" to the pattern so that a file with the exact name "prefix-" 
would not be selected since that would result in a modified filename that 
was an empty string.

I also reduced the "##" to "#" so it only removes one "prefix-" from the 
beginning even if there are more than one.

Like I said, "Almost correct."  Your version works in all cases except for 
filenames matching patterns "prefix-" and "prefix-prefix-*" where it would 
fail or give what are probably undesired results.

--
Jeff Woods <kazrak+kernel@cesmail.net> 



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming multiple files
  2004-12-09 21:02       ` Jeff Woods
@ 2004-12-10 15:17         ` Glynn Clements
  0 siblings, 0 replies; 9+ messages in thread
From: Glynn Clements @ 2004-12-10 15:17 UTC (permalink / raw)
  To: Jeff Woods; +Cc: zyserman, linux-admin


Jeff Woods wrote:

> At 12/9/2004 07:09 PM +0000, Glynn Clements wrote:
> >Jeff Woods wrote:
> >>>         for file in prefix-* ; do
> >>>              mv "$file" "${file##prefix-}"
> >>>         done
> 
> >>What happens if one of the files is named "prefix-prefix-1"?
> >
> >It gets renamed to prefix-1, which is what I would expect.
> 
> No.  Because you used two of "##" it removes all instances of "prefix1" 
> from the front of the variable and the resulting filename would be "1".

Not here:

	$ file=prefix-prefix-1
	$ echo "${file##prefix-}"
	prefix-1

AFAICT, the difference between # and ## (shortest/longest match) is
only relevant if the pattern contains wildcards, not for fixed
strings.

> >>What about "prefix-"?
> >>
> >>I suggest:
> >>
> >>for file in prefix-?*
> >>do
> >>mv "$file" "${file#prefix-}"
> >>done
> >
> >Huh?
> 
> I added "?" to the pattern so that a file with the exact name "prefix-" 
> would not be selected since that would result in a modified filename that 
> was an empty string.

OK.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2004-12-10 15:17 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-11-22 15:57 renaming multiple files Fabio Zyserman
2004-11-22 16:42 ` harry_b
2004-11-22 18:03 ` urgrue
     [not found] ` <2005107FDBE38F10D101E700@192.168.1.247>
2004-11-22 18:30   ` Helge Pettersen
2004-11-23  1:30 ` Glynn Clements
2004-12-08  1:05   ` Jeff Woods
2004-12-09 19:09     ` Glynn Clements
2004-12-09 21:02       ` Jeff Woods
2004-12-10 15:17         ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).