Windows.  Viruses.  Laptops.  Internet.  Office.  Utilities.  Drivers

If you often work on the command line on a computer or server where there is no graphical shell, sooner or later you will be faced with the task of searching for files. Fortunately, Linux already has a built-in command that allows you to find a file on the system.

Today we will talk about using the find command - believe me, it is worth learning how to work with this powerful and convenient tool.

How to use the find command

In simple terms, the basic syntax for the find command is as follows:

Find /path parameters filename

Let's understand the capabilities of the team

Search for a file by name

The first parameter is the path. If you have no idea where the file might be hiding, replace the path with / - that's enough. However, searching the entire disk can be quite time-consuming, and if you know where the file may be located, specify the folder to start searching from (for example, ~ to search in the user's home folder).

Now let's move on to the search parameters. There are two options for searching a file by name:

  • name - search for case sensitive file name
  • iname - search case-insensitive file name

It must be remembered that in Linux the case of the file name is important, and if you need to find, for example, the file Ubuntu.txt, then the following command will not give any results:

Find / -name ubuntu.txt

But you can use the iname command and perform a case-insensitive search:

Find / -iname ubuntu.txt

Search by type

The find command allows you to search for more than just files. Here are the types of handles search supports:

  • f - regular file
  • d - directory
  • l - symbolic link
  • c - character device
  • b - block device

For example, to find all directories within the current user's home directory that begin with "config", run the following command:

Find ~ -type d -name config*

Outputting search results to a file

The convenient function of outputting the result of a command to a file will be extremely useful if there are a lot of search results or there is a need to work with them later. For example, in order to save a list of all found configuration files to file conf_search, run the following command:

Find /etc -type f -name “*.conf” > conf_search

Search files by size

This is a very useful option when you are running out of disk space and need to figure out where it went. For example, to find files larger than 1000 MB, run the following command:

Find / -size +1000M

The following size abbreviations can be used:

  • c - bytes
  • k - kilobytes
  • M - megabytes
  • G - gigabytes
  • b - blocks of 512 bytes

These are just a few ways to use a powerful command. More ways can be found in the manual by running the command

This article is an excerpt from the book " Linux&Unix - programming in Shell", David Tansley.

I made the edits a little in a hurry; if you notice any typos, please write in the comments.

Often during work there is a need to search for files with certain characteristics, such as access rights, size, type, etc. The find command is a universal search tool: it allows you to search for files and directories, view all directories on the system, or only the current directory.

This chapter covers the following topics related to using the find command:

find command options;

Examples of using various options of the find command;

Examples sharing xargs and find commands.

The capabilities of the find command are extensive, and the list of options offered is large. This chapter describes the most important of them. The find command can even search disks NFS (Network File System- network file system), of course, if you have the appropriate permissions. IN similar cases the command usually runs in the background because browsing the directory tree is time consuming. The general format of the find command is:

find path_name -options

Where path_name- this is the directory from which to start the search. The '.' character is used to denote the current directory, the / character is the root directory, and the "~" character is the one stored in the variable. $HOME the current user's home directory.

2.1. find command options

Let's dwell on the description of the main options of the find command.

Name Search for files whose names match a given pattern

Print Writes the full names of found files to standard output

Perm Search for files for which the specified access mode is set

Prune Used to prevent the find command from executing recursive search by an already found path name; if the -depth option is specified, the -prune option is ignored

User Search for files owned by a specified user

Group Search for files that belong to a given group

Mtime -n +n Search for files whose contents have been modified less than (-) or more than (+) n days ago; There are also options -atime and -ctime, which allow you to search for files according to the date of last read and the date of last change of file attributes

Nogroup Search for files belonging to a non-existent group for which, in other words, there is no entry in the file /etc/groups

Nouser Finds files owned by a non-existent user, for which, in other words, there is no entry in the file /etc/passwd

Newer file Search for files that are created later than the specified file

Type Search for files of a specific type, namely: b- special block file; d- catalogue; With- special symbol file; p- named pipe; l- symbolic link; s- socket; f- regular file

Size n Search for files whose size is n units; The following units of measurement are possible: b- block size 512 bytes (default setting); With- byte; k- kilobyte (1024 bytes); w- two-byte word

Depth When searching for files, the contents of the current directory are first looked at and only then the entry corresponding to the directory itself is checked

F stype Searches for files that are in a file system of a specific type; Usually the relevant information is stored in a file /etc/fstab, which contains data about the file systems used on the local computer

Mount Searches for files only in the current file system; The equivalent of this option is the -xdev -exec option Execute an interpreter command shell for all detected files; executed commands have the format command ( ) ;

(note the space between the characters () and 😉

Ok Similar to -exec, but displays a prompt before executing the command

2.1.1. Option -name

When working with the find command, the -name option is most often used. It must be followed by a file name pattern in quotes.
If you need to find all files with the extension . txt in your home directory, specify the symbol as the pathname. The name of the starting directory will be extracted from the variable $HOME.

$ find ~ -name "*.txt" -print

To find all files with the extension .txt located in the current directory, use the following command:

$find. -name "*.txt" -print

To find all files in the current directory whose names contain at least one character in uppercase, enter the following command:

$find. -name "*" -print

Find in the catalog /etc files whose names begin with the characters " host", the command allows

$ find /etc -name "hoat*" -print

Search the starting directory for all files with the extension .txt as well as files whose names begin with a dot, the command produces

$ find ~ -name "*.txt" -print -o -name ".*" -print

Option -O is a designation for the logical OR operation. If it is used, in addition to files with regular names, files whose names begin with a dot will be found.

If you want to list all files on the system that do not have an extension, run the command below, but be careful as it can significantly slow down the system:

$ find / -name "*" -print

The following shows how to find all files whose names start with lowercase characters, followed by two numbers and an extension .txt(For example, akh37.xt):

$find. -name » [a-x] [a-x] . txt" -print

2.1.2. Option -perm

The -perm option allows you to find files with a specified access mode. For example, to search for files with access mode 755 (any user can view and execute them, but only the owner has the right to write) you should use the following command:

$find. -perm 755 -print

If you insert a hyphen before the mode value, it will search for files for which all of the specified permission bits are set, and the remaining bits will be ignored. For example, the following command searches for files to which other users have full access:

$find. -perm -007 -print

If a plus sign is entered before the mode value, files are searched for which at least one of the specified permission bits is set, and the remaining bits are ignored.

2.1.3. Option -prune

When you don't want to search a particular directory, use the -prune option. This instructs you to stop searching at the current pathname. If the pathname points to a directory, the find command will not go into it. If the -depth option is present, the -prune option is ignored.

The following command searches the current directory without going into a subdirectory /bin:

$find. -name "bin" -prune -o -print

2.1.4. Options -user and --nouser

To find files owned by a specific user, specify the -user option in the find command, followed by the username. For example, searching the initial directory for files owned by the user dave, is carried out using the following command:

$ find ~ -user dave -print

Search the catalog /etc files owned by the user uucp, executes the following command:

$ find /etc -uaer uucp -print

Thanks to the -nouser option, it is possible to search for files belonging to non-existent users. When using it, a search is made for files whose owners do not have an entry in the file /etc/passwd. There is no need to specify a specific username; the find command does all the necessary work itself. To find all files that are owned by non-existent users and are located in a directory /home, enter this command:

$ find /home -nouaer -print

2.1.5. Options -group and -nogroup

The -group and -nogroup options are similar to the -user-nouser/apps of all files owned by users in the group accts:

$ find /arra -group accta -print

The following command searches the entire system for files belonging to non-existent groups:

$ find / -nogroup -print

2.1.6. Option -mtime

The -mtime option should be used when searching for files that were accessed X days ago. If the option argument is supplied with a '-' sign, files that have not been accessed for a while will be selected. X days. An argument with a ‘+’ sign leads to the opposite result - files are selected that were accessed during the last X days.

The following command allows you to find all files that have not been updated in the last five days:

$ find / -mtime -5 -print

Below is the command that searches the directory /var/adm files that have been updated within the last three days:

$ find /var/adm -mtime +3 -print

2.1.7. -newer option

If you need to find files that were accessed in the time between updates to two specified files, use the -newer option. The general format for its application is as follows:

Newer old_file! -newer new_file

Sign ' ! ‘ is the logical negation operator. It means: find files that are newer than old_file, but older than new_file.

Let's say we have two files that were updated a little over two days apart:

Rwxr-xr-x 1 root root 92 Apr 18 11:18 age.awk
-rwxrwxr-x 1 root root 1054 Apr 20 19:37 belts.awk

To find all files that were updated later than age.awk, but earlier than belts.awk, run the following command (the use of the -exec option is described below):

$find. -new age.awk ! -newer belts.awk -exec Is -1 () ;
-rwxrwxr-x 1 root root 62 Apr 18 11:32 ./who.awk
-rwxrwxr-x 1 root root 49 Apr 18 12:05 ./group.awk
-rw-r-r- 1 root root 201 Apr 20 19:30 ./grade2.txt
-rwxrwxr-x 1 root root 1054 Apr 20 19:37 ./belts.awk

But what if you need to find files created, say, within the last two hours, but you don’t have a file created exactly two hours ago to compare with? Create such a file! The command touch -t is intended for this purpose, which creates a file with a given timestamp in the format MMDChhmm (month-day-hours-minutes). For example:

$ touch -t 05042140 dstamp
$ls -1 dstamp
-rw-r-r- 1 dave admin 0 May 4 21:40 dstamp

The result will be a file whose creation date is May 4, creation time -21:40 (assuming that the current time is 23:40). You can now use the find command with the -newer option to find all files that have been updated within the last two hours:

$find. -new datamp -print

2.1.8. Option -type

OS UNIX And Linux support Various types files. Finding files of the desired type is done using the find command with the -type option. For example, to find all subdirectories in a directory /etc use this command:

$ find /etc -type d -print

To list all files but not directories, run the following command:

$find. ! -type d -print

Below is the command which is designed to find all symbolic links in a directory /etc.

$ find /etc -type 1 -print

2.1.9. Option -size

During the search process, the file size is specified using the -size option N, Where N- file size in blocks of 512 bytes. Possible arguments have the following meanings: +N- search for files whose size is larger than a specified one, -N- less than specified, N- equal to the given one. If the argument additionally specifies the symbol With, then the size is considered specified in bytes, not in blocks, and if the character k- in kilobytes. To search for files whose size exceeds 1 MB, use the command

$find. -aize -flOOOk -print

The following command searches the directory /home/apache files whose size is exactly 100 bytes:

$ find /home/apache -sixe 100s -print

The following command allows you to search for files larger than 10 blocks (5120 bytes):

$find. -size +10 -print

2.1.10. Option Option -depth

The -depth option allows you to organize the search in such a way that all files of the current directory (and recursively all its subdirectories) are checked first, and only at the end - the entry of the directory itself. This option is widely used when creating a list of files to be archived on tape using the cpio or tar command, since in this case the directory image is first written to the tape and only then the access rights to it are set. This allows the user to archive those directories for which he does not have write permission.
The following command lists all files and subdirectories of the current directory:

$find. -name "*" -print -o -name ".*" -print -depth

Here's what the results of her work might look like:

./.Xdefaults ./.bash_logout ./.bash_profile ./.bashrc ./.bash_nistory ./file ./Dir/filel ./Dir/file2 ./Dir/file3 ./Dir/Subdir/file4 ./Dir/Subdir ./Dir

2.1.11. -mount option

The -mount option of the find command allows you to search for files only on the current file system, excluding other mounted file systems. The following example searches for all files with the extension .xc in the current disk partition:

$ find / -name "*.XC" -mount -print

2.1.12. Searching for files and then archiving them using the cpio command

The cpio command is used primarily to write files to and read from tape. Very often it is used in conjunction with the find command, receiving a list of files from it via a pipe.

Here's how to record directory contents to tape /etc, /home And /apps:

$cd/
$ find etc home appa -depth -print | cpio -ov > dev/rmtO

Option -O The cpio command specifies the mode for writing files to tape. Option -v (verbose- verbal mode) is an instruction to the cpio command to report each file being processed.

Please note that directory names do not have a leading '/' character. In this way, relative path names of the archived directories are set, which, when subsequently reading files from the archive, will allow them to be recreated in any part of the operating system, and not just in the root directory.

2.1.13. Options -exec and -ok

Let's assume you have found the files you need and want to perform certain actions on them. In this case, you will need the -exec option (some systems only allow ls or ls -1 commands to be executed with the -exec option). Many users use the -exec option to find old files to delete. I recommend running ls instead of rm to make sure the find command finds the files you want to remove.

After the -exec option, specify the command to be executed, followed by curly braces, a space, a backslash, and finally a semicolon. Let's look at an example:

$find. -type f -exec Xa -1 () ;
-rwxr-xr-x 10 root wheel 1222 Jan 4 1993 ./sbin/C80
-rwxr-xr-x 10 root wheel 1222 Jan 4 1993 ./sbin/Normal
-rwxr-xr-x 10 root wheel 1222 Jan 4 1993 ./sbin/Rewid

This searches for regular files, a list of which is displayed on the screen using the ls -1 command.

To find files that have not been updated in a directory /logs within the last five days, and remove them, run the following command:

$ find /log" -type f -mtime +5 -exec rm () ;

You should be careful when moving or deleting files. Use the -ok option, which allows you to execute the mv and rm commands in safe mode(before processing the next file, a confirmation request is issued). In the following example, the find command finds files with the extension .log, and if a file was created more than five days ago, it deletes it, but first asks you to confirm this operation:

$find. -name "*.LOG" -mtime +5 -ok rm () ;
< rm … ./nets.LOG >? at

To delete a file, enter at, and to prevent this action - n.

2.1.14. Additional examples of using the find command

Let's look at a few more examples illustrating the use of the find command. Below is how to find all the files in your starting directory:

$ find ~ -print

Find all files for which the bit is set SUID, the following command allows:

$find. -type f -perm +4000 -print

To get a list of empty files, use this command:

$ find / -type f -size 0 -exec Is -1 () ;

One of my systems creates a system audit log every day. A number is added to the log file name, which allows you to immediately determine which file was created later and which was created earlier. For example, file versions admin.log numbered sequentially: admin.log.001, admin.log.002 etc. Below is the find command which removes all files admin.log created more than seven days ago:

$ find /logs -name ‘admin.log.1 -atima +7 exec rm () ;

2.2. xargs team

With the -exec option, the find command passes all found files to the specified command, which are processed at one time. Unfortunately, in some systems the length command line limited, so when processing a large number of files you may receive an error message that typically reads: "Too many arguments"(too many arguments) or "Arguments too long"(too large list of arguments). In this situation, the xargs command comes to the rescue. It processes files received from the find command in chunks, rather than all at once.

Consider an example in which the find command returns a list of all the files present on the system, and the xargs command runs the file command on them, checking the type of each file:

$ find / -type f -print I xarge.file
/etc/protocols: English text /etc/securetty: ASCII text

Below is an example that demonstrates searching for dump files whose names the echo command puts into a file /tmp/core.log.

$ find / -name core -print | xarge echo > /tmp/core.log

In the following example, in the directory /apps/audit searches for all files to which other users have full access. chmod command removes write permission for them:

$ find /appe/audit -perm -7 -print | xarge chmod o-w

Rounding out our list is an example in which the grep command searches for files containing the word " device«:

$ find / -type f -print | xarge grep "device"

2.3. Conclusion

The find command is an excellent tool for searching various files using a wide variety of criteria. Thanks to the -exec option, as well as the xargs command, found files can be processed by almost any system command.

While working on any operating system, sometimes there is a need to use tools to quick search this or that file. This is also relevant for Linux, so all will be discussed below possible ways search for files in this OS. Will be presented as tools file manager, and the commands used in "Terminal".

If you need to specify many search parameters to find the file you need, then the command find irreplaceable. Before considering all its variations, it is worth going through the syntax and options. It has the following syntax:

find path option

Where path- this is the directory in which the search will take place. There are three main options for specifying the path:

  • / — search in the root and adjacent directories;
  • ~ — search by home directory;
  • ./ — search in the directory in which the user is currently located.

You can also specify the path directly to the directory itself where the file is supposedly located.

Options find a lot, and it is thanks to them that it is possible to achieve flexible setup search by setting the necessary variables:

  • -name- conduct a search using the name of the element you are looking for as a basis;
  • -user- search for files that relate to a specific user;
  • -group- search for a specific group of users;
  • -perm- show files with the specified access mode;
  • -size n- search based on the size of the object;
  • -mtime +n -n- search for files that have changed more than ( +n) or less ( -n) days ago;
  • -type- search for files of a specific type.

There are also many types of elements you are looking for. Here is their list:

  • b- block;
  • f- ordinary;
  • p- named pipe;
  • d- catalogue;
  • l- link;
  • s- socket;
  • c- symbolic.

After a detailed analysis of the command syntax and options find You can go directly to illustrative examples. Due to the abundance of options for using the command, examples will not be given for all variables, but only for the most used ones.

Method 1: Search by name (-name option)

Most often, users use the option to search the system -name, so that’s where we’ll start. Let's look at a few examples.

Search by extension

Let's say you need to find a file on the system with the extension ".xlsx", which is located in the directory "Dropbox". To do this you need to use the following command:

find /home/user/Dropbox -name "*.xlsx" -print

From its syntax we can say that the search is carried out in the directory "Dropbox" ("/home/user/Dropbox"), and the desired object must have the extension ".xlsx". An asterisk indicates that the search will be carried out on all files of this extension, without taking into account their name. "-print" indicates that search results will be displayed.

Search by file name

For example, you want to search in a directory "/home" file with name "lumps", but its extension is unknown. In this case, you need to do the following:

find ~ -name "lumpics*" -print

As you can see, the symbol used here is «~» , which means the search will take place in the home directory. After option "-name" the name of the searched file is indicated ( "lumps*"). The asterisk at the end means that the search will be carried out only by name, without taking into account the extension.

Search by first letter of name

If you only remember the first letter of the file name, there is a special command syntax that will help you find it. For example, you want to find a file that starts with the letter from "g" before "l", and you don't know what directory it's in. Then you need to run the following command:

find / -name "*" -print

Judging by the “/” symbol that comes immediately after the main command, the search will be carried out starting from the root directory, that is, throughout the entire system. Next, part «*» means that the searched word will begin with a certain letter. In our case from "g" before "l".

By the way, if you know the file extension, then after the symbol «*» you can specify it. For example, you need to find the same file, but you know that it has the extension ".odt". Then you can use this command:

find / -name "*.odt" -print

Method 2: Search by access mode (-perm option)

Sometimes you need to find an object whose name you do not know, but you know what access mode it has. Then you need to use the option "-perm".

It is quite simple to use; you just need to specify the search location and access mode. Here is an example of such a command:

find ~ -perm 775 -print

That is, the search is carried out in the home section, and the objects being searched will have access 775 . You can also write a “-” symbol in front of this number, then the found objects will have permission bits from zero to the specified value.

Method 3: Search by user or group (options -user and -group)

Every operating system has users and groups. If you want to find an object belonging to one of these categories, you can use the option "-user" or "-group", respectively.

Find a file by its username

For example, you need to find in the directory "Dropbox" file "Lamps", but you don’t know what it’s called, you just know that it belongs to the user "user". Then you need to run the following command:

find /home/user/Dropbox -user user-print

In this command you specified the required directory ( /home/user/Dropbox), indicated that you need to look for a file belonging to the user ( -user), and indicated which user this file belongs to ( user).

Searching for a file by its group name

Searching for a file that belongs to a specific group is just as easy - you just need to replace the option "-user" to option "-group" and indicate the name of this group:

find / -groupe guest -print

That is, you indicated that you want to find a file in the system that belongs to the group "guest". The search will occur throughout the system, this is indicated by the symbol «/» .

Method 4: Search for a file by its type (-type option)

Finding an element of a certain type in Linux is quite simple, you just need to specify the appropriate option ( -type) and designate the type. At the beginning of the article, all the type designations that can be used for searching were listed.

For example, you want to find all block files in your home directory. In this case your command would look like this:

find ~ -type b -print

Accordingly, you indicated that you are searching by file type, as evidenced by the option "-type", and then determined its type by putting the block file symbol - "b".

In the same way, you can display all directories in the desired directory by entering the symbol in the command "d":

find /home/user -type d -print

Method 5: Search file by size (-size option)

If all you know about a file is its size, then even that may be enough to find it. For example, you want to find a 120 MB file in a certain directory, to do this, do the following:

find /home/user/Dropbox -size 120M -print

As you can see, the file we need was found. But if you don't know what directory it is in, you can search the entire system by specifying the root directory at the beginning of the command:

find / -size 120M -print

If you know the approximate size of the file, then there is a special command for this case. You need to register in "Terminal" the same thing, just put a sign before indicating the file size «-» (if you need to find files smaller than the specified size) or «+» (if the size of the file you are looking for is larger than the specified size). Here is an example of such a command:

find /home/user/Dropbox +100M -print

Method 6: Find a file by modification date (option -mtime)

There are times when it is most convenient to search for a file by the date it was modified. On Linux, this is done using the option "-mtime". It’s quite simple to use, let’s look at everything using an example.

Let's say in the folder "Images" we need to find objects that have been subject to changes in the last 15 days. Here's what you need to write in "Terminal":

find /home/user/Images -mtime -15 -print

As you can see, this option shows not only files that have changed during the specified period, but also folders. It also works in the opposite direction - you can find objects that were changed after the specified date. To do this, you need to enter the sign before the digital value «+» :

find /home/user/Images -mtime +10 -print

GUI

The graphical interface makes life much easier for beginners who have just installed Linux distribution. This method search is very similar to the one in Windows, although it cannot provide all the benefits that it offers "Terminal". But first things first. So, let's look at how to search for files in Linux using GUI systems.

Method 1: Search through the system menu

Now we will look at how to search for files through the Linux system menu. The actions taken will be performed in the Ubuntu 16.04 LTS distribution, but the instructions are common for everyone.

The find command is one of the most useful and important commands on Linux.
It is installed by default and available on almost all Linux versions. In Linux, everything is stored as files, and it's obviously worth knowing how to look for these files.

Using the find command, you can search for files of interest using a number of search criteria. The criteria can be specified both together and separately, and then actions can be taken with the result obtained. In this tutorial, we are going to describe the find command with usage examples.

1) List all files in the current directory and its subdirectories

