This will find all directories named .git, and make sure their parent directories have names ending with .git:
find / -depth -type d -name .git -printf '%h\0' | while read -d "" old ; do
[ ! -d "$old" ] && continue
[ "$old" == "$new" ] && continue
if [ -e "$new" ]; then
echo "$new: Already exists" >&2
mv -vi "$old" "$new"
flag will tell find to descend first, so that all child directories are checked before a parent directory. The -printf '%h\0'
option will output the parent directory name and a NUL separator (zero byte; the only thing you cannot have in pathnames). If using Bash, the -d ""
option to the read builtin will read such NUL-separated pathnames; other shells won't support that.
(If you are not using Bash, use -printf "%h\n" | while read dir ; do
instead. It'll bork if you have directories with a newline in their names, though.)
The first continue test checks if this directory has already been renamed something else. (I think it is impossible for that to happen, but it doesn't hurt to check.)
The second continue test checks if the directory already has an acceptable name.
If the new directory name is still available, the directory is renamed.
This will find empty all directories that have names ending in .git (but not just .git), and create file "file" in it:
find / -depth -type d -name '?*.git' -printf '%p\0' | while read -d "" dir ; do
find "$dir/" -depth -mindepth 1 -delete
The glob pattern ?*.git
matches (anything not empty).git .
The inner find deletes everything in the directory, but not the directory itself (since minimum depth is 1). Although rm
usually feels like the choice for this, it'd either delete the directory itself too, skip any files or subdirectories having a dot in the name, or throw an error about nonexistent files, depending on what you supply to it. If you delete and then recreate the directory, you'll lose any extended attributes. So, surprisingly, find is better for this.
However, I'm utterly at loss at why
you would like to do something like this, and if this actually is what you want to do. I'm afraid you may delete much more than you originally intended..