'rename' with expression|replacement with a leading '-' (hyphen|minus)
up vote
2
down vote
favorite
I have many files like xyz_123_foo.ext for which I would like to add -bar to the filenames at the end to result in xyz_123_foo-bar.ext. I tried:
rename . -bar. xyz_*
which resulted in:
rename: invalid option -- 'b'
followed by the usage text. I then tried variations with '-bar' and "-bar" to no avail.
How can I get rename to accept - as part of the replacement string?
Or would another command be more efficient or appropriate?
My shell is bash and I am using the rename from util-linux on SuSe Linux SLE12.
rename string arguments options
New contributor
astzge2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
2
down vote
favorite
I have many files like xyz_123_foo.ext for which I would like to add -bar to the filenames at the end to result in xyz_123_foo-bar.ext. I tried:
rename . -bar. xyz_*
which resulted in:
rename: invalid option -- 'b'
followed by the usage text. I then tried variations with '-bar' and "-bar" to no avail.
How can I get rename to accept - as part of the replacement string?
Or would another command be more efficient or appropriate?
My shell is bash and I am using the rename from util-linux on SuSe Linux SLE12.
rename string arguments options
New contributor
astzge2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Whichrenameare you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
– terdon♦
2 days ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have many files like xyz_123_foo.ext for which I would like to add -bar to the filenames at the end to result in xyz_123_foo-bar.ext. I tried:
rename . -bar. xyz_*
which resulted in:
rename: invalid option -- 'b'
followed by the usage text. I then tried variations with '-bar' and "-bar" to no avail.
How can I get rename to accept - as part of the replacement string?
Or would another command be more efficient or appropriate?
My shell is bash and I am using the rename from util-linux on SuSe Linux SLE12.
rename string arguments options
New contributor
astzge2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have many files like xyz_123_foo.ext for which I would like to add -bar to the filenames at the end to result in xyz_123_foo-bar.ext. I tried:
rename . -bar. xyz_*
which resulted in:
rename: invalid option -- 'b'
followed by the usage text. I then tried variations with '-bar' and "-bar" to no avail.
How can I get rename to accept - as part of the replacement string?
Or would another command be more efficient or appropriate?
My shell is bash and I am using the rename from util-linux on SuSe Linux SLE12.
rename string arguments options
rename string arguments options
New contributor
astzge2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
astzge2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited yesterday
terdon♦
126k31244420
126k31244420
New contributor
astzge2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
astzge2
133
133
New contributor
astzge2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
astzge2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
astzge2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Whichrenameare you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
– terdon♦
2 days ago
add a comment |
Whichrenameare you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
– terdon♦
2 days ago
Which
rename are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?– terdon♦
2 days ago
Which
rename are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?– terdon♦
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
mmv is nice for tasks like this ex.
mmv -n -- '*.ext' '#1-bar.ext'
or for any dot extension
mmv -n -- '*.*' '#1-bar.#2'
Remove the -n once you are happy that it is doing the right thing.
This seems really elegant, but I get an error and the usage when I use the-nOption:> mmv -n '*.*' '#1-bar.#2'.../mmv: illegal option -- n.../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ]Without-nnothing seems to happen
– astzge2
2 days ago
@astzge2 it seems ourmmvs are different unfortunately - what is your OS?
– steeldriver
2 days ago
I'm on SLE12. However, you've inadvertently answered my question! The--end-of-options in Linux was unknown to me until I saw your edit. Including that causedrenameto stop interpreting-bar.as a-boption. Thanks!
– astzge2
yesterday
add a comment |
up vote
3
down vote
Assuming that your rename is the Perl variant of the rename utility:
rename 's/$/-bak/' xyz_*
This uses a Perl expression that simply inserts -bak at the end of all given filenames through a substitution of $ (the "end of line" anchor).
You could also use
rename '$_ .= "-bak"' xyz_*
which uses another Perl expression that appends the string -bak to $_ (the "current thing" in Perl, here it's a filename).
For a shell loop solution:
for name in xyz_*; do
mv -i "$name" "$name-bak"
done
The Perl rename won't help if they have the util-linux one...
– ilkkachu
2 days ago
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
2 days ago
add a comment |
up vote
1
down vote
Assuming we're talking about the PERL extension, rename (and not the rename from util-linux)...
rename 's/.ext/-bar.ext/' xyz_123_foo.ext
The result changes the file name to xyz_123_foo-bar.ext.
The portion within single quotes is a PERL regular expression. The s substitutes .ext in the file name with -bar.ext. One might combine this with find, assuming these files are in your home directory (~).
find ~ -type f -name '*.ext' -exec rename 's/.ext/-bar.ext/' {} +
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
mmv is nice for tasks like this ex.
mmv -n -- '*.ext' '#1-bar.ext'
or for any dot extension
mmv -n -- '*.*' '#1-bar.#2'
Remove the -n once you are happy that it is doing the right thing.
This seems really elegant, but I get an error and the usage when I use the-nOption:> mmv -n '*.*' '#1-bar.#2'.../mmv: illegal option -- n.../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ]Without-nnothing seems to happen
– astzge2
2 days ago
@astzge2 it seems ourmmvs are different unfortunately - what is your OS?
– steeldriver
2 days ago
I'm on SLE12. However, you've inadvertently answered my question! The--end-of-options in Linux was unknown to me until I saw your edit. Including that causedrenameto stop interpreting-bar.as a-boption. Thanks!
– astzge2
yesterday
add a comment |
up vote
4
down vote
accepted
mmv is nice for tasks like this ex.
mmv -n -- '*.ext' '#1-bar.ext'
or for any dot extension
mmv -n -- '*.*' '#1-bar.#2'
Remove the -n once you are happy that it is doing the right thing.
This seems really elegant, but I get an error and the usage when I use the-nOption:> mmv -n '*.*' '#1-bar.#2'.../mmv: illegal option -- n.../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ]Without-nnothing seems to happen
– astzge2
2 days ago
@astzge2 it seems ourmmvs are different unfortunately - what is your OS?
– steeldriver
2 days ago
I'm on SLE12. However, you've inadvertently answered my question! The--end-of-options in Linux was unknown to me until I saw your edit. Including that causedrenameto stop interpreting-bar.as a-boption. Thanks!
– astzge2
yesterday
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
mmv is nice for tasks like this ex.
mmv -n -- '*.ext' '#1-bar.ext'
or for any dot extension
mmv -n -- '*.*' '#1-bar.#2'
Remove the -n once you are happy that it is doing the right thing.
mmv is nice for tasks like this ex.
mmv -n -- '*.ext' '#1-bar.ext'
or for any dot extension
mmv -n -- '*.*' '#1-bar.#2'
Remove the -n once you are happy that it is doing the right thing.
edited 2 days ago
answered 2 days ago
steeldriver
33.7k34983
33.7k34983
This seems really elegant, but I get an error and the usage when I use the-nOption:> mmv -n '*.*' '#1-bar.#2'.../mmv: illegal option -- n.../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ]Without-nnothing seems to happen
– astzge2
2 days ago
@astzge2 it seems ourmmvs are different unfortunately - what is your OS?
– steeldriver
2 days ago
I'm on SLE12. However, you've inadvertently answered my question! The--end-of-options in Linux was unknown to me until I saw your edit. Including that causedrenameto stop interpreting-bar.as a-boption. Thanks!
– astzge2
yesterday
add a comment |
This seems really elegant, but I get an error and the usage when I use the-nOption:> mmv -n '*.*' '#1-bar.#2'.../mmv: illegal option -- n.../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ]Without-nnothing seems to happen
– astzge2
2 days ago
@astzge2 it seems ourmmvs are different unfortunately - what is your OS?
– steeldriver
2 days ago
I'm on SLE12. However, you've inadvertently answered my question! The--end-of-options in Linux was unknown to me until I saw your edit. Including that causedrenameto stop interpreting-bar.as a-boption. Thanks!
– astzge2
yesterday
This seems really elegant, but I get an error and the usage when I use the
-n Option: > mmv -n '*.*' '#1-bar.#2' .../mmv: illegal option -- n .../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ] Without -n nothing seems to happen– astzge2
2 days ago
This seems really elegant, but I get an error and the usage when I use the
-n Option: > mmv -n '*.*' '#1-bar.#2' .../mmv: illegal option -- n .../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ] Without -n nothing seems to happen– astzge2
2 days ago
@astzge2 it seems our
mmvs are different unfortunately - what is your OS?– steeldriver
2 days ago
@astzge2 it seems our
mmvs are different unfortunately - what is your OS?– steeldriver
2 days ago
I'm on SLE12. However, you've inadvertently answered my question! The
-- end-of-options in Linux was unknown to me until I saw your edit. Including that caused rename to stop interpreting -bar. as a -b option. Thanks!– astzge2
yesterday
I'm on SLE12. However, you've inadvertently answered my question! The
-- end-of-options in Linux was unknown to me until I saw your edit. Including that caused rename to stop interpreting -bar. as a -b option. Thanks!– astzge2
yesterday
add a comment |
up vote
3
down vote
Assuming that your rename is the Perl variant of the rename utility:
rename 's/$/-bak/' xyz_*
This uses a Perl expression that simply inserts -bak at the end of all given filenames through a substitution of $ (the "end of line" anchor).
You could also use
rename '$_ .= "-bak"' xyz_*
which uses another Perl expression that appends the string -bak to $_ (the "current thing" in Perl, here it's a filename).
For a shell loop solution:
for name in xyz_*; do
mv -i "$name" "$name-bak"
done
The Perl rename won't help if they have the util-linux one...
– ilkkachu
2 days ago
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
2 days ago
add a comment |
up vote
3
down vote
Assuming that your rename is the Perl variant of the rename utility:
rename 's/$/-bak/' xyz_*
This uses a Perl expression that simply inserts -bak at the end of all given filenames through a substitution of $ (the "end of line" anchor).
You could also use
rename '$_ .= "-bak"' xyz_*
which uses another Perl expression that appends the string -bak to $_ (the "current thing" in Perl, here it's a filename).
For a shell loop solution:
for name in xyz_*; do
mv -i "$name" "$name-bak"
done
The Perl rename won't help if they have the util-linux one...
– ilkkachu
2 days ago
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
2 days ago
add a comment |
up vote
3
down vote
up vote
3
down vote
Assuming that your rename is the Perl variant of the rename utility:
rename 's/$/-bak/' xyz_*
This uses a Perl expression that simply inserts -bak at the end of all given filenames through a substitution of $ (the "end of line" anchor).
You could also use
rename '$_ .= "-bak"' xyz_*
which uses another Perl expression that appends the string -bak to $_ (the "current thing" in Perl, here it's a filename).
For a shell loop solution:
for name in xyz_*; do
mv -i "$name" "$name-bak"
done
Assuming that your rename is the Perl variant of the rename utility:
rename 's/$/-bak/' xyz_*
This uses a Perl expression that simply inserts -bak at the end of all given filenames through a substitution of $ (the "end of line" anchor).
You could also use
rename '$_ .= "-bak"' xyz_*
which uses another Perl expression that appends the string -bak to $_ (the "current thing" in Perl, here it's a filename).
For a shell loop solution:
for name in xyz_*; do
mv -i "$name" "$name-bak"
done
edited 2 days ago
answered 2 days ago
Kusalananda
118k16222360
118k16222360
The Perl rename won't help if they have the util-linux one...
– ilkkachu
2 days ago
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
2 days ago
add a comment |
The Perl rename won't help if they have the util-linux one...
– ilkkachu
2 days ago
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
2 days ago
The Perl rename won't help if they have the util-linux one...
– ilkkachu
2 days ago
The Perl rename won't help if they have the util-linux one...
– ilkkachu
2 days ago
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
2 days ago
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
2 days ago
add a comment |
up vote
1
down vote
Assuming we're talking about the PERL extension, rename (and not the rename from util-linux)...
rename 's/.ext/-bar.ext/' xyz_123_foo.ext
The result changes the file name to xyz_123_foo-bar.ext.
The portion within single quotes is a PERL regular expression. The s substitutes .ext in the file name with -bar.ext. One might combine this with find, assuming these files are in your home directory (~).
find ~ -type f -name '*.ext' -exec rename 's/.ext/-bar.ext/' {} +
add a comment |
up vote
1
down vote
Assuming we're talking about the PERL extension, rename (and not the rename from util-linux)...
rename 's/.ext/-bar.ext/' xyz_123_foo.ext
The result changes the file name to xyz_123_foo-bar.ext.
The portion within single quotes is a PERL regular expression. The s substitutes .ext in the file name with -bar.ext. One might combine this with find, assuming these files are in your home directory (~).
find ~ -type f -name '*.ext' -exec rename 's/.ext/-bar.ext/' {} +
add a comment |
up vote
1
down vote
up vote
1
down vote
Assuming we're talking about the PERL extension, rename (and not the rename from util-linux)...
rename 's/.ext/-bar.ext/' xyz_123_foo.ext
The result changes the file name to xyz_123_foo-bar.ext.
The portion within single quotes is a PERL regular expression. The s substitutes .ext in the file name with -bar.ext. One might combine this with find, assuming these files are in your home directory (~).
find ~ -type f -name '*.ext' -exec rename 's/.ext/-bar.ext/' {} +
Assuming we're talking about the PERL extension, rename (and not the rename from util-linux)...
rename 's/.ext/-bar.ext/' xyz_123_foo.ext
The result changes the file name to xyz_123_foo-bar.ext.
The portion within single quotes is a PERL regular expression. The s substitutes .ext in the file name with -bar.ext. One might combine this with find, assuming these files are in your home directory (~).
find ~ -type f -name '*.ext' -exec rename 's/.ext/-bar.ext/' {} +
answered 2 days ago
Christopher
9,39032846
9,39032846
add a comment |
add a comment |
astzge2 is a new contributor. Be nice, and check out our Code of Conduct.
astzge2 is a new contributor. Be nice, and check out our Code of Conduct.
astzge2 is a new contributor. Be nice, and check out our Code of Conduct.
astzge2 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f484675%2frename-with-expressionreplacement-with-a-leading-hyphenminus%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
Which
renameare you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?– terdon♦
2 days ago