I need a command to list files. But I have three problems:

  1. I want files ending with .HTML and .HTM
  2. If any file has parts like: google docs or form in the name, I want to exclude it.
  3. I want just files bigger than 1kb

By this moment I got this command:

ls *.html *.htm | grep -v 'backup\|form\|docs\|google'

It's working quite well. It's excluding words with: backup\|form\|docs\|google - but I want to exclude files SMALLER than "1kb".

All scripts that I've found to show files "bigger" than {size} are using "find". Anybody has any idea?

  • Can't you use find? I think that is the better option. If not you could parse the output of ls -lh command searching for filesize bigger than 1kb. Talking about your command, why don't you reduce the first part to ls *.htm* – migrc Nov 10 '15 at 12:41

Using GNU find:

find . -size +1k -type f -iregex '.*\.html?$' \
    -not -iregex '.*\(backup\|form\|docs\|google\).*'

This finds all files over 1k with names matching the regex .*\.html?$ but not maching the regex .*\(backup\|form\|docs\|google\).*

If you want files only in the current directory, and not subdirectories, use the -maxdepth 1 option.

find . -maxdepth 1 -size +1k -type f -iregex '.*\.html?$' \
    -not -iregex '.*\(backup\|form\|docs\|google\).*'

You can do whatever you need to with that file list, e.g. by adding an -exec ... option, or -print0 and pipe to xargs -0r.


find . -size +1k | grep '.htm' | grep -v 'backup\|form\|docs\|google'

  • 1
    Your find command could use -name, -a, and -not with some grouping to do all the matching without needing the extra processes – Eric Renouf Nov 10 '15 at 13:13
  • It could, yes. I generally try to use many simple commands rather than few complex ones to make them easy for my brain to understand. Processing power is generally in greater supply than brain power in my office at least :) – David King Nov 10 '15 at 13:15
  • 1
    1. this find will of course find all files in sub-directories as well; 2. the grep will match .htm anywhere in the name, not just at the end; 3. you should deal with the ./ at the front of the file name; 4. as pointed out above, you could probably do the whole thing using find alone (but I like your reasoning - harks back to the true Unix™ philosophy ... although, once you have it working, it's ok to optimise) – Murray Jensen Nov 10 '15 at 14:35
  • 1
    PS: 5. I think it might be more portable to use egrep rather than use the backslash version for alternation – Murray Jensen Nov 10 '15 at 14:38

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.