16

How can I tell ( if I can tell !) find to find files of both the directory and file type?

find -type fd

It is like the heuristic dark ages up in here.

1
  • 1
    Typing the find command using that syntax results in this error on Ubuntu: find: Must separate multiple arguments to -type using: ',' ... for Google. Jan 15, 2021 at 21:38

3 Answers 3

21

Here is the command you can use:

find -type f -or -type d
6
  • 7
    Or find -type f -o -type d for POSIX compliant alternative.
    – manatwork
    Nov 3, 2012 at 14:54
  • Hey I ran into a little trouble with this when I run $find . -type d -or -type f -and -iname "vision" it does not do exactly what I expect, am I missing something else! Nov 3, 2012 at 15:56
  • 2
    -a has precedence over -o, use parenthesis (but escape them for the shell). No need to use the non-standard -or or -and. Nov 3, 2012 at 16:52
  • if I try ` find . -type d or -type f -name 'abc'. I get an error.. find: paths must precede expression:`.. Any idea why?
    – alpha_989
    Jul 1, 2018 at 21:38
  • 1
    @alpha_989 missing - before or? Jul 1, 2018 at 21:41
4

tl:dr

use find . -name "*string*" -type f -o -name "*string*" -type d

explanation

the -o command ors the arguments after the filepath completely, such that find . -name "*string*" -type f -o -type d computes find . (-name "*string*" -type f) -o (-type d). For this reason, you must specify the

Most users will want something that looks like

find . -name  "*string*" -type f -o -name "*string*" -type d

which computes as

find . (-name  "*string*" -type f) -o (-name "*string*" -type d)

find syntax details

-name "*string*" searches for names that contain the string string anywhere in them.

4

If you're using the GNU find then the following solution might suit you:

find -type d,f

See man find for more details:

To search for more than one type at once, you can supply the combined list of type letters separated by a comma , (GNU extension).

3
  • 3
    Using GNU find version 4.7.0.. It doesn't seem to support ,. The error message says that find: Arguments to -type should contain only one letter.
    – alpha_989
    Jul 1, 2018 at 21:37
  • This works on Ubuntu for me, however, for some reason using -type f -or -type d or POSIX version -type f -o -type d seems to ignore my ! -path parameter a.k.a. -not -path ... Beware to anyone who combines these two parameters, because you might accidentally find/delete files. Use -type d,f instead... Jan 15, 2021 at 21:33
  • 3
    @JesseNickles you need to use parens (escaped) to group the OR clauses: find ... -not -path ... \( -type f -o -type d \). If you don't specify an operator then -a (AND) is implied. Since AND binds more tightly, you end up with unexpected behavior in your case.
    – zackse
    Jan 16, 2021 at 1:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .