09.10.2021

Searching Linux with find. How to find a file in Linux How to use the find command


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

In this case, the find command is handy. How to use it? Of course, this utility comes with a large man page, but we'll look at some typical cases. Browse the directory tree, starting with the current one, and find the lostfile.txt file:

If you are searching through a large directory tree, the find command can be quite slow. Sometimes it's more convenient to use the locate command. It doesn't look for the file directly in the file system, but looks through its database. This method is much faster, but the database may become outdated. On some distributions, this database is updated every night. You can manually run the updatedb command from time to time to update 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 allow a maximum of 2 errors and use a service character, simply type:

Directory tree overview

Sometimes it is necessary to get an overview of a directory tree. For example, you have 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 methods. Tree (sunsite.unc.edu/pub/Linux/utils/file/tree-1.2.tgz) displays a directory tree as a diagram.

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

You can use a small 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, but for most cases these are sufficient.

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

The standard utilities for finding text strings in files are grep / egrep for normal expression searches and fgrep for searching 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 egrep's search.

A very interesting search program is agrep. Agrep works basically like egrep , but allows you to search based on misspellings. To search for an expression and allow a maximum of 2 misspellings, type:

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

glimpse -i -2 "search expression"

glimpse also allows spelling errors (as does agrep) and -2 indicates that two errors are allowed. glimpse is available on

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

Find command syntax:

Find path -options

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

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

Main options:
-name- search by name, search pattern 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- catalog;
  • c- a special character file;
  • f- normal file;
  • l- symbolic link;
  • p- named pipe;
  • s- socket.

-sizen- 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

This command will search the directory /mnt/usb is the directory where 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 start keyword:

$ find ~ -name "config*" --print

The result will show a list of found files starting with the keyword config.
One of strengths commands 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 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 this purpose 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 search for files that have at least one of the given permission bits set, the rest of the bits will be ignored.

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

Search for files of a specific user:

$ find / -user admin --print

The search will find files owned by 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 particular directory.

Search for symbolic links:

$ find /etc -type l --print

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

Sometimes it is necessary to view the list of nested directories in any directory, for such tasks there is the following command.

View nested directories in a directory:

$ find /mnt/usb -type d --print

The screen will display 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

Files with a size of 2000 kilobytes will be searched, sizes can also be specified in megabytes, for this, instead of the letter “k”, the letter “M” should be indicated.

As the next example, we will use the ability to search for files by their modification time. For this purpose, 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 done in the directory /var/www/html, and files that have changed in 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 a 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 deal with 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 the Linux system in the future. The skills of a competent file search saves your personal time, and as you know, time is priceless. Successes in work!

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 their respective packages, not to the web server itself.

Updated: 02.11.2019 Published: 25.07.2016

The most versatile and functional Linux search command is − find. This article is a cheat sheet with examples of its use.

General Syntax

find<где искать> <опции>

<где искать> — the path to the root directory from where to start the search. For example, find /home/user - search in the appropriate directory. For the current directory, use the dot ".".

<опции> — a set of rules to search by.

* by default, the search is recursive. To search in a specific directory, you can use the option maxdepth.

Description of options

Option Description
-name Search by name.
-iname Case insensitive search by name.
-type

The type of the search object. Possible options:

  • f - file;
  • d - directory;
  • l - link;
  • p - pipe;
  • s is the socket.
-size The size of the object. Specified in blocks of 512 bytes or simply in bytes (with the symbol "c").
-mtime File modification time. Specified in days.
-mmin Change time in minutes.
-atime The time the object was last accessed in days.
-amin Last access time in minutes.
-ctime The last change in the owner or rights to the object in days.
-cmin Last owner or rights change in minutes.
-user Search by owner.
-group By group.
-perm With certain access rights.
-depth The search should not start from the root, but from the most deeply nested directory.
-maxdepth The maximum depth of the directory search. -maxdepth 0 - search only in the current directory. By default, the search is recursive.
-prune Exclude the listed directories.
-mount Don't go to others file systems.
-regex By name with regular expression.
-regextype<тип> The type of the regular expression.
-L Displays the contents of symbolic links (symlinks).
-empty Search for empty directories.
-delete Delete found.
-ls Output as ls -dgils
-print Show found.
-print0 Path to found objects.
-exec<команда> {} \; Run command on found.
-ok Issue a prompt before executing -exec.

The full set of actual options can be obtained with the command man find.

Find Usage Examples

Finding a file by name

find / -name "file.txt"

* this example will search for a file named file.txt throughout the file system, starting at the root / .

