Blog Post
How to Find Files in Linux Using Command Line
Commands

How to Find Files in Linux Using Command Line

You know the feeling when you leave your car keys somewhere in the room, but you don’t have the slightest idea where they are? The same happens with files on a PC, except that you can use the “find” function to locate them. Let’s take a look at how to find files in Linux using the command line.

Basic Syntax

We will get to the specifics in a moment, but first, let’s get to know the basics of the “find” utility. Here is how the command will look in most cases:

find option path expression

Here is a detailed explanation of the line:

  • Find – the basic command utility.
  • Option – allowing you to include additional controls to the command.
  • Path – this is where you want to look for the file.
  • Expression – details about what you want to search.

Now, here are some examples of the option you can use for additional parameters:

  • -S – socket
  • -P – named pipe
  • -B – block devices
  • -C – character devices
  • -L – symbolic links
  • -D – folder
  • -F – standard file

Check out an example of the “find” command:

find -L /www/var/ -type "*.html" -exec chmod 666

We used the “-L” parameter which signals the system to follow symbolic links. We also ordered Linux to look for all “.html” files that are located in the “/www/var” directory tree. Finally, we added the parameter that the searched files should have the chmod 666.

Find Files by Type

We already mentioned this command above, so let’s try to use something different now. Let’s say you want to find all folders in the directory you are currently located in. In that case, use this command:

find . -type d

The system will list all folders within your current directory on the screen.

find files by type

You can also search for character devices on the system type:

find / -type c

For the end, here is an advanced example of how the “find” command can be extremely useful. It can order your system to adjust all folder permissions to 755 and file permissions to 644 recursively.

Here is how:

find folder_path -type d -exec chmod 0755 {} \; find folder_path -type f -exec chmod 0644 {} \;

These are essentially two separate commands. With the first part, you changed the chmods of folders while the second part affected files.

Use the Names of Files to Find Them

We showed you a couple of tricks you can use with the “find” command, but the chances are you are here because you want to use a filename to find it. That is why you should use the -name function, and the command should look something like this:

sudo find folder_path -type f -name file_name.txt

In this case, we used the superuser permission to find a particular “file_name.txt” in the “folder_path” directory. You can adjust the name of the file and the extension any way you like.

Would you like to make the search case insensitive? You only need to change one letter:

sudo find folder_path -type f -iname file_name.txt

If you compare the two previous commands, you will see that we used the “iname” option now.

Use the Extension to Find Multiple Files

You might not remember the exact name of the file, but you know the extension and its approximate location. That is when you should consider utilizing the extension to search for multiple files.

find folder/path -type f -name '*.pdf'

The above line will order Linux to display all files that have the “.pdf” extension in the “folder_path.” Please note that it is vital to enter quotes before you specify the extension and use the “*” symbol. Otherwise, the system will misinterpret your command.

Here is how you can put a twist on the command that allows you to find multiple files. How about identifying all those that do not belong to a particular extension?

Here is how we can do that:

find folder/path -type f -not -name '*.pdf'

We used the “-not” option to display files of all the other extensions except for the specified one.

Use the Size of the Files to Find Them

Here are some basic parameters that you need to know when searching for files by size:

  • G– gigabytes
  • M– megabytes
  • c-bytes
  • b– 512-byte blocks
  • w– two-byte words

The chances are that you will not know the exact size of the file, so here is how to search within a particular range:

find folder_path -type f -size +2M

The above line will display all the files larger than two megabytes in the “folder_path.” If you want to search in the current working folder, enter “.” Instead of the directory path.

You can also search for files less than a particular size. If you want to identify files less than 2M, enter “-2M” when you specify the size.

Now, let’s say that you want to find files larger than two megabytes, but smaller than five. Here is how the command should look:

find folder_path -type f -size +2M -size -5M

Finally, let’s see how you can find folders of the exact size. In this example, we use the size of 1MB:

find folder_path -type f -size 1M

When Have You Last Modified the Files?

Another option to look for files on your Linux system is based on when you last modified them. The commands are similar to the ones you used when searching for file types.

Here is a basic example:

find folder_path -mtime 10

The above line will tell the system to display all the files in the particular “folder_path” that you modified within the last 10 days.

You can combine this search with the desired name, type, or format of the file. If you want to look for all recently modified PDF files in the root folder, here is how that would look like:

find /root -mtime 10 -name "*.pdf"

An alternative way of using a modification date to find a file is to use the “-daystart” parameter.

find folder_path -mtime +10 -daystart

The above line orders Linux to show all the files in the specified folder that were modified 10 or more days ago.

Use Permissions to Find Files

The “perm” function will allow you to find files depending on the given permissions. The option may be useful if you want to ensure that you haven’t made a mistake of giving different permissions than planned to a particular file.

Use the following line:

find folder_path -perm 666

The OS will list all the files in the specified folder that have 666 permission. Once you identify one that doesn’t belong to that group, use the “chmod” command to change its permission.

You can also use prefixes that will modify the search in various ways.

The most commonly used prefix is a slash (/). It will tell Linux to show you the files that have the specified permission for one of the categories (others, group, or user).

find folder_path -perm /666

Alternatively, you can also use “-“ as a parameter. For example, let’s say that you want to look for files that have read and write permissions by all the categories (owner, group, others).

find folder_path -perm -666

Use the Owner to Find Files

The last way of finding a file we will show you in this tutorial is by using the owner of the file. You can specify either a user or a group and here is how that looks:

find folder_path -user account_name
find folder_path -group group_name

It is interesting to mention that you can change the ownership of the files with the “find” command. Here is how this advanced command looks:

find folder_path -user account_name -type f -exec chown account_name2 {} \;

The first account name specifies the current owner while you use the “chown” parameter to change the ownership to the second account name.

Delete Files with the Find Command

If you want to shorten the process of deleting the files, you can use the “find” command to remove them.

Take a look at its form:

find folder_path -name "*.pdf" -delete

In this case, you will delete all pdf files in the directory tree of “folder_path.” You should be extremely careful with the delete parameter because you don’t want to end up deleting files that you need.

Wrap Up

You can use the “find” command in Linux in many different ways. We covered the most commonly used ones in this guide. In the end, allow us to remind you that you can always type “man find” to learn more about what you can do with the command.

Related posts
Leave a Reply

Required fields are marked *

Copyright © 2019 Blackdown.org. All rights reserved.