Renaming multiple files with rename
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I need to rename files in batch--the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be named. I do know that they will start with NORMAL
and end with -lib*
. I'd like to replace everything in between with some string X
. For example,
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.concordant
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.deletion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.divergent
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.inversion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.translocation
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.concordant
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.deletion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.divergent
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translocation
So it will probably be of the form rename "s/something/X/"
, but I don't know what that something should be, as I don't know how to use regex.
shell shell-script rename
add a comment |
up vote
5
down vote
favorite
I need to rename files in batch--the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be named. I do know that they will start with NORMAL
and end with -lib*
. I'd like to replace everything in between with some string X
. For example,
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.concordant
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.deletion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.divergent
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.inversion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.translocation
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.concordant
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.deletion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.divergent
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translocation
So it will probably be of the form rename "s/something/X/"
, but I don't know what that something should be, as I don't know how to use regex.
shell shell-script rename
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I need to rename files in batch--the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be named. I do know that they will start with NORMAL
and end with -lib*
. I'd like to replace everything in between with some string X
. For example,
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.concordant
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.deletion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.divergent
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.inversion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.translocation
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.concordant
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.deletion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.divergent
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translocation
So it will probably be of the form rename "s/something/X/"
, but I don't know what that something should be, as I don't know how to use regex.
shell shell-script rename
I need to rename files in batch--the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be named. I do know that they will start with NORMAL
and end with -lib*
. I'd like to replace everything in between with some string X
. For example,
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.concordant
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.deletion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.divergent
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.inversion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.translocation
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.concordant
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.deletion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.divergent
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translocation
So it will probably be of the form rename "s/something/X/"
, but I don't know what that something should be, as I don't know how to use regex.
shell shell-script rename
shell shell-script rename
edited Nov 17 at 2:21
Rui F Ribeiro
38.2k1475123
38.2k1475123
asked Aug 1 '14 at 15:19
goodcow
348136
348136
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
accepted
If you are lucky enough to have rename
available, then the following should be sufficient:
rename 's/(NORMAL).*(-lib)/$1X$2/' *
add a comment |
up vote
2
down vote
The useful Perl powered rename
isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename
utility that does basic search and replace and nothing much else.
If you're unfortunate enough to be using one of those, then something like the following may help:
for oldname in *; do
newname=$(echo "$oldname" | sed -e 's/(NORMAL).*(-lib.*)/1X2/')
mv "$oldname" "$newname"
done
add a comment |
up vote
1
down vote
There are two unrelated programs called rename
. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do
rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib*
The s
operator performs a regular expression replacement. There are oodles of regular expression tutorials on the web, e.g. 1. .*-lib
matches the string up to the last occurrence of -lib
(.*
matches any string), so s/.*-lib/NORMAL_X-lib/
replaces the NORMAL_blahblah-lib
part by NORMAL_X-lib
.
If you have a Linux distribution that isn't derived from Debian, then the rename
utility is one that is suitable for almost no practical task. You can use a shell loop instead.
for x in NORMAL_*-lib*; do
mv "$x" "NORMAL_X-lib$x##*-lib"
done
$x##*-lib
is the value of the variable x
minus the part up to the last occurrence of -lib
. This is standard parameter expansion syntax.
This task is easiest in zsh, with its zmv
function. Put autoload -U zmv
in your .zshrc
(or run it on the command line for a once-off), then run
zmv 'NORMAL_*-(lib*)' 'NORMAL_X-$1'
add a comment |
up vote
0
down vote
You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib)
i.e.
$ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' *
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed as NORMALX-lib4.translation
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If you are lucky enough to have rename
available, then the following should be sufficient:
rename 's/(NORMAL).*(-lib)/$1X$2/' *
add a comment |
up vote
1
down vote
accepted
If you are lucky enough to have rename
available, then the following should be sufficient:
rename 's/(NORMAL).*(-lib)/$1X$2/' *
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If you are lucky enough to have rename
available, then the following should be sufficient:
rename 's/(NORMAL).*(-lib)/$1X$2/' *
If you are lucky enough to have rename
available, then the following should be sufficient:
rename 's/(NORMAL).*(-lib)/$1X$2/' *
answered Aug 1 '14 at 19:04
Nick Tomlin
21738
21738
add a comment |
add a comment |
up vote
2
down vote
The useful Perl powered rename
isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename
utility that does basic search and replace and nothing much else.
If you're unfortunate enough to be using one of those, then something like the following may help:
for oldname in *; do
newname=$(echo "$oldname" | sed -e 's/(NORMAL).*(-lib.*)/1X2/')
mv "$oldname" "$newname"
done
add a comment |
up vote
2
down vote
The useful Perl powered rename
isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename
utility that does basic search and replace and nothing much else.
If you're unfortunate enough to be using one of those, then something like the following may help:
for oldname in *; do
newname=$(echo "$oldname" | sed -e 's/(NORMAL).*(-lib.*)/1X2/')
mv "$oldname" "$newname"
done
add a comment |
up vote
2
down vote
up vote
2
down vote
The useful Perl powered rename
isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename
utility that does basic search and replace and nothing much else.
If you're unfortunate enough to be using one of those, then something like the following may help:
for oldname in *; do
newname=$(echo "$oldname" | sed -e 's/(NORMAL).*(-lib.*)/1X2/')
mv "$oldname" "$newname"
done
The useful Perl powered rename
isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename
utility that does basic search and replace and nothing much else.
If you're unfortunate enough to be using one of those, then something like the following may help:
for oldname in *; do
newname=$(echo "$oldname" | sed -e 's/(NORMAL).*(-lib.*)/1X2/')
mv "$oldname" "$newname"
done
answered Aug 1 '14 at 18:27
garethTheRed
23.6k35978
23.6k35978
add a comment |
add a comment |
up vote
1
down vote
There are two unrelated programs called rename
. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do
rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib*
The s
operator performs a regular expression replacement. There are oodles of regular expression tutorials on the web, e.g. 1. .*-lib
matches the string up to the last occurrence of -lib
(.*
matches any string), so s/.*-lib/NORMAL_X-lib/
replaces the NORMAL_blahblah-lib
part by NORMAL_X-lib
.
If you have a Linux distribution that isn't derived from Debian, then the rename
utility is one that is suitable for almost no practical task. You can use a shell loop instead.
for x in NORMAL_*-lib*; do
mv "$x" "NORMAL_X-lib$x##*-lib"
done
$x##*-lib
is the value of the variable x
minus the part up to the last occurrence of -lib
. This is standard parameter expansion syntax.
This task is easiest in zsh, with its zmv
function. Put autoload -U zmv
in your .zshrc
(or run it on the command line for a once-off), then run
zmv 'NORMAL_*-(lib*)' 'NORMAL_X-$1'
add a comment |
up vote
1
down vote
There are two unrelated programs called rename
. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do
rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib*
The s
operator performs a regular expression replacement. There are oodles of regular expression tutorials on the web, e.g. 1. .*-lib
matches the string up to the last occurrence of -lib
(.*
matches any string), so s/.*-lib/NORMAL_X-lib/
replaces the NORMAL_blahblah-lib
part by NORMAL_X-lib
.
If you have a Linux distribution that isn't derived from Debian, then the rename
utility is one that is suitable for almost no practical task. You can use a shell loop instead.
for x in NORMAL_*-lib*; do
mv "$x" "NORMAL_X-lib$x##*-lib"
done
$x##*-lib
is the value of the variable x
minus the part up to the last occurrence of -lib
. This is standard parameter expansion syntax.
This task is easiest in zsh, with its zmv
function. Put autoload -U zmv
in your .zshrc
(or run it on the command line for a once-off), then run
zmv 'NORMAL_*-(lib*)' 'NORMAL_X-$1'
add a comment |
up vote
1
down vote
up vote
1
down vote
There are two unrelated programs called rename
. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do
rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib*
The s
operator performs a regular expression replacement. There are oodles of regular expression tutorials on the web, e.g. 1. .*-lib
matches the string up to the last occurrence of -lib
(.*
matches any string), so s/.*-lib/NORMAL_X-lib/
replaces the NORMAL_blahblah-lib
part by NORMAL_X-lib
.
If you have a Linux distribution that isn't derived from Debian, then the rename
utility is one that is suitable for almost no practical task. You can use a shell loop instead.
for x in NORMAL_*-lib*; do
mv "$x" "NORMAL_X-lib$x##*-lib"
done
$x##*-lib
is the value of the variable x
minus the part up to the last occurrence of -lib
. This is standard parameter expansion syntax.
This task is easiest in zsh, with its zmv
function. Put autoload -U zmv
in your .zshrc
(or run it on the command line for a once-off), then run
zmv 'NORMAL_*-(lib*)' 'NORMAL_X-$1'
There are two unrelated programs called rename
. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do
rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib*
The s
operator performs a regular expression replacement. There are oodles of regular expression tutorials on the web, e.g. 1. .*-lib
matches the string up to the last occurrence of -lib
(.*
matches any string), so s/.*-lib/NORMAL_X-lib/
replaces the NORMAL_blahblah-lib
part by NORMAL_X-lib
.
If you have a Linux distribution that isn't derived from Debian, then the rename
utility is one that is suitable for almost no practical task. You can use a shell loop instead.
for x in NORMAL_*-lib*; do
mv "$x" "NORMAL_X-lib$x##*-lib"
done
$x##*-lib
is the value of the variable x
minus the part up to the last occurrence of -lib
. This is standard parameter expansion syntax.
This task is easiest in zsh, with its zmv
function. Put autoload -U zmv
in your .zshrc
(or run it on the command line for a once-off), then run
zmv 'NORMAL_*-(lib*)' 'NORMAL_X-$1'
answered Aug 1 '14 at 23:16
Gilles
521k12610401569
521k12610401569
add a comment |
add a comment |
up vote
0
down vote
You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib)
i.e.
$ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' *
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed as NORMALX-lib4.translation
add a comment |
up vote
0
down vote
You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib)
i.e.
$ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' *
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed as NORMALX-lib4.translation
add a comment |
up vote
0
down vote
up vote
0
down vote
You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib)
i.e.
$ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' *
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed as NORMALX-lib4.translation
You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib)
i.e.
$ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' *
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion
NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed as NORMALX-lib4.translation
answered Aug 1 '14 at 16:16
steeldriver
33.6k34982
33.6k34982
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f147879%2frenaming-multiple-files-with-rename%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown