10 Basic Find Commands in Linux With Examples

/ Category: Guide, Tips and Tricks / Comments: None

basic find commands in linux
Spread the love

In this tutorial, we are going to explain the most used 10 find Commands in Linux with real examples.

Find command is one of the most used commands in Linux since with the find command we can easily locate files and folders on our server. The find command is executed with a couple of arguments and conditions that can easily locate files by users, groups, size, date and etc.

In this tutorial, we are going to use the latest Ubuntu 22.04 as OS, but you can choose any Linux distro to practice these examples by the “find” command. Let’s get started!

Prerequisites

  • Fresh install of Ubuntu 22.04 OS
  • User privileges: root or non-root user with sudo privileges

Update the System

Before we start with the find command, we are going to update the system packages to the latest versions available.

sudo apt update -y && sudo apt upgrade -y

Once the system is updated, we are ready to show you the basic find commands in Linux.

1. Find files under a specific directory

To find some files located under a specific directory, for example, the html directory executes the following command:

find /var/www/html -name index.html

You should receive the following output:

[email protected]:/var/www/html# find /var/www/html -name index.html
/var/www/html/<strong>index.html</strong>

2. Find files under a specific directory by ignoring Case

find /var/www/html -iname index.html

You should receive the following output:

[email protected]:/var/www/html# find /var/www/html -iname index.html
/var/www/html/<strong>Index.html</strong>
/var/www/html/<strong>index.html</strong>
/var/www/html/<strong>INDEX.html</strong>

3. Find all PHP files

To find all PHP files in some directory, execute the following command:

find . -type f -name "*.php"

You should receive output similar to this:

[email protected]:/var/www/html# find . -type f -name "*.php"
<strong>./config.php
./database.php
./index.php
</strong>

4. Find all files and set the 644 permissions

To find all files in the current directory and set the 644 permissions to execute the following command:

find . -type f -exec chmod 644 {} \;

5. Find all directories and set the 755 permissions

To find all directories in the current directory and set the 755 permissions to execute the following command:

find . -type d -exec chmod 755 {} \;

6. Find the last modified files

To find the last ten days’ modified files in the current directory, execute the following command:

find . -mtime 10

7. Find files bigger than the specified size

To find all files bigger than 50MB, for example, execute the following command

find . -type f -size +50M

You can search in bytes, megabytes, and gigabytes.

8. Find files based on specific user

To find files based on a specific user (www-data) execute the following command:

find . -user www-data

9. Find files based on a specific group

To find files based on a specific group (www-data) execute the following command:

find . -group www-data

10. Man Command for find

Finally, the last command will be the man for find, which is a very useful command if you want to know everything about the find command and the parameters that can be used. Execute the man find, and you will receive the following output:

[email protected]:# man find
FIND(1)                                                               General Commands Manual                                                               FIND(1)

NAME
       find - search for files in a directory hierarchy

SYNOPSIS
       find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]

DESCRIPTION
       This  manual  page  documents the GNU version of find.  GNU find searches the directory tree rooted at each given starting-point by evaluating the given ex‐
       pression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false  for  and
       operations, true for or), at which point find moves on to the next file name.  If no starting-point is specified, `.' is assumed.

       If  you  are  using  find  in  an  environment where security is important (for example if you are using it to search directories that are writable by other
       users), you should read the `Security Considerations' chapter of the findutils documentation, which is called Finding Files and comes with findutils.   That
       document also includes a lot more detail and discussion than this manual page, so you may find it a more useful source of information.

OPTIONS
       The  -H, -L and -P options control the treatment of symbolic links.  Command-line arguments following these are taken to be names of files or directories to
       be examined, up to the first argument that begins with `-', or the argument `(' or `!'.  That argument and any following arguments are taken to be  the  ex‐
       pression  describing what is to be searched for.  If no paths are given, the current directory is used.  If no expression is given, the expression -print is
       used (but you should probably consider using -print0 instead, anyway).

That’s it. In this blog post, we explained the 10 basic find commands in Linux with real examples. These commands are very often used by system administrators and users that are familiar with Linux servers. If you find it difficult to use this command, you can always sign up for one of our NVMe VPS plans and submit a support ticket. We are available 24/7

If you liked this post about ten basic find commands in Linux with an example, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below.

Be the first to write a comment.

Your feedback