find Usage.

1
2
3
$ find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]
...

Indicates that a file (folder) is found under a path.

  1. Entering a single find will display all files and folders in the current directory.

    1
    2
    3
    4
    5
    6
    7
    8
    
    $ find
    .
    ./app
    ./app/file1.txt
    ./app/file2.txt
    ./file1.txt
    ./file2.txt
    ./file3.txt
    
  2. Search in the specified directory

    Look in the . /app/ directory to find files.

    1
    2
    3
    4
    
    $ find app/
    app/
    app/file1.txt
    app/file2.txt
    
  3. Specify the type of file to find as a directory or file

    Use -type d to find only directories, d is short for directory and finds all directories in the current directory.

    1
    2
    3
    
    $ find . -type d
    .
    ./app
    

    Use -type f to find only files, f is short for file. Find all files in the current directory.

    1
    2
    3
    4
    5
    6
    
    $ find . -type f
    ./app/file1.txt
    ./app/file2.txt
    ./file1.txt
    ./file2.txt
    ./file3.txt
    
  4. Specify file (folder) name search

    Use -name <name> to find files (folders) with the name <name>. Find the file with the name file1.txt in the current directory and subdirectories.

    1
    2
    3
    
    $ find . -type f -name file1.txt
    ./app/file1.txt
    ./file1.txt
    

    Names support wildcards, but must be included in quotation marks.

    1
    2
    3
    4
    5
    6
    
    $ find . -name "file*"
    ./app/file1.txt
    ./app/file2.txt
    ./file1.txt
    ./file2.txt
    ./file3.txt
    
  5. Search by path

    -path means find by path.

    1
    2
    3
    
    $ find . -path "*app/*"
    ./app/file1.txt
    ./app/file2.txt
    
  6. Combined lookup: AND and OR operators

    -or means or, the following command finds the file (folder) with the name file1.txt or the name file2.txt.

    1
    2
    3
    4
    5
    
    $ find . -name file1.txt -or -name file2.txt
    ./app/file1.txt
    ./app/file2.txt
    ./file1.txt
    ./file2.txt
    

    -and means with (and), the following command looks for a folder whose name satisfies file1* and is a folder.

    1
    2
    
    $ find . -name "file1*" -and -type d
    ./file1
    
  7. Ignore case

    Suppose you want to find files of type js and ignore the suffix case, you can do so.

    1
    
    $ find . -name "*.js" -or -name "*.JS" -or -name "*.Js" -or -name "*.jS"
    

    Similarly you can simply add an i-iname before name to indicate that case is ignored.

    1
    
    $ find .-iname "*.js"
    

    All of the previously mentioned options support prefixing i to indicate case-insensitive.

  8. NOT operator

    A more complex operation can be added, using NOT to mean not (no). Find files (folders) with names *.js and *.html but with paths other than *programs*.

    1
    2
    3
    
    $ find . \( -name "*.js" -or -name "*.html" \) -and -not -path "*programs*"
    ./websites/simple/index.html
    ./websites/simple/code.js
    
  9. Delete the results of the search

    You can use -delete to delete the results of a search, for example, to delete all html files in the current repository.

    1
    
    $ find . -name "*.html" -type f -delete
    

    Note that using this option to delete folders does not delete non-empty folders. If you want to delete a folder, for example I want to delete all node_modules in the directory (I often have this need under large front-end projects), you can do so.

    1
    
    $ find . -name node_modules -type d | xargs rm -rf