Search code examples
terminalgreppipelinebatch-rename

How to rename several directories in all subfolders In Unix terminal


I have a main directory called like "Experiment", where I keep several subfolders like "time_data", then several subfolders "bacterial_strain"and in the end "drug concentration" that is written like "0,5 mM". So the final path is:

/Experiment/time_data/bacterial_strain/0,5 mM

I want to rename all the directories in all subdirectories from "0,5 mM" to "0,7 mM"

I tried to run several different pipelines as:

find . -type d | grep 0,5 | mv "0,5 mM/" "0,7 mM/"

or

grep -r "0,5 mM" "/path_to_Experiment_folder" | mv 0,5\ mM 0,7\ mM

But Terminal gives the same mistake:

mv: rename 0,5 mM to 0,7 mM: No such file or directory

Please, help me, how to change the pipeline in order to do that?


Solution

  • This should work:

    $ find . -type d -name 0,5\ mM -print | while read dir ; do mv -v "${dir}" "${dir/0,5/0,7}" ; done