Renaming part of name of all files in a directory tree if match

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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?










share|improve this question






















  • 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















0















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?










share|improve this question






















  • 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













0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 10 at 11:46









DEKKERDEKKER

1




1












  • 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
















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










2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer

























  • 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











  • 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


















0














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.






share|improve this answer























  • 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










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
);



);













draft saved

draft discarded


















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









1














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.






share|improve this answer

























  • 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











  • 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















1














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.






share|improve this answer

























  • 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











  • 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













1












1








1







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 10 at 13:52

























answered Jan 10 at 13:31









P_YadavP_Yadav

1,82431024




1,82431024












  • 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











  • 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












  • 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













0














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.






share|improve this answer























  • 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















0














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.






share|improve this answer























  • 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













0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?