1

I'm having trouble with the find command in bash. I'm trying to find a file that ends with .c and has a file size bigger than 2000 bytes. I thought it would be:

find $HOME -type f -size +2000c .c$

But obviously that isn't correct.

What am I doing wrong?

3 Answers 3

6

find $HOME -type f -name "*.c" -size +2000c

Have a look to the -name switch in the mane page:

-name pattern
              Base of  file  name  (the  path  with  the  leading  directories
              removed)  matches  shell  pattern  pattern.   The metacharacters
              (`*', `?', and `[]') match a `.' at the start of the  base  name
              (this is a change in findutils-4.2.2; see section STANDARDS CON‐
              FORMANCE below).  To ignore a directory and the files under  it,
              use  -prune; see an example in the description of -path.  Braces
              are not recognised as being special, despite the fact that  some
              shells  including  Bash  imbue  braces with a special meaning in
              shell patterns.  The filename matching is performed with the use
              of  the  fnmatch(3)  library function.   Don't forget to enclose
              the pattern in quotes in order to protect it from  expansion  by
              the shell.

Note the suggestion at the end to always enclose the pattern inside quotes. The order of the options is not relevant. Have, again, a look to the man page:

EXPRESSIONS
       The  expression  is  made up of options (which affect overall operation
       rather than the processing of a specific file, and always return true),
       tests  (which  return  a  true or false value), and actions (which have
       side effects and return a true or false value), all separated by opera‐
       tors.  -and is assumed where the operator is omitted.

       If the expression contains no actions other than -prune, -print is per‐
       formed on all files for which the expression is true.

So, options are, by default, connected with and -and operator: they've to be all true in order to find a file and the order doesn't matter at all. The order could be relevant only for more complicated pattern matching where there are other operators than -and.

2
  • Thank you, as an aside, does the order of commands matter in a find statement. Could I type find $HOME -name "*c" -type f -size +2000c if I wanted to?
    – Unknown
    Oct 23, 2012 at 15:30
  • 1
    @BernieMacinflor, don't be scared of man pages. Edited the answer with the answer to your comment. ;)
    – Zagorax
    Oct 23, 2012 at 15:38
-1

Try this:

find $HOME -type f -size +2000c -name *.c
-1

Try the following:

find $HOME -type f -size +2000c -name *.c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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