To list all the files in the current directory and its subdirectories, we can use:

Alternatively, we can also use 'find. ’ which will give you the same result.

2) Find all files and directories in your current working directory

If you only need to find directories, you can use:

$find. -type d

To find only files and not directories:

$find. -type f

3) List all files in a specific directory

In order to find files from a specific directory you need to enter:

$ find /root

This command will look for all the files in /root directory.

4) Find the file by name in the directory

To search for a file by name in a specific directory, enter:

$ find /root -name "linuxtechi.txt"

This command will look for the linuxtechi.txt file in the /root directory. We can also find all files with the extension .txt:

$ find /root -name "*.txt"

5)Find the file in multiple directories

To search for files in multiple directories, enter:

$ find /root /etc -name "linuxtechi.txt"

With this command, we can look for linuxtechi.txt file in /root & /etc directories.

With this command we can find the linuxtechi.txt file in the /root and /etc directories.

6)Find the file by name, case insensitive

Search for case-insensitive files using -iname:

$ find /root -iname "Linuxtechi.txt"

As a result, you will get all files called linuxtechi.txt. In this case, there may be several files, since linuxtechi.txt will be equal to LINUXTECHI.txt.

7) Find all file types other than those mentioned

Let's assume that we need to find all files other than a certain file type. To achieve this we enter:

$ find /root -not -name "*.txt"

8)Find files based on multiple criteria

We can combine more than one condition when searching for files. Let's assume that we need files with .txt and .html extensions:

$find. -regex ".*\.\(txt\|html\)$"

9)Find files using OR condition

We can also combine several search criteria, which will lead to a search for files based on satisfying one of the conditions. This is done using the OR operator:

$ find -name "*.txt" -o -name "linuxtechi*"

10)Search files based on permissions

To find files with specific access use -perm:

$ find /root -type f -perm 0777

11)Find all hidden files

For search hidden files in the directory enter:

$ find ~ -type f name ".*"

12) Find all files with SGID

To search for files with SGID bits, run the command:

$find. -perm /g=s

13) Find all files with SUID

To search for files with SUID bits we use:

$find. -perm /u=s

14) Find all executable files

For search only executable files enter:

$find. -perm /a=x

15) Find read-only files

In addition, you can use the find command to find read-only files:

$ find /root -perm /u=r

16)Find all user files

To search for files of a specific user, use the following command:

$find. -user linuxtechi

17)Find all group files

To search for files of a specific group, use:

$find. -group apache

18) Find all files of a certain size

If we want to search whose size we know, then we can use -size:

$ find / -size -2M

19)Find all files in a size range

If we are looking for a file whose size we do not know, but we know its approximate size, or we simply need to make a selection of files in a certain size range, then we can enter:

$ find / -size +2M -size -5M

You can use the find command when searching for files larger than, for example, 50 mb:

$ find / -size +50M

20) Find files modified N days ago

For example, we want to locate all the files that have been modified 8 days ago. We can accomplish that using ‘-mtime‘ option in find command

For example, we can find all files edited 8 days ago. This is done using the -mtime command:

$ find / -mtime 8

21)Find files that were accessed N days ago

You can find files that were included 8 days ago using -atime:

$ find / -atime 8

22) Find all empty files and directories

To find all empty files in the system, enter:

$ find / -type f -empty

And to display their directories:

$ find ~/ -type d -empty

23)Find the largest and smallest files

To display a list of the largest or smallest files, we use find in conjunction with sort , and if we need to display the 3 “most-most”, then we also use head .

To output three files from the current directory, enter:

$find. -type f -exec ls -s () \; | sort -n -r | head -3

In a similar way, we can display the smallest files in the current directory:

$find. -type f -exec ls -s () \; | sort -n | head -3

24) Find all files with a certain access and change it to 644 (or something else)

The find command can offer advanced use cases. For example, we can change all permissions of 644 specific files to 777. To do this, we execute:

$ find / -type f -perm 644 -print -exec chmod 777 () \;

25)Find all files that match certain criteria and delete them

Sooner or later you may need to delete certain files. If this happens, then enter:

$ find / -type f -name "linuxtechi.*" -exec rm -f () \;

The above examples perfectly demonstrate the capabilities of the find command, which can greatly simplify the task of finding files.

The need to search for files may arise in almost every operating system that allows you to work with file system. A classic example for us would be operating system Linux, which we will use in console mode. Let's look at the possibilities of searching for files in the system using the console. To search for files in the Linux system, there is a find command, which allows you to perform a fairly flexible search, allowing you to specify additional search criteria. Let's take a closer look at the capabilities of this command.

Find command syntax:

Find path –options

The path is the directory in which to search. For example, these could be values ​​like this:
. – current directory
/ - root directory
~ - home directory

After specifying the path, search options are indicated. Everything looks confusing, but in reality there are no difficulties here.

Main options:
-name- search by name, set the search pattern;
-user- search for files belonging to a specific user;
-group- search for files belonging to a specific group;
-perm- search for files by access mode;
-type- search for files by type, list of types:

  • b- special block file;
  • d- catalogue;
  • c- special symbol file;
  • f- regular file;
  • l- symbolic link;
  • p- named pipe;
  • s- socket.

-size n- search for files with size n units;
-mtime -n +n- search for files by modification date, less (-) or more (+) days ago.

Let's try to search for files, simulating various situations.
Let's find the files on a removable device connected to USB and pre-mounted in the system.

Search files by extension:

$ find /mnt/usb -name "*.mp3" -print

As a result of executing this command, a search will be performed in the directory /mnt/usb– This is the directory in which the device is mounted. The search will be performed on all files (*) with the extension .mp3.
The search mask can be changed quite flexibly; for example, you can set a keyword with which the file name begins and search for it.

Search files by starting keyword:

$ find ~ -name "config*" –print

The result will show a list of found files starting with keyword config.
One of the strengths of the command is the ability to search according to a regular expression. To demonstrate, let's search for files starting with Latin letters from "a" to "j".

Search files by pattern regular expression:

$ find / -name "*" –print

According to the specified pattern, a search will be made for all files in the system starting with the letters “a” to “j”.

Anyone more or less familiar with Linux systems knows that file access modes are a very important matter. Sometimes you need to find files that have specific specified rights; for these purposes, you can use the search with the option –perm.

Search for files with access mode 755:

$find. -perm 775 –print

In the same way, you can make a more flexible search. Let's find files by user group that have full access.

Search for files with permissions for a group:

$find. -perm -070 –print

The hyphen sign can be replaced with a plus sign, this will allow you to search for files that have at least one of the specified permission bits set, the remaining bits will be ignored.

In the following example, consider searching for files belonging to a specific user or group.

Search for files of a specific user:

$ find / -user admin –print

As a result of the search, files belonging to the user will be found admin.

Search for files belonging to a specific user group:

$ find / -group bots –print

Files that belong to the user group will be found bots. In addition, you can search for files whose owners are non-existent users or groups:

$ find / -nouser –print $ find / -nogroup –print

The ability to search for specific file types is also an important functionality. For example, if there is a need to find all symbolic links in a specific directory.

Search for symbolic links:

$ find /etc -type l –print

The search will be performed in the /etc directory, in which all symbolic links will be selected.

It may be necessary to view a list of subdirectories in a particular directory; for such tasks there is the following command.

View subdirectories in a directory:

$ find /mnt/usb -type d –print

The screen will show a list of directories present in the directory /mnt/usb. Now let's move on to the next option, this is the ability to search for files of a set size.

Search files by size:

$find. -size 2000k –print

A search will be made for files with a size of 2000 kilobytes; the sizes can also be specified in megabytes; for this, instead of the letter “k”, you should specify the letter “M”.

As the next example, we will use the ability to search files by their modification time. For these purposes we will use the option –mtime.

Search for files modified in the last 2 days:

$ find /var/www/html -mtime +2 –print

The search will be performed in the directory /var/www/html, and will search for files that have changed during the last 2 days. Perhaps one of the most important and convenient search options. You can also search by modification date from reverse condition. Let's try to find files in the directory that have not changed for 5 days.

Search for files that have not changed for 5 days:

$ find /var/www/html -5 –print

That's all for now, I hope these examples helped you understand this command. Its convenience is presented clearly, and it will not be superfluous to know about its capabilities, especially if you are going to work with Linux system. The skills of competent file search significantly save your personal time, and as you know, time is priceless. Good luck in your work!

If you notice an error, select a piece of text and press Ctrl+Enter
SHARE: