/

Linux Commands: Find

Linux Commands: Find

In this blog post, we will explore the powerful find command in Linux, which is commonly used to search for files and folders on the filesystem. The find command allows you to search for files or folders based on specific criteria and can perform recursive searches.

Let’s dive into some examples to understand how to use the find command effectively.

To find all files with the .js extension under the current directory and print their relative paths, you can use the following command:

1
find . -name '*.js'

It’s important to wrap special characters like * in quotes to prevent the shell from interpreting them as wildcards.

If you’re looking for directories with the name “src” under the current directory, you can use the following command:

1
find . -type d -name src

You can modify the search behavior by using the -type option. For example, -type f allows you to search for files only, while -type l searches for symbolic links only.

By default, the -name option is case-sensitive. If you want to perform a case-insensitive search, you can use the -iname option.

You can search in multiple root directories by providing them as arguments to the find command:

1
find folder1 folder2 -name filename.txt

If you need to find directories matching multiple names, you can use the -or operator. For instance, to find directories named “node_modules” or “public” under the current directory, use the following command:

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

In some cases, you may want to exclude certain directories from the search. To do this, you can use the -not -path option. Here’s an example where we exclude the “node_modules” directory:

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

You can also search for files based on their size. For example, to find files larger than 100 bytes, use the following command:

1
find . -type f -size +100c

If you need to search for files within a specific size range, you can combine the -size option with the less than (-) and greater than (+) symbols. For instance, to search for files larger than 100KB but smaller than 1MB, use this command:

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

In addition to searching based on size, you can also search for files based on their modification time. For example, to find files edited more than 3 days ago, use this command:

1
find . -type f -mtime +3

Similarly, to find files edited within the last 24 hours, use this command:

1
find . -type f -mtime -1

If you want to delete all files that match a certain search criteria, you can use the -delete option. For example, to delete files edited within the last 24 hours, use this command:

1
find . -type f -mtime -1 -delete

Lastly, the find command allows you to execute a command on each search result. In this example, we use the cat command to print the content of each file found:

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

Make sure to include the terminating \;, and note that {} will be replaced with the actual file name during execution.

With the find command, you have a powerful tool at your disposal for searching and manipulating files and directories on the Linux filesystem. Experiment with different options and criteria to make the most out of this versatile command.

tags: [“Linux”, “commands”, “find”, “search”, “files”, “folders”, “filesystem”]