8

I want to clean up my server from large log files and backups.

I came up with this:

find ./ -size +1M | xargs rm

But I do not want to include mp3 and mp4. I just want to do this for log and archive files (zip, tar, etc.)

How will the command look like?

  • 2
    try find / -type f \( -name "*zip" -o -name "*tar" -o -name "*gz" \) -delete .. add whatever extension you want to delete – Sundeep Jun 4 '16 at 10:39
  • 1
    and better to dry run the command without the -delete portion for sanity check – Sundeep Jun 4 '16 at 10:41
  • 2
    @Saeid Yazdani. ..... Deleting all +1M in / will "delete" your OS : libc.so.** is about 1.8MB ! - – Knud Larsen Jun 4 '16 at 10:57
  • @KnudLarsen you are right I meant to be in my home directory and fo "find ./"....I edited the question – Saeid Yazdani Jun 4 '16 at 10:59
  • @spasic thanks, it was what I needed...you want to post as answer? – Saeid Yazdani Jun 4 '16 at 10:59
12
find -type f \( -name "*zip" -o -name "*tar" -o -name "*gz" \) -size +1M -delete
  • the \( \) construct allows to group different filename patterns
  • by using -delete option, we can avoid piping and troubles with xargs See this, this and this
  • ./ or . is optional when using find command for current directory


Edit: As Eric Renouf notes, if your version of find doesn't support the -delete option, use the -exec option

find -type f \( -name "*zip" -o -name "*tar" -o -name "*gz" \) -size +1M -exec rm {} +

where all the files filtered by find command is passed to rm command

  • 1
    It's worth noting that not all versions of find support -delete, though you could still avoid xargs with -exec rm {} + – Eric Renouf Jun 4 '16 at 11:43
7

If you want to exclude files by name, you can use this syntax:

find . -type f ! -name '*.mp3' ! -name '*.mp4' -size +1M -delete

or if your find does not support delete:

find . -type f ! -name '*.mp3' ! -name '*.mp4' -size +1M -exec rm {} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.