Searching for a file by part of its name:

find / -name "*.tmp"

* this command will search for all folders or files in the root directory /, ending in .tmp

Find all files or folders that start with sess_ and end with cd

find. -name "sess_*" -a -name "*cd"

* -a: logical AND, -o: logical OR.

Search by date

1. Search for files that have changed a certain number of days ago:

find. -type f -mtime +60

* this command will find files that have been changed more than 60 days ago.

2. Search for files using newer. This option has been available since version 4.3.3 (you can view it with the command find --version).

a) date of change:

find. -type f -newermt "2019-11-02 00:00"

* will show all files that have changed since 02.11.2019 00:00.

find. -type f -newermt 2019-10-31 ! -newermt 2019-11-02

* will find all files that changed between 10/31/2019 and 11/01/2019 (inclusive).

b) date of application:

find. -type f -newerat 2019-10-08

* all files accessed since 10/08/2019.

find. -type f -newerat 2019-10-01 ! -newerat 2019-11-01

* all files accessed in October.

c) creation date:

find. -type f -newerct 2019-09-07

find. -type f -newerct 2019-09-07 ! -newerct "2019-09-09 07:50:00"

* files created from 09/07/2019 00:00:00 to 09/09/2019 07:50

Type

Search the current directory and all its subfolders for files only:

* f— search only for files.

Search by permissions

We are looking for all the right to read and write:

find / -perm 0666

Find files that only the owner has access to:

find / -perm 0600

File search by content

find / -type f -exec grep -i -H "content" () \;

* in this example, a recursive search for all files in the directory is performed / and a list of those containing the string content.

Sorted by modification date

find /data -type f -printf "%TY-%Tm-%Td %TT %p\n" | sort-r

* command will find all files in the directory /data, appends the modification date to the name, and sorts the data by name. As a result, we get that the files will go in the order they are changed.

find /home/user/* -type d -exec chmod 2700 () \;

* in this example, we are looking for all directories ( type d) in the directory /home/user and set rights for them 2700 .

Scheduled cleaning

team find convenient to use for automatic removal outdated files.

Open cron jobs for editing:

And add:

0 0 * * * /bin/find /tmp -mtime +14 -exec rm () \;

* in this example, we delete all files and folders from the directory /tmp, hundredth older 14 days. The job runs every day at 00:00 .
* we look at the full path to the find executable file with the command which find- in different UNIX systems, it can be located in different places.

This article is an excerpt from the book Linux&Unix - Shell Programming», David Tensley.

I made a few edits in a hurry, if you notice typos, write in the comments.

Often in the course of work, it becomes necessary to search for files with certain characteristics, such as access rights, size, type, etc. The find command is a versatile search tool: it allows you to search for files and directories, view all directories on the system, or just the current directory.

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

Find command options;

Examples of using the various options of the find command;

Examples sharing xargs and find commands.

The possibilities of the find command are extensive, and the list of options offered is also 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, with the appropriate permissions. In such cases, the command is usually run in the background, because browsing the directory tree is time consuming. The general format of the find command is:

find pathname -options

Where pathname 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 written in the variable $HOME home directory of the current user.

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 the specified pattern

Print Write the full names of found files to standard output.

Perm Search for files that have the specified access mode set

Prune Used to prevent the find command from recursively searching a pathname already found; if the -depth option is specified, the -prune option is ignored

User Search for files owned by the 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 the last read and the date last change 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 Search for 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 were created later than the specified file

Type Search files certain type, namely: b- special block file; d- catalog; With- a special character 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 searched first, and only then the entry corresponding to the directory itself is checked

F stype Search for files that are in a file system of a certain 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 analogue of this option is the option -xdev -exec Execute an interpreter command shell for all detected files; executable commands have the format command ( ) ;

(note the space between the symbols () and 😉

Ok Similar to -exec , but prompted before executing the command

2.1.1. Option -name

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

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

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

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

To find all files in the current directory that have at least one uppercase character in their names, type the following command:

$ find . -name "*" -print

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

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

Search in the home directory for all files with an extension .txt, as well as files whose names begin with a dot are produced by the command

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

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

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

$ find / -name "*" -print

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

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

2.1.2. Option -perm

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

$ find . -perm 755 -print

Prefixing the mode value with a hyphen will search for files that have all of the specified permission bits set, ignoring the remaining bits. For example, the following command looks for files that other users have full access to:

$ find . -perm -007 -print

If the mode value is preceded by a plus sign, the search is performed for files that have at least one of the specified permission bits set, while the remaining bits are ignored.

2.1.3. -prune option

When you don't want to search in a particular directory, use the -prune option. It serves as an indication 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 along with the username. For example, searching the home directory for files owned by the user dave is done with the following command:

$ find ~ -user dave -print

Catalog search /etc user-owned files uucp, executes the following command:

$ find /etc -uaer uucp -print

The -nouser option makes it possible to search for files owned by non-existent users. When using it, a search is made for files whose owners do not have an entry in the file. /etc/passwd. You don't need to specify a specific username: the find command does all necessary work herself. To find all files that are owned by non-existent users and are in a directory /home, issue the following command:

$ find /home -nouaer -print

2.1.5. Options -group and -nogroup

The -group and -nogroup options are the same as the options -user-nouser/apps of all files owned by group users 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 accessed X days ago. If the option argument is provided with a '-' sign, files that have not been accessed for a period of time will be selected. X days. The argument with the '+' sign leads to the opposite result - the selection of files that have been accessed during the last X days.

To find all files that have not been updated in the last five days, use the following command:

$ find / -mtime -5 -print

Following is the command to search in a directory /var/admin files that have been updated within the last three days:

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

2.1.7. Option -newer

If you need to find files that were accessed between updates of 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 updated later than age.awk, but before belts.awk, run the following command (using the -exec option is described below):

$ find . -newer 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 within, say, the last two hours, and you don't have a file created exactly two hours ago to compare against? Create such a file! For this purpose, the touch -t command is used, 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, the creation time is -21:40 (it is assumed 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 . -newer 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

Following is the command which is to find all symlinks in a directory /etc.

$ find /etc -type 1 -print

2.1.9. Option -size

During the search, 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 larger than the specified size, -N- less than specified N- is equal to the given one. If the argument additionally contains the symbol With, then the size is considered given in bytes, not blocks, and if the character k- in kilobytes. To search for files larger than 1 MB, use the command

$ find . -aize -flOOOk -print

The following command searches the directory /home/apache files that are exactly 100 bytes in size:

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

To search for files larger than 10 blocks (5120 bytes), use the following command:

$ find . -size +10 -print

2.1.10. Option -depth option

The -depth option allows you to organize the search in such a way that first all files of the current directory (and recursively all its subdirectories) are checked, 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 written to tape first and only then permissions 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

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

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

2.1.12. Search for files with subsequent archiving with the cpio command

The cpio command is primarily used 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 over a pipe.

Here's how directory content is written to tape /etc, /home And /apps:

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

Option -O cpio command sets the mode for writing files to tape. Option -v (verbose- verbal mode) tells the cpio command to report each file it processes.

Notice that the leading '/' is missing from the directory names. In this way, relative path names of the archived directories are set, which, when reading files from the archive later, will allow them to be recreated in any part operating system, not just in the root directory.

2.1.13. Options -exec and -ok

Suppose you have found necessary files and want to perform certain actions on them. In this case, you need the -exec option (some systems only allow ls or ls -1 commands with the -exec option). Many users use the -exec option to find old files to be deleted. I recommend running ls first instead of rm to make sure find finds exactly 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. Consider 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 normal files, which are listed 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 () ;

Care must be taken when moving and deleting files. Use the -ok option, which allows you to run 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 is more than five days old, 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 find command examples

Let's look at a few more examples to illustrate the use of the find command. The following shows how to find all files in your home directory:

$ find ~ -print

Find all files that have a bit set SUID, the following command allows:

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

To get a list of empty files, use the following command:

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

On one of my systems, a system audit log is created every day. A number is added to the name of the log file, which makes it possible to immediately determine which file was created later and which one was created earlier. For example, file versions admin.log numbered sequentially: admin.log.001, admin.log.002 etc. Following 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 command

With the -exec option, the find command passes all found files to the specified command, which are processed in one go. Unfortunately, on some systems the length of the command line is limited, so when processing a large number of files, an error message may be generated, which usually reads: "Too Many Arguments"(too many arguments) or Arguments too long(too big list of arguments). In this situation, the xargs command comes to the rescue. files received from the find command, it processes in portions, and not all at once.

Consider an example where the find command returns a list of all files on the system, and the xargs command executes the file command on them, which checks the type of each file:

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

The following is an example demonstrating how to search for dump files whose names the echo command places in the 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

We complete our list with an example in which the grep command looks for files containing the word " device«:

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

2.3. Conclusion

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


2023
maccase.ru - Android. Brands. Iron. News