7

I have a file structure that I had to recover and it contains :2f in the name

For example directory name anyname:2fmorename

I want to be able to change :2f to _ in all directories and sub directories.

I thinking something like

find . -name *:2f* -type d -execdir mv{} *_* \;

But I can not figure out for sure.

2
  • 6
    be careful if you use find, to use -depth, or else you may rename a parent directory before finding a child one
    – Jeff Schaller
    Commented Jul 11, 2017 at 15:23
  • If you ever want to test something like your proposed find command, there's nothing stopping you using echo mv instead of mv. Commented Jul 11, 2017 at 23:46

2 Answers 2

6

If you have the Perl rename installed you can do it with this:

find . -name '*:2f*' -type d -exec rename 's/:2f/_/g' {} \;

If you want to test it first then add an echo:

find . -name '*:2f*' -type d -exec echo rename 's/:2f/_/g' {} \;
4

Thanks I ended up getting it to work using the following:

find /path -depth -type d -name '*:2f*' -execdir bash -c 'mv -v "$1" "${1//:2f/_}"' _ {} \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .