Why result of find command in linux is unsorted?

07
2014-07
  • dspjm

    When I do a find the results are apparently in random order, shouldn't they be sorted by name?

  • Answers
  • David Schwartz

    To return sorted results, find would have to find everything before it could output anything. That would make things much, much slower. If you need the results sorted, you can easily sort them by piping the output of find to sort.


  • Related Question

    find command in Linux to locate pdf files
  • Questioner

    My goal is to find all pdf files on a remote machine, so I resort to the useful command find. So I type

    find ~ *.pdf
    or
    find ~ "*.pdf"
    and I get nothing. I do the same on my machine and I get nothing. I do a regular search from the menu on my machine and I find quite a few pdf files. Would somebody please tell me what am I doing wrong?


  • Related Answers
  • Mitch Dempsey

    find is far from useless. You are just not using it properly.

    try:

    find . -type f -iname '*.pdf'

  • Michael Aaron Safyan

    Take a look at the documentation for the findutils. The find command is an incredibly powerful one and, consequently, has a somewhat complicated interface. You can do what you want with:

    find . -type f -iname '*.pdf' 
    

    The command above means "find within . for entries of type file with a case insensitive name matching *.pdf (and print the name of such matches)". The find command can actually be used to execute commands on the files that are found (instead of or in addition to printing the file names). For your purposes, though, you may find yourself more comfortable with the locate command, which -- assuming you have built up the locate database using updatedb -- makes it very easy to find files. For example:

    locate '*.pdf'
    

    You will also find that the locate command is typically faster than the find command as locate uses an index of filenames (the locate database), whereas find will walk the hierarchy for each invocation.

  • Owen S.

    You're simply missing the predicate that says what you're searching by (e.g. -name.)

    To find in home directory by name:

    find ~ -name \*.pdf
    

    Note that the wildcard * has to be escaped so that the shell doesn't interpret it before find gets its hands on it. Using '*.pdf' and "*.pdf" will have the same effect as \*.pdf.

    To find case-insensitively:

    find ~ -iname \*.pdf
    

    To prune the results to files only (the name expression will probably take care of this for you, but just in case you have any weirdly-named directories):

    find ~ -type f -iname \*.pdf
    

    To make sure find follows symbolic links (I usually want to do this myself when doing searches):

    find ~ -follow -type f -iname \*.pdf
    

    To do something with the files you found: you can dump this to a file using stdout redirection (e.g. tack on > filename at the end), or use the -exec option to run a command (see the man page for details). The latter runs a command on each file at a time, though. it's often faster to let the xargs command pass your found files as arguments to another command, all at once or big chunks at a time. For example, for ad-hoc (but unindexed) greps through header files:

    find ~ -follow -type f -name \*.h | xargs grep -nH "identifier"
    

    And one final extension, to make that last command work properly if you have files & directories with spaces in them:

    find ~ -follow -type f -name \*.h -print0 | xargs -0 grep -nH "identifier"
    
  • Tiberiu Hajas

    take a look at this tutorial

    i think what you want to do is

    find . -name "*.pdf"