Linux command: find

A quick guide to the find command to find files and folders on the file system

ThisfindCommands can be used to find files or folders that match a specific search pattern. It searches recursively.

Let us learn it by example.

Find all files with the following content under the current tree:.jsExtend the name and print the relative path of each matching file:

find . -name '*.js'

It is important to use quotes around special characters (for example)*Avoid the shell interpreting them.

Find a directory matching the name "src" under the current tree:

find . -type d -name src

use-type fSearch files only, or-type lSearch only for symbolic links.

-namecase sensitive. use-inamePerform a case-insensitive search.

You can search under multiple root trees:

find folder1 folder2 -name filename.txt

Find the directory that matches the name "node_modules" or "public" under the current tree:

find . -type d -name node_modules -or -name public

You can also exclude the path using the following method-not -path:

find . -type d -name '*.md' -not -path 'node_modules/*'

You can search for files containing more than 100 characters (bytes):

find . -type f -size +100c

Search for files larger than 100KB but smaller than 1MB:

find . -type f -size +100k -size -1M

The search file was modified 3 days ago

find . -type f -mtime +3

Search for files edited in the last 24 hours

find . -type f -mtime -1

You can delete all files matching the search by adding the following:-deleteOptions. This will delete all files edited in the last 24 hours:

find . -type f -mtime -1 -delete

You can execute commands for each search result. In this example, we runcatPrint file content:

find . -type f -exec cat {} \;

Note termination\;.{}Fill in with the file name during execution.

Download mine for freeLinux Command Manual


More cli tutorials: