Renaming part of name of all files in a directory tree if match
Clash Royale CLAN TAG#URR8PPP
I have a directory tree like this
.
|-- players
| |-- red_alice.plr
| |-- red_bob.plr
|-- resources
| |-- red_cash.rsc
| |-- red_food.rsc
I want to go through all directories and rename any file that starts with "red" to instead start with "blue"...
I think it is work of find and sed commands but I am not sure how to formulate this situation...I know for example I can find files by
find . -name "red*" -type f
then how to use this output to rename red to blue?
bash sed files find rename
add a comment |
I have a directory tree like this
.
|-- players
| |-- red_alice.plr
| |-- red_bob.plr
|-- resources
| |-- red_cash.rsc
| |-- red_food.rsc
I want to go through all directories and rename any file that starts with "red" to instead start with "blue"...
I think it is work of find and sed commands but I am not sure how to formulate this situation...I know for example I can find files by
find . -name "red*" -type f
then how to use this output to rename red to blue?
bash sed files find rename
Tryfind path_to_dir/ -type f -name 'red_*' -exec sh -c 'for f; do echo mv -- "$f" "$f/red/blue"; done' _ +
When satisfied, remove theecho
part
– Valentin Bajrami
Jan 10 at 13:21
add a comment |
I have a directory tree like this
.
|-- players
| |-- red_alice.plr
| |-- red_bob.plr
|-- resources
| |-- red_cash.rsc
| |-- red_food.rsc
I want to go through all directories and rename any file that starts with "red" to instead start with "blue"...
I think it is work of find and sed commands but I am not sure how to formulate this situation...I know for example I can find files by
find . -name "red*" -type f
then how to use this output to rename red to blue?
bash sed files find rename
I have a directory tree like this
.
|-- players
| |-- red_alice.plr
| |-- red_bob.plr
|-- resources
| |-- red_cash.rsc
| |-- red_food.rsc
I want to go through all directories and rename any file that starts with "red" to instead start with "blue"...
I think it is work of find and sed commands but I am not sure how to formulate this situation...I know for example I can find files by
find . -name "red*" -type f
then how to use this output to rename red to blue?
bash sed files find rename
bash sed files find rename
asked Jan 10 at 11:46
DEKKERDEKKER
1
1
Tryfind path_to_dir/ -type f -name 'red_*' -exec sh -c 'for f; do echo mv -- "$f" "$f/red/blue"; done' _ +
When satisfied, remove theecho
part
– Valentin Bajrami
Jan 10 at 13:21
add a comment |
Tryfind path_to_dir/ -type f -name 'red_*' -exec sh -c 'for f; do echo mv -- "$f" "$f/red/blue"; done' _ +
When satisfied, remove theecho
part
– Valentin Bajrami
Jan 10 at 13:21
Try
find path_to_dir/ -type f -name 'red_*' -exec sh -c 'for f; do echo mv -- "$f" "$f/red/blue"; done' _ +
When satisfied, remove the echo
part– Valentin Bajrami
Jan 10 at 13:21
Try
find path_to_dir/ -type f -name 'red_*' -exec sh -c 'for f; do echo mv -- "$f" "$f/red/blue"; done' _ +
When satisfied, remove the echo
part– Valentin Bajrami
Jan 10 at 13:21
add a comment |
2 Answers
2
active
oldest
votes
You can do it with rename command:
find . -type f -name "red*" -exec rename s/red/blue/g "" +;
So it will find the files with name starting with red
and then will put all of them in single command like
rename s/red/blue/g red_first red_second
It will search for files in all directories below the current directory.
Cool. Didn't knowrename
. Also has a option-n
to print what would be done instead of doing it.
– Ralf
Jan 10 at 13:34
Yeah, just verbose kind.
– P_Yadav
Jan 10 at 13:35
This did not change anything and also did not produce any error message..hmmm
– DEKKER
Jan 15 at 9:31
It should work I tried it.
– P_Yadav
Jan 15 at 9:37
@P_Yadav I think the Linux machine I am working on is quite old and this way is not supported
– DEKKER
Jan 21 at 13:36
|
show 1 more comment
Maybe like this:
find . -name "red_*" -exec bash -c 'echo mv "" "$(echo "" | sed "s%/red_%/blue_%" )"' ;
This just prints the commands. Good to check if it would actually work. If you verified that, remove the echo
before mv
.
hmm I get this error 'find missing argument to -exec''
– DEKKER
Jan 10 at 12:16
Did you also add the;
at the end?
– Ralf
Jan 10 at 13:25
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f493688%2frenaming-part-of-name-of-all-files-in-a-directory-tree-if-match%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do it with rename command:
find . -type f -name "red*" -exec rename s/red/blue/g "" +;
So it will find the files with name starting with red
and then will put all of them in single command like
rename s/red/blue/g red_first red_second
It will search for files in all directories below the current directory.
Cool. Didn't knowrename
. Also has a option-n
to print what would be done instead of doing it.
– Ralf
Jan 10 at 13:34
Yeah, just verbose kind.
– P_Yadav
Jan 10 at 13:35
This did not change anything and also did not produce any error message..hmmm
– DEKKER
Jan 15 at 9:31
It should work I tried it.
– P_Yadav
Jan 15 at 9:37
@P_Yadav I think the Linux machine I am working on is quite old and this way is not supported
– DEKKER
Jan 21 at 13:36
|
show 1 more comment
You can do it with rename command:
find . -type f -name "red*" -exec rename s/red/blue/g "" +;
So it will find the files with name starting with red
and then will put all of them in single command like
rename s/red/blue/g red_first red_second
It will search for files in all directories below the current directory.
Cool. Didn't knowrename
. Also has a option-n
to print what would be done instead of doing it.
– Ralf
Jan 10 at 13:34
Yeah, just verbose kind.
– P_Yadav
Jan 10 at 13:35
This did not change anything and also did not produce any error message..hmmm
– DEKKER
Jan 15 at 9:31
It should work I tried it.
– P_Yadav
Jan 15 at 9:37
@P_Yadav I think the Linux machine I am working on is quite old and this way is not supported
– DEKKER
Jan 21 at 13:36
|
show 1 more comment
You can do it with rename command:
find . -type f -name "red*" -exec rename s/red/blue/g "" +;
So it will find the files with name starting with red
and then will put all of them in single command like
rename s/red/blue/g red_first red_second
It will search for files in all directories below the current directory.
You can do it with rename command:
find . -type f -name "red*" -exec rename s/red/blue/g "" +;
So it will find the files with name starting with red
and then will put all of them in single command like
rename s/red/blue/g red_first red_second
It will search for files in all directories below the current directory.
edited Jan 10 at 13:52
answered Jan 10 at 13:31
P_YadavP_Yadav
1,82431024
1,82431024
Cool. Didn't knowrename
. Also has a option-n
to print what would be done instead of doing it.
– Ralf
Jan 10 at 13:34
Yeah, just verbose kind.
– P_Yadav
Jan 10 at 13:35
This did not change anything and also did not produce any error message..hmmm
– DEKKER
Jan 15 at 9:31
It should work I tried it.
– P_Yadav
Jan 15 at 9:37
@P_Yadav I think the Linux machine I am working on is quite old and this way is not supported
– DEKKER
Jan 21 at 13:36
|
show 1 more comment
Cool. Didn't knowrename
. Also has a option-n
to print what would be done instead of doing it.
– Ralf
Jan 10 at 13:34
Yeah, just verbose kind.
– P_Yadav
Jan 10 at 13:35
This did not change anything and also did not produce any error message..hmmm
– DEKKER
Jan 15 at 9:31
It should work I tried it.
– P_Yadav
Jan 15 at 9:37
@P_Yadav I think the Linux machine I am working on is quite old and this way is not supported
– DEKKER
Jan 21 at 13:36
Cool. Didn't know
rename
. Also has a option -n
to print what would be done instead of doing it.– Ralf
Jan 10 at 13:34
Cool. Didn't know
rename
. Also has a option -n
to print what would be done instead of doing it.– Ralf
Jan 10 at 13:34
Yeah, just verbose kind.
– P_Yadav
Jan 10 at 13:35
Yeah, just verbose kind.
– P_Yadav
Jan 10 at 13:35
This did not change anything and also did not produce any error message..hmmm
– DEKKER
Jan 15 at 9:31
This did not change anything and also did not produce any error message..hmmm
– DEKKER
Jan 15 at 9:31
It should work I tried it.
– P_Yadav
Jan 15 at 9:37
It should work I tried it.
– P_Yadav
Jan 15 at 9:37
@P_Yadav I think the Linux machine I am working on is quite old and this way is not supported
– DEKKER
Jan 21 at 13:36
@P_Yadav I think the Linux machine I am working on is quite old and this way is not supported
– DEKKER
Jan 21 at 13:36
|
show 1 more comment
Maybe like this:
find . -name "red_*" -exec bash -c 'echo mv "" "$(echo "" | sed "s%/red_%/blue_%" )"' ;
This just prints the commands. Good to check if it would actually work. If you verified that, remove the echo
before mv
.
hmm I get this error 'find missing argument to -exec''
– DEKKER
Jan 10 at 12:16
Did you also add the;
at the end?
– Ralf
Jan 10 at 13:25
add a comment |
Maybe like this:
find . -name "red_*" -exec bash -c 'echo mv "" "$(echo "" | sed "s%/red_%/blue_%" )"' ;
This just prints the commands. Good to check if it would actually work. If you verified that, remove the echo
before mv
.
hmm I get this error 'find missing argument to -exec''
– DEKKER
Jan 10 at 12:16
Did you also add the;
at the end?
– Ralf
Jan 10 at 13:25
add a comment |
Maybe like this:
find . -name "red_*" -exec bash -c 'echo mv "" "$(echo "" | sed "s%/red_%/blue_%" )"' ;
This just prints the commands. Good to check if it would actually work. If you verified that, remove the echo
before mv
.
Maybe like this:
find . -name "red_*" -exec bash -c 'echo mv "" "$(echo "" | sed "s%/red_%/blue_%" )"' ;
This just prints the commands. Good to check if it would actually work. If you verified that, remove the echo
before mv
.
answered Jan 10 at 12:00
RalfRalf
3428
3428
hmm I get this error 'find missing argument to -exec''
– DEKKER
Jan 10 at 12:16
Did you also add the;
at the end?
– Ralf
Jan 10 at 13:25
add a comment |
hmm I get this error 'find missing argument to -exec''
– DEKKER
Jan 10 at 12:16
Did you also add the;
at the end?
– Ralf
Jan 10 at 13:25
hmm I get this error 'find missing argument to -exec''
– DEKKER
Jan 10 at 12:16
hmm I get this error 'find missing argument to -exec''
– DEKKER
Jan 10 at 12:16
Did you also add the
;
at the end?– Ralf
Jan 10 at 13:25
Did you also add the
;
at the end?– Ralf
Jan 10 at 13:25
add a comment |
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.
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%2f493688%2frenaming-part-of-name-of-all-files-in-a-directory-tree-if-match%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
Try
find path_to_dir/ -type f -name 'red_*' -exec sh -c 'for f; do echo mv -- "$f" "$f/red/blue"; done' _ +
When satisfied, remove theecho
part– Valentin Bajrami
Jan 10 at 13:21