BenFradet
BenFradet

Reputation: 453

renaming directories based on a pattern

I'm currently trying to rename all directories containing 123 to 321.
For example, if I have a directory called test123test, I would like to rename it to test321test.

So far, I have something which would look like:
find . -depth -name *123* -exec mv {} that's where I'm having a problem\;
Because I don't see how I would be able to replace the folder's name based on the original one.

If you have any idea on how to proceed, feel free to contribute.

Upvotes: 0

Views: 78

Answers (1)

Zsolt Botykai
Zsolt Botykai

Reputation: 51663

Try with a for loop:

for dir in `find . -type d -depth X -name '*123*'` ; do
    mv "$dir" "${dir/123/321}"
done

Note: you need to edit X in the above. And it might fail if there are paths like './dir123/dir123/'

Upvotes: 2

Related Questions