'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
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
Whichrename
are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
– terdon♦
Nov 28 at 17:30
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
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
New contributor
edited Nov 29 at 9:02
terdon♦
127k31244421
127k31244421
New contributor
asked Nov 28 at 14:23
astzge2
133
133
New contributor
New contributor
Whichrename
are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
– terdon♦
Nov 28 at 17:30
add a comment |
Whichrename
are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
– terdon♦
Nov 28 at 17:30
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♦
Nov 28 at 17:30
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♦
Nov 28 at 17:30
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-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
Nov 28 at 16:00
@astzge2 it seems ourmmv
s are different unfortunately - what is your OS?
– steeldriver
Nov 28 at 16:07
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 causedrename
to stop interpreting-bar.
as a-b
option. Thanks!
– astzge2
Nov 29 at 8:01
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
Nov 28 at 15:41
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
Nov 28 at 16:01
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-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
Nov 28 at 16:00
@astzge2 it seems ourmmv
s are different unfortunately - what is your OS?
– steeldriver
Nov 28 at 16:07
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 causedrename
to stop interpreting-bar.
as a-b
option. Thanks!
– astzge2
Nov 29 at 8:01
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-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
Nov 28 at 16:00
@astzge2 it seems ourmmv
s are different unfortunately - what is your OS?
– steeldriver
Nov 28 at 16:07
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 causedrename
to stop interpreting-bar.
as a-b
option. Thanks!
– astzge2
Nov 29 at 8:01
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 Nov 28 at 16:04
answered Nov 28 at 14:43
steeldriver
33.7k34983
33.7k34983
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
Nov 28 at 16:00
@astzge2 it seems ourmmv
s are different unfortunately - what is your OS?
– steeldriver
Nov 28 at 16:07
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 causedrename
to stop interpreting-bar.
as a-b
option. Thanks!
– astzge2
Nov 29 at 8:01
add a comment |
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
Nov 28 at 16:00
@astzge2 it seems ourmmv
s are different unfortunately - what is your OS?
– steeldriver
Nov 28 at 16:07
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 causedrename
to stop interpreting-bar.
as a-b
option. Thanks!
– astzge2
Nov 29 at 8:01
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
Nov 28 at 16:00
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
Nov 28 at 16:00
@astzge2 it seems our
mmv
s are different unfortunately - what is your OS?– steeldriver
Nov 28 at 16:07
@astzge2 it seems our
mmv
s are different unfortunately - what is your OS?– steeldriver
Nov 28 at 16:07
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
Nov 29 at 8:01
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
Nov 29 at 8:01
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
Nov 28 at 15:41
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
Nov 28 at 16:01
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
Nov 28 at 15:41
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
Nov 28 at 16:01
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 Nov 28 at 16:00
answered Nov 28 at 14:31
Kusalananda
118k16223361
118k16223361
The Perl rename won't help if they have the util-linux one...
– ilkkachu
Nov 28 at 15:41
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
Nov 28 at 16:01
add a comment |
The Perl rename won't help if they have the util-linux one...
– ilkkachu
Nov 28 at 15:41
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
Nov 28 at 16:01
The Perl rename won't help if they have the util-linux one...
– ilkkachu
Nov 28 at 15:41
The Perl rename won't help if they have the util-linux one...
– ilkkachu
Nov 28 at 15:41
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
Nov 28 at 16:01
@ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
– Kusalananda
Nov 28 at 16:01
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 Nov 28 at 16:00
Christopher
9,40032846
9,40032846
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
rename
are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?– terdon♦
Nov 28 at 17:30