How to use the find command. How to search for files in Linux using find command

You are probably familiar with the following problem: There is a file, and you do not remember where you put it.

The find command comes in handy in this case. How do I use it? Of course, this utility comes with a large man page, but we'll cover some typical cases. Browse the directory tree starting with the current one and find the lostfile.txt file:

If you are looking in a large tree of directories, the find command can be quite slow. Sometimes it is more convenient to use the locate command. It does not look for the file directly in the file system, but searches its database. This method is much faster, but the database can become out of date. In some distributions, this database is modified every night. You can manually run the updatedb command from time to time to modify it. locate looks for substrings:

The number of spelling errors allowed depends on the length of the filename, but can be set with the -t option. To resolve a maximum of 2 errors and use a wildcard, just type:

Directory tree overview

Sometimes it is necessary to get an overview of a directory tree. For example, you received a new CD-ROM and would like to know what is on it. You can just use ls - R. Personally, for readability, I prefer one of the following ways... Tree (sunsite.unc.edu/pub/Linux/utils/file/tree-1.2.tgz) displays the directory tree as a diagram.

Or use good old find. In the Gnu version of find that usually comes with Linux, it is possible to change the output format to display, for example, the filename and its size:

You can use a little perl routine that works with the ls command to do this sort of thing. It can be downloaded from here: lsperl.gz. There are many other directory tree browsing utilities available, but these are sufficient for most cases.

Search for files by content (search for text strings in files).

The standard utilities for finding text strings in files are grep / egrep for regular expression searches and fgrep for literal strings. To search for an expression in all files in the current directory, simply type:

If you find it difficult to remember these long commands, use a small script that can be downloaded from here: grepfind.gz. The script also removes non-printable characters from the search string so that you don't accidentally get a binary file as a result of an egrep search.

Very interesting program search - agrep. Agrep works basically like egrep, but allows for misspelled searches. To search for an expression and resolve a maximum of 2 spelling errors, type:

After that, you can search for the string in all files that were previously indexed

glimpse -i -2 "search exprission"

glimpse is also misspelling (like agrep) and -2 indicates that two errors are allowed. glimpse is available on

    Find the file by its name. Such simplest search is performed using the find utility. The command below will search for a file in the current directory and all of its subdirectories.

    find -iname "filename"

    • Enter -iname instead of -name to ignore case in the entered filename. The -name command is case sensitive.
  1. Start searching in the root directory. To run a system-wide search, add the / modifier to your query. In this case, the find command will search for the file in all directories, starting from the root.

    find / -iname "filename"

    • You can start searching in a specific directory; to do this, replace / with the path to the directory, for example, / home / max.
    • Can be used. instead of / to search for the file only in the current directory and its subdirectories.
  2. Use a generalization symbol.* to find files whose name matches the part of the request. You can use the wildcard * character to find a file whose full name is unknown, or to find all files with a specific extension.

    find / home / max -iname "* .conf"

    • This command will find all .conf files in the Max user folder (and its subfolders).
    • Use this command to find all files that match part of the query. For example, if you have many WikiHow-related files on your computer, find all the files by typing "* wiki *".
  3. Make it easier to manage your search results. If there are too many search results, it is difficult to find among them desired file... Use the symbol | to filter the search results with less. This will make it easier to view and filter your search results.

    find / home / max -iname "* .conf" | less

    Find specific items. Use modifiers to show only specific items in search results. You can search for regular files (f), directories (d), symbolic links (l), character I / O devices (c), and block devices (b).

    find / -type f -iname "filename"

  4. Filter your search results by file size. If you have many files with similar names on your computer, but you know the size of the file you are looking for, filter the search results by file size.

    find / -size + 50M -iname "filename"

    • This command will find all files larger than 50 MB. Use the + or - modifier to indicate an increase or decrease in size. If there is no + or - modifier, the command will find files that are exactly the same size as the specified size.
    • Search results can be filtered by bytes (c), kilobytes (k), megabytes (M), gigabytes (G), or 512-byte blocks (b). Note that the modifiers shown are case sensitive.
  5. Use logical operators(boolean operators) to combine search filters. You can use the -and, -or, -not operators to combine different search queries in one request.

    find / travelphotos -type f -size + 200k -not -iname "* 2015 *"

    • This command will find files in the "Travelphotos" folder that are larger than 200 KB and which do not have the number 2015 in their names.

