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, 2016 at 10:39
  • 1
    and better to dry run the command without the -delete portion for sanity check
    – Sundeep
    Jun 4, 2016 at 10:41
  • 2
    @Saeid Yazdani. ..... Deleting all +1M in / will "delete" your OS : libc.so.** is about 1.8MB ! - Jun 4, 2016 at 10:57
  • @KnudLarsen you are right I meant to be in my home directory and fo "find ./"....I edited the question Jun 4, 2016 at 10:59
  • @spasic thanks, it was what I needed...you want to post as answer? Jun 4, 2016 at 10:59

3 Answers 3

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

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

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 {} \;

Based on the above answers I used below command to clear the server from big log files (after cd'ing into /var/lib/jenkins)

find -type f -name *log -size +500M -delete

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.