Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

With the exception of certain file extensions. (removing junk other than subtitle files from a video directory)

share|improve this question

3 Answers 3

find . -type f ! -name '*.ext' -size -20M -delete

is more easy to type and remember than anything else. You have to remember that -delete is position dependent, and deletes without further notice and trashcan.

Just be careful, backup often, and test before deleting:

find . -type f ! -name '*.ext' -size -20M 
share|improve this answer

First, run

find . -type f ! -name '*.sub' ! -name '*.srt' -size -20M -print  

to check that it mathes only file types that you want. I'm a long time unix user and I always do this before asking any command to remove anything (typos always happen at the worst possible time). Then edit the previous command line to become

find . -type f ! -name '*.sub' ! -name '*.srt' -size -20M -print0 | xargs -0 /bin/rm -f  

and run it. It should do what you want. You can repeat the ! -name '*.srt' part more times if you need to exclude more files. Be careful to include the single quotes: they are important to prevent the shell from doing filename expansion.

share|improve this answer
I'd argue -exec rm {} \; or -delete is cleaner than piping through xargs –  Oli Nov 3 '10 at 22:22
I'd argue that using -delete or -exec rm '{}' \; is more error prone than using xargs -0: -delete is position dependent so you can inadvertently delete more than you intended and -exec rm '{}' \; doesn't specify how it treats unusual filenames. –  Li Lo Nov 3 '10 at 23:08
@Oli: and {} needs to be quoted so to avoid expansion by the shell –  Li Lo Nov 3 '10 at 23:10
find would be best stripped of any action other than -print0. It single-handedly breaks most all the Unix principles of "do one thing, do it well, interact with other programs". (Yeah, I know how many legacy scripts depend on -exec, so then I recommend that people pretend it doesn't exist) –  msw Nov 4 '10 at 3:12
@Li Lo: No, {} doesn't need quotation. Try it. –  user unknown Mar 19 '11 at 23:45


find . -type f -name *.bak -size -20M -exec rm {} \;

with -name *.bak you are looking for files with extension "bak", -type f is looking for files (not directories), -size -20M give you all files smaller than 20MB (-size +20M for the reverse search of files bigger than 20MB) and with the -exec you are removing the results of this search.

share|improve this answer
You should quote the pattern as well as {} to prevent the shell from expanding them. –  Li Lo Nov 3 '10 at 22:09
No, you only need to quote the "*.bak", not the {}. And instead of -exec rm {} \; you can just use -delete. And the question was not to delete files of some extension. –  user unknown Mar 19 '11 at 23:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.