This is the default welcome page used to test the correct operation of the Apache2 server after installation on Ubuntu systems. It is based on the equivalent page on Debian, from which the Ubuntu Apache packaging is derived. If you can read this page, it means that the Apache HTTP server installed at this site is working properly. You should replace this file(located at /var/www/html/index.html) before continuing to operate your HTTP server.

If you are a normal user of this web site and don "t know what this page is about, this probably means that the site is currently unavailable due to maintenance. If the problem persists, please contact the site" s administrator.

Configuration Overview

Ubuntu "s Apache2 default configuration is different from the upstream default configuration, and split into several files optimized for interaction with Ubuntu tools. The configuration system is fully documented in /usr/share/doc/apache2/README.Debian.gz... Refer to this for the full documentation. Documentation for the web server itself can be found by accessing the manual if the apache2-doc package was installed on this server.

The configuration layout for an Apache2 web server installation on Ubuntu systems is as follows:

/ etc / apache2 / | - apache2.conf | `- ports.conf | - mods-enabled | | - * .load | `- * .conf | - conf-enabled | `- * .conf | - sites-enabled | `- * .conf

  • apache2.conf is the main configuration file. It puts the pieces together by including all remaining configuration files when starting up the web server.
  • ports.conf is always included from the main configuration file. It is used to determine the listening ports for incoming connections, and this file can be customized anytime.
  • Configuration files in the mods-enabled /, conf-enabled / and sites-enabled / directories contain particular configuration snippets which manage modules, global configuration fragments, or virtual host configurations, respectively.
  • They are activated by symlinking available configuration files from their respective * -available / counterparts. These should be managed by using our helpers a2enmod, a2dismod, a2ensite, a2dissite, and a2enconf, a2disconf. See their respective man pages for detailed information.
  • The binary is called apache2. Due to the use of environment variables, in the default configuration, apache2 needs to be started / stopped with /etc/init.d/apache2 or apache2ctl. Calling / usr / bin / apache2 directly will not work with the default configuration.

Document Roots

By default, Ubuntu does not allow access through the web browser to any file apart of those located in / var / www, public_html directories (when enabled) and / usr / share (for web applications). If your site is using a web document root located elsewhere (such as in / srv) you may need to whitelist your document root directory in /etc/apache2/apache2.conf.

The default Ubuntu document root is / var / www / html. You can make your own virtual hosts under / var / www. This is different to previous releases which provides better security out of the box.

Reporting Problems

Please use the ubuntu-bug tool to report bugs in the Apache2 package with Ubuntu. However, check before reporting a new bug.

Please report bugs specific to modules (such as PHP and others) to respective packages, not to the web server itself.

Undoubtedly, when working with Linux, sometimes it becomes necessary to file search with certain characteristics. These characteristics can be file size or type, permissions, and more.


Linux command find, helps a lot to cope with file search according to various criteria.


Today we will analyze find files in Linux and give the main command options find that you can apply in your work.

Command format find:

findpath-options

where path is the directory in which to search. The following values ​​can be specified as the path:

. - search in the current directory;

/ - search from the root directory;

~ - search in the home directory.

As for the options, there is a much larger list, which it is imperative to read carefully (this will help a lot in the future!). So, basic find command options:

-name- search for files by name using the given template;

-user- search for files belonging to the specified user;

-group- search for files belonging to the specified group;

-perm- search for files with the specified access mode;

-type- search for files of a certain type. There are enough types too:

  • b - special block file;
  • d - directory;
  • c - special character 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, the content of which changed less than (-) or more than (+) days ago.

Consider some examples of using the command find :

The most commonly used option is the option -name which searches for files by name.

Examples with the -name option :

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

will search for all files (indicated by the * sign) with the extension .mp3 on a USB device mounted in the / mnt / usb directory.

$ find ~ -name "test *" -print

will display a list of home directory files starting with test.

If you need to find files starting with certain letters (for example, from a to j), then it will be convenient to use regular expressions here, which are extremely convenient to use:

$ find / -name "*" -print

The above command will find all files on the system starting with letters from a to j.

Search for files with specific access modes

If you need to find files with certain access modes, the option will help you -perm, which will easily help with this.

For example, let's search for files with access mode 775 (owner and group have full rights and other users have write restrictions) located in the current directory:

$ find. -perm 775 -print

Searching with find with the -perm option, you can use another method - you can put a hyphen in front of the mode value and then the search will be made for files for which all the specified permission bits are set. Moreover, the rest of the bits are ignored in this case.

For example, let's find files to which the users of the group have full access:

$ find. -perm -070 -print

Instead of a hyphen, you can specify a plus sign. In this case, a search will be made for files that have at least one of the specified permission bits set. The rest of the bits are ignored.

Search for files of a specific user or group

Finding files for a specific user is extremely simple to implement. To do this, just run the command:

$ find / -user admin -print

The above command will produce file search on the system, owned by the user admin.

To find files belonging to a certain group (for example managers), run the command:

$ find / -group managers -print

To search for files of non-existent users or groups, you can use the options -nouser and -nogroup:

$ find / -nouser -print

$ find / -nogroup -print

Search for files of a specific type

One of the handy features of the team find, is the ability to search for files of a specific type. Consider the use cases for the option -type:

Searching for symbolic links in the / etc directory:

$ find / etc -type l -print

Display a list of directories present in the / mnt / raid directory

$ find / mnt / raid -type d -print

Search for files of a specific size

Option -size allows you to search for files of a certain size and looks like this when executed:

$ find. -size 2000k -print

The above command will find and display on the screen the 2 megabyte files located in the current directory. If, for example, you need to find files less than 500 kilobytes in size, then the command will be as follows:

$ find. -size -500k -print

If you need to find files larger than 600 megabytes, then we will use the command:

$ find / -size + 600M -print

Finding files using the -mtime option

Option -mtime will allow you to find files that have changed over a period of time.

For example, we are faced with the task of finding files located in the / mnt / raid / upload directory and changed over the last 5 days. The command will help us with this:

$ find / mnt / raid / upload -mtime -5 -print

If we need the opposite, for example, to find files that have not changed for a week, use the command:

$ find / mnt / raid / upload -7 -print

Team find is extremely convenient tool for file search and can also be used for file search on NFS disks (network file systems), but in this case it is necessary to take into account that it will take much more time to search for something on NFS than on local disks.

In this article, we have covered only the basic options of the find command that will help you in your work. Use what is convenient and don't worry about trifles!

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

The find command syntax is:

Find path - options

The path is the directory in which you want to search. For example, these can be values ​​of the following form:
... - current directory
/ - root directory
~ - home directory

After specifying the path, this is how the search options are indicated. It looks confusing, but in reality there are no difficulties.

Main options:
-name- search by name, a search template is set;
-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- directory;
  • c- a special character 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 than (-) or more (+) days ago.

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

Search files by extension:

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

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

Search for files by starting keyword:

$ find ~ -name "config *" –print

This will show a list of found files starting with keyword config.
One of strengths commands are the ability to search by regular expression. For demonstration, let's search for files starting with Latin letters from "a" to "j".

Search for files using a regular expression pattern:

$ find / -name "*" –print

According to the specified pattern, all files in the system starting with letters from "a" to "j" will be searched.

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

Search for files with access mode 755:

$ find. -perm 775 –print

A more flexible search can be done in the same way. 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 rest of the bits will be ignored.

In the following example, consider finding files owned by a specific user or group.

Search for files of a specific user:

$ find / -user admin –print

The search will find files belonging to the user 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 owned by non-existent users or groups:

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

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

Finding symbolic links:

$ find / etc -type l –print

The search will be done in the / etc directory, in which all symbolic links will be searched.

Sometimes it is necessary to view the list of subdirectories in a directory, for such tasks there is the following command.

Browse 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 specified size.

Search files by size:

$ find. -size 2000k –print

The search will be performed for files with a size of 2000 kilobytes, sizes can also be specified in megabytes, for this, instead of the letter "k", 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 files that have changed within the last 2 days will be searched. Perhaps one of the most important and convenient search options. You can also search by modification date with the opposite condition. Let's try to find files in the directory that haven't changed in 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 figure out this command. Its convenience is clearly presented, and it will not be superfluous to know about its capabilities, especially if you are going to work with Linux system... Skills of competent search for files significantly save your personal time, and as you know, time is priceless. Good luck in your work!