DEV Community

Discussion on: Command Line Snippets do you keep handy?

Collapse
fboaventura profile image
Frederico Freire Boaventura

Hi!

Just as an advice, take real care with this:

// remove a repo
find . -type f | grep -i ".git" | xargs rm
cd ..
rm -rf 

I've made a quick setup just to illustrate, but let me explain the inner workings of these commands, specially the find and grep combination, that is the dangerous part here.

The tree I have here is:

tree

Where the blue items are folders and the gray are files.

find . -type f will search and print all the files, starting on the actual folder up to all levels downward. And the grep -i ".git" will match to anything that has <any_ character>[gG][iI][tT], at any part of the name.

So, on my special and quick setup, I'll get this result::

» find . -type f | grep -i '.git'
./.git/configure
./.git/HEAD
./folder1/agitos
./.gitignore
./.gitattributes
./agitar/aaa
./agitar/a/1
./agitar/a/2
./agitar/a/3
./agitar/a/4
./agitar/a/5
./agitar/a/6
./agitar/b/1
./agitar/b/2
./agitar/b/3
./agitar/b/4
./agitar/b/5
./agitar/b/6
./agitar/c/1
./agitar/c/2
./agitar/c/3
./agitar/c/4
./agitar/c/5
./agitar/c/6

This is definitely not the expected behavior, and is quite easy to be achieved (believe me :) ). If you pass this result into the xargs rm you may end up removing a lot of undesired files. And you have to take extra care wit this, because asking file by file if you want it to be deleted isn't the default behavior of the rm command. On RedHat, CentOS and Fedora there is an alias rm=rm -i, to make the confirmation mandatory by default.

cd .. is going up one level on the folders, and the rm -rf without any other arguments is doing absolutely nothing, but leaves space for a disaster.

My advice, when you want to remove a repository from a folder, is to make sure you are removing the right folder, so pass the complete path to the rm command, like so:

rm -rf /home/user/dev/my_program/.git

Always, when using rm -rf pass the complete path to the file/folder you want to remove.

Also, while I'm around, the find command is a real swiss knife. To find and remove the .git folder and remove it you can use (but is hardly not advised to):

find /home/user/dev/my_program -type d -name '.git' -delete

You may want to, for example, find all the repositories inside you home folder:

find /home/user -type d -name '.git'

You can also search for folders/files older than, newer than, ..., ..., ...

Have fun and take care! :)

Collapse
jrohatiner profile image
Judith Author

Thank you! That is very very helpful.
Namaste!