renaming multiple files
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
-----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
Quoted 7 lines · <[email protected]> wrote:
Hi all,
I have some files in a directory, called
prefix-file1
prefix-file2
etc
Quoted 6 lines
I want to rename them to
file1
file2
etcHow 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-----
Using the command "mmv":
mmv "prefix-*" "#1"
This would strip "prefix-" from the beginning of each file.
Quoted 22 lines · Fabio Zyserman
Hi all,
I have some files in a directory, called
prefix-file1
prefix-file2
etcI want to rename them to
file1
file2
etcHow 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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
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. :-)
Quoted 60 lines · On Mon, 22 Nov 2004 17:42:58 +0100, [email protected] <[email protected]> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Hi Fabia,
try something like this:
for f in prefix-*; do n=
echo $f | sed -e 's/prefix-//'; echo $f $n; doneMake 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
Quoted 7 lines · <[email protected]> wrote:
Hi all,
I have some files in a directory, called
prefix-file1
prefix-file2
etcQuoted 6 lines
I want to rename them to
file1
file2
etcHow 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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 11 lines · Fabio Zyserman
I have some files in a directory, called
prefix-file1
prefix-file2
etcI want to rename them to
file1
file2
etcHow can I do it with a bash command?
for file in prefix-* ; do
mv "$file" "${file##prefix-}"
doneVarious 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).
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 3 lines · Glynn Clements
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
Quoted 6 lines · Jeff Woods
Quoted 3 lines
for file in prefix-* ; do
mv "$file" "${file##prefix-}"
doneAlmost 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.
Quoted 8 lines
What about "prefix-"?
I suggest:
for file in prefix-?*
do
mv "$file" "${file#prefix-}"
done
Huh?
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 8 lines · Glynn Clements
Quoted 5 lines · Jeff Woods
Quoted 3 lines
for file in prefix-* ; do
mv "$file" "${file##prefix-}"
doneWhat 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".
Quoted 10 lines
Quoted 8 lines
What about "prefix-"?
I suggest:
for file in prefix-?*
do
mv "$file" "${file#prefix-}"
doneHuh?
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 <[email protected]>
Quoted 6 lines · Jeff Woods
Quoted 4 lines · Glynn Clements
Quoted 3 lines · Jeff Woods
Quoted 3 lines
for file in prefix-* ; do
mv "$file" "${file##prefix-}"
done
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
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-1AFAICT, the difference between # and ## (shortest/longest match) is only relevant if the pattern contains wildcards, not for fixed strings.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
What about "prefix-"?
I suggest:
for file in prefix-?*
do
mv "$file" "${file#prefix-}"
done
Quoted 5 lines
Quoted 1 line
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.
Signature · 1 line
Glynn Clements <[email protected]>