Home > Linux > recursive find and delete on linux

recursive find and delete on linux

Today I have come across a problem where, I have a folder (say users) and inside users i have folders for each user (Ex Jhon, Seena etc..) and inside each user folder i have a svn folder. (I’m running Ubuntu)

Users ->

Jhon

-> a.svn

seena

-> b.svn

And my requirement was to  remove all the svn folders. So heres how i did that (this code spinet was given one of my collegues). And heres how you can do it

go to the required folder (Ex: Users)

and open up a command prompt

type this command

find . -name “*.svn” -exec rm -rf ‘{}’ \;

and that should do the magic 🙂

So let me explain the command

find . -name “*.svn” -exec rm -rf ‘{}’ \;

find -> will find the files we are requesting

.      -> means the current folder (you can give any folder you want)

-name  -> what is the characteristic to find (in this case its find by name)

“<you files>”  -> name of the files (*.svn means all the files which has .svn extension)

-exec              -> this will execute the next command which we give

rm                -> remove command

-rf                -> r means recursive (since these are folders) and f means FORCE since .svn files are read only

‘{}’               -> this tells the system to get what ever the parameters which sent by our earlier find command

\                 -> do this action recursively

hope this helps 🙂

Advertisements
Categories: Linux Tags:
  1. October 2, 2009 at 9:58 am

    Yep, ‘find’ with -exec is really a handy tool. When configuring servers, it comes very useful with chown and chmod.
    find . -exec chown user:group {} \; will change the ownership of all files and folders.

  2. ilias iliadis
    October 23, 2009 at 3:54 pm

    find . -name “*.svn” -exec rm -rf ‘{}’ \;
    DOES NOT WORK (at least on ubuntu)
    find . -name *.svn -exec rm -rf {} \;
    works

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: