renaming multiple files
am 22.11.2004 16:57:40 von Fabio Zyserman
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
Re: renaming multiple files
am 22.11.2004 17:42:58 von harry_b
-----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
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+2zjDXYqe3jfiEihAcBBfabxrgACf T1Uc
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
Re: renaming multiple files
am 22.11.2004 19:03:02 von urgrue
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
>
-
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
Re: renaming multiple files
am 22.11.2004 19:30:13 von Helge Pettersen
This is also possible to do with the rename command.
As stated in the help file: rename .
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 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
>
>
> 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+2zjDXYqe3jfiEihAcBBfabxrgACf T1Uc
> 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
>
-
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
Re: renaming multiple files
am 23.11.2004 02:30:40 von Glynn Clements
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
-
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
Re: renaming multiple files
am 08.12.2004 02:05:47 von Jeff Woods
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
-
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
Re: renaming multiple files
am 09.12.2004 20:09:26 von Glynn Clements
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
-
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
Re: renaming multiple files
am 09.12.2004 22:02:18 von Jeff Woods
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
-
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
Re: renaming multiple files
am 10.12.2004 16:17:44 von Glynn Clements
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
-
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