I would like to rename all directories under a basedir which match a name. For example:
In basedir/
, I have:
- foo/bar/blah
- my/bar/foo
- some/bar/foo1
- other/foo/bar
I would like to rename all directories matching bar
, but I would like to preserve the prefix part.
With find
, I can easily make a list of all the directories like this:
find . -name repositoryunit -type d
However, how can I use -exec mv {} ...
(or perhaps combine with another app) so that the prefix is preserved?
Many thanks in advance!
You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.
Renaming Multiple Directories using mv Command To rename multiple directories with mv you have to use in conjunction with a for loop or while loop. Here's an example of renaming directories with mv using the “for loop”. for d in *; – Start a for loop. do if [ -d “$d” ]; – Check if it's a directory.
find . -depth -name bar -type d -execdir mv {} baz \;
-execdir
changes directory to the parent before executing the command, so the mv
here will be local to each parent directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With