coding >> Linux pipe find with rm (5,180 views)

find /path/to/dir -name name_of_file_to_match | xargs /bin/rm

I use often the above command to clean a directory from all the back up files created by emacs, so for example if I am developing a site at: /var/www/a-site/ then at the end of the day or at the beginning of a new one I clean up the directory as follow:
find /var/www/a-site -name '*~' | xargs rm
Be careful with the above command, specially if you run it as root and you get wrong the regex you could delete things you didn’t mean to !

Leave a Comment or Trackback from your own site.

9 Responses to “Linux pipe find with rm”

  1. Florian Marcovici says:

    you can achieve the same result like this:
    find . -name '*~' -exec rm {} \;

  2. Florian Marcovici says:

    damn, they modified my ‘ but you get the point :)

  3. Thanks Florian,
    you are absolutely right, rm will be executed for each file discovered by find.
    The curly brackets {} are replaced with the current discovered file.
    The semicolon ; signals the end of the arguments passed to rm
    The backslash \ escapes the semicolon ;

  4. Sweed Test says:

    great site.. yo no what i mean?

  5. For reasons unknown i’m receiving a blank page while i attempt to post a comment,do you recognize the particular reason why its heading?i’m making use of oprea web-browser

  6. Hi……….I found your website through search engine. I like your website contentI’m deeply in love with every single piece of information you post here.I’ll b back often to read more updates…Now this is hghly recommeded post for me. I will surely email this to my friendI am very much impressedThanks…………

  7. Black says:

    @yurikomuro no it doesn’t require more weed.

  8. What if you wanted to run rm with the -i option, so you could approve each file to be rm’ed?

Leave a Reply