Linux find File using the command-line [8 Different Ways]

When working with Linux systems, you will often find yourself in a situation where you need to look/find particular files on your system. If you are on a Graphical system, you can quickly type the file name on the search bar to retrieve it. Unfortunately, this method might not be quite efficient for finding files inside multiple folders or even on a headless system such as a remote server. A solution for that is to use the find command-line utility. This command uses a simple syntax to filter files recursively and can be used with the exec command to retrieve and process a file right away.

The find command allows you to search/ lookup files and directories by name, size, ownership, type, date modified, and much more. You can also use it with other commands like grep and sed to get a more filtered output. Let's dive in.

Advertisement

 

Find Command Syntax

When you do a simple lookup online or run the man find command on the Terminal, you will realize that different people use different syntaxes of the find command depending on the scenario. After making some comparisons, we found the general syntax should be as follows.

find [options] [path...] [expression] [what_to_find]

Let's dissect this command.

  • find: Here, we are simply calling the find command.
  • [options]: This parameter is used to handle symbolic links, optimizations, and debugging options.
  • [path ...]: This parameter defines the location of the directory where you want to look up your file.
  • ]expression]: This is where you add your options, actions, and search patterns.
  • [what_to_find]: Here, you specify the file name you are looking for.

Let's write a simple find command that utilizes the syntax above.

find -L /etc/apache2/sites-available -name "*.js"

Linux find File using the command-line [8 Different Ways]

  • -L: This parameter tells the find command to follow symbolic links.
  • /etc/apache2/sites-available: This is the location where we want to look up the file.
  • -name "*.js": This is an expression to find all files that end with the .js extension.

Let's now look at more practical examples of the find command.

 

Find Files by Name

That is probably the most utilized method by users to lookup files on Linux. That's because it is relatively simple as you only need to pass the name of the file you are searching for. Let's look at a simple example.

find /var/www/wordpress/ -type f -name wp-config.php

Linux find File using the command-line [8 Different Ways]

Let's look a the command above:

Advertisement
  • -type f: This parameter specifies that we are looking for a file and not a directory.
  • -name wp-config.php: This expression specifies we are looking for a file called 'wp-config.php.'

Linux find File using the command-line [8 Different Ways]

 

Find Files by Extension

Finding files by looking up their extension name is not very different from 'Finding files by name.' The only difference is that instead of passing the file's name, we will use a wildcard to specify the extension. Let's take a look at the command below.

find /var/www/ -type f -name "*.js"

Linux find File using the command-line [8 Different Ways]

  • /var/www/: That is the directory where we want to search for files.
  • -type f: This parameter specifies that we are looking for files and not directories.
  • -name "*.js": This expression stipulates that we are looking for files with the extension name '.js.'

Alternatively, you can also use this method to list all files that don't match the specified regex by using the -not parameter. For example, we will use the command below to list all files that don't have the '.js' extension.

find /var/www/ -type f -not -name "*.js"

Linux find File using the command-line [8 Different Ways]

 

Find Files by Type

Before diving deep into this topic, we need to understand the different types of files in Linux systems.

  • d: Directory
  • f: A regular file
  • l: Symbolic link
  • c: character devices
  • s: socket
  • b: block devices
  • p: named pipe (FIFO)

For example, to list all directories in the current working directory, w2e would execute the command below.

find . -type d

Linux find File using the command-line [8 Different Ways]

Execute the command below to list all regular files in the current working directory.

find . -type f

Linux find File using the command-line [8 Different Ways]

find /tmp/workspace -type d -exec chmod -v 777 {} \;
find /tmp/workspace -type f -exec chmod -v 777 {} \;

Linux find File using the command-line [8 Different Ways]

NOTE:
We have included the -v (verbose) parameter to see what is happening behind the scenes.

 

Find File by Size

We will use the -size parameter to lookup files depending on their size. Before diving deeper, let's look at the different size parameters used in Linux systems.

  • b: 512-byte blocks (default)
  • c: Size in bytes
  • w: two-byte words
  • k: Size in kilo-bytes
  • M: Size in Megabytes
  • G: Size in Gigabytes

As a practical example, let's look for a file in our /tmp/workspace directory of size 4149 bytes in size.

find /tmp/workspace/ -type f -size 4149c

Linux find File using the command-line [8 Different Ways]

To look for a file 2MB or 2GB in size, we would use the command below.

find /tmp/workspace/ -type f -size 2M
find /tmp/workspace/ -type f -size 2G

Even more interesting, you can search for files greater than or less than a specified size using the minus (-) and plus-sign(+).

For example, the command below checks for files less than 2MB. You will notice we have put a minus sign before M.

find /tmp/workspace/ -type f -size -2M

We will use the command below to list all files greater than 2MB. You will notice we have put a plus sign before 2M.

Advertisement
find /tmp/workspace/ -type f -size +2M

Additionally, you can search for files within a given range. For example, the command below looks for files greater than 1MB but less than 10MB.

find /tmp/workspace/ -type f -size +1M -size -10M

 

Find Files by Modification Date

That is one of the most powerful features you will find in the find command. You can look up files depending on the time they were last modified. Like the size parameter, we will also use the plus sign (+) for greater than and minus (-) for less than in this section.

For example, we will use the command below to get all the files with a '.js' extension modified within the last five days in the /var/www directory.

find /var/www/ -name "*.js" -mtime 5

Additionally, we can use -daystart option. For example, we will use the command below to get all files modified 30 or more days ago in the home directory.

find /home/ -name "*.js" -mtime +30 -daystart

Linux find File using the command-line [8 Different Ways]

 

Find Files by Permission

You can search for files by the assigned permissions using the -perm parameter. See the command below.

find /var/www/ -perm 644

Linux find File using the command-line [8 Different Ways]

You can use two prefixes - slash (/) or minus (-) to control the output.

Advertisement
  • Slash (/): When used, at least one category (owner, group, others) must possess the set permissions.
  • Minus (-): That means all the categories (owner, group, others) must possess all the specified permissions.

Let's explain that in a practical example.

All the files in our /var/www directory have a permission of 644. If we run the command find /var/www/ -perm /744, we will still get an output, and that's because the group and others have read permissions.

Linux find File using the command-line [8 Different Ways]

However, when we run the command find /var/www/ -perm -744 we won't get any output since all the files have a permission of 644, but in our command, we are looking for files with exactly permission 744.

Linux find File using the command-line [8 Different Ways]

 

Find Files by Ownership

You can also lookup files depending on the owner using the -user and -group parameters. For example, we will run the command below to search for all files owned by the ubuntu-user in the /tmp directory.

find /tmp/ -user ubuntu-user -type f

Linux find File using the command-line [8 Different Ways]

As a real-world example, let's lookup all files owned by user ubuntu-user and assign them to user johndoe. See the command below.

find / ubuntu-user -type f --exec chown johndoe {} \;

 

Find and Delete Files

Find also allows you to lookup files and delete them automatically using the -delete parameter. In the command below, we are searching for any '.js' files in the /var/www directory and deleting them.

Advertisement
find /var/www/ -type f -name "*.js" -delete
NOTE:
: Please use the -delete parameter with extreme caution. If you place it before you have specified the files you are searching for, it will permanently delete all the files in the specified path. The -delete parameter can only delete empty directories, similar to the rmdir command when used on directories.

 

Conclusion

That's it! This post has given you a step-by-step guide on using the find command to lookup files on your Linux system. You can also check the find man page for any additional information that might have been left out. Do you have any questions regarding the above topic? If yes, please feel free to hit the comments below.

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X