Linux
Posted By Azker

How-To: “rm” command to empty a directory with huge file list


Though I’m a Linux user, even I find my self trapped in such occasion when executing commands. But, I never forget that Google is my friend. I make a note to my self of whatever I find as solution. Therefore, I thought this might help somene like me to make their life easy. : D

May be some you experts already know this. But this is an interesting piece of information for as I was striving to solve my problem. Well, as you know “rm” well known as “remove”, a command in linux terminal to delete files & directories. Though the command says remove, even it has its own restrictions. This is what cause me to find a solution here at Launchpad. Launchpad will be the last place that I’ll ping as I’m very sure that I would find at least a clue on what I’m looking for. Web is owned by Canonical Ltd, who’s also producing the very famous open-source distro named “Ubuntu“.

Okay, now let me come the point. We maintain a virtual private server running on Redhat Enterprise where we use it as our mail distribution. As a daily activity I use to SSH the server if I’m in need of doing any modification, back-up… etc. Once I was trying to delete mails of a user since the mail box is over-quota where the user had more than 3000+ mails. Unfortunately “rm” command returned me with no luck mentioning “bash: /bin/rm: Argument list too long“.

This happened because of the restriction applied for the command. It took me a while to get an answer from launchpad (post can be seen on 2nd parag.). The solution was to execute “rm” command with the help “find” command by piping its input. See below;

find -name *.tmp | xargs /bin/rm

Explaining the command

  • find ~ use to find files/directories
    • -name ~ find using name which explains in the ” “
    • ” “ ~ is to quote & explain what needs to be found
    • * ~ means all (just everything)
    • .tmp ~ all files ends with .tmp
  • | ~ is known as pipe, used to get an output & input to the next execution (e.g.: get the output of find & in put it to the next with xargs
  • xargs ~ used to build and execute command lines from standard input.
  • /bin/rm ~ remove command which will be executed from “bin” directory

Note: Users will have to use the same format by only modifying the field quoted with ” ” according to their convenience.

 


An IT professional living far away from the pearl of Indian ocean. A Telecom nomad moved towards networking & hospitality IT building elegant resort in the romantic island of Maldives. An open-source fanatic, a geek and yes! a minion fan. I prefer football & popcorn ^_^

View Comments
View Comments
  • Imran Mohamed

    Can we use the command

    find -name “*.tmp“ | xargs rm -f

    instead of above sir….?


    • Azker M

      Hey There!

      Yes, you can use that too. Either way it does the same. I’ve shown an example of using the rm command. Even a simple rm will execute when you pipe with xargs. Make sure you use it with xargs for longer arguments.

      Cheerz 😉

      Azker M


This site uses Akismet to reduce spam. Learn how your comment data is processed.