Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename multiple directories matching pattern

Tags:

linux

bash

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!

like image 939
carlspring Avatar asked Aug 02 '13 23:08

carlspring


People also ask

How do I batch rename multiple folders?

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.

How do I rename multiple folders?

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.


1 Answers

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.

like image 176
John Kugelman Avatar answered Sep 19 '22 00:09

John Kugelman