Searching for files and data in Linux
Searching for information in Linux is a key skill that every administrator of Linux systems must master. Various distributions have all the necessary utilities to enable fast and efficient searching. These include the powerful find and grep tools, which allow you to search at the file system level and within text files, providing flexibility and accuracy when working with data.Searching for files with find
The find command in Linux allows you to search for files in two main ways: simplified and full. Simplified mode is used when you only need to specify the location and name of a file. The full mode includes more detailed parameters that allow you to customize the search for specific requirements.A simple variant of the find command has the following format:
find [where we are looking] -name [what we are looking for]
For example, if we need to find all PHP scripts in the cdn_test project directory while in the same directory, the command would be like this:find ./ -name "*.php"
Here ./ denotes the current directory where the search is launched from. If you are outside the project directory, you should specify the full path to the directory:find /var/www/cdn_test/ -name "*.php"
The asterisk (*) plays the role of an operator that replaces any number of characters. This allows you to find files with the extension .php, regardless of their name.The find command with the -name parameter is case sensitive. To search for case insensitive files, the -iname parameter is used.
The full syntax of the find command includes the following elements:
find [where we're looking] [search parameters] [search criterion] [template] [action]
- Where to search: indicates the directory to be searched.
- Search parameters: sets additional conditions, such as search depth.
- Search criterion: it can be file name, date of its creation, access rights, owner and other characteristics.
- Template: a specific value corresponding to the search criterion by which the files will be selected.
- Action: describes what to do with each file found, such as output the name or delete.
File search criteria
In the find command, the search criteria define the conditions by which files or directories are selected.- File name (-name, -iname):
-name - case-sensitive file name search.
-iname - case insensitive search. - File type (-type):
f - regular files.
d - directories.
l - symbolic links. - File size (-size):
Specifies the size of the file (for example, +50M for files larger than 50MB). - Modification time (-mtime, -ctime, -atime):
-mtime - search by last modification date.
-ctime - by date of status change.
-atime - by date of last access. - Permissions (-perm):
Search for files with specific access rights, e.g. -perm 644. - Owner (-user, -group):
-user - search by file owner.
-group - search by owner's group.
Working with found files
After searching for files, you can perform various actions on the found objects.- Output to the screen (-print).
- Deleting files (-delete): be careful: the action is irreversible. Example:
find /path -name "*.log" -delete
- Executing commands (-exec):
find /path -name "*.txt" -exec rm {} \;
deletes all .txt files. - Counting the number of files (-print | wc -l):
find /path -type f | wc -l
- Moving:
find /path -name "*.txt" mv {} /newpath/ \;
- Copying:
find /path -name "*.txt" cp {} /newpath/ \;
Search inside text files using grep
The main advantage of grep is the ability to search for text matches in several files simultaneously. For example, if you want to find lines containing the phrase “Content to export” in all project files, the command would look like this:grep -rn --include="*.php" "Content to export" .
Here we have used several parameters to make the search more precise:- -r is a recursive search that looks through all subdirectories.
- -n - output numbers of lines where a match was found.
- --include="*.php" - search only in files with the extension .php. If this parameter is not used, all files in the specified directory will be searched.
- "Content to export" - the string we're looking for.
- . - directory in which the search is performed (in this case, the current directory and all subdirectories).
The grep command can also perform more complex tasks, such as searching for several different occurrences at the same time. For this purpose, a special syntax is used that allows you to combine patterns: 'pattern 1\|pattern 2'. Here, a backslash (\) escapes the pip character (|), separating it into two separate patterns.
Example of a command to search for multiple occurrences:
grep -rn --include="*.php" "Content to export\|Upload content" .
Find is a powerful utility for searching files, directories, sockets and block devices by various criteria. It allows you to perform actions with found objects: sorting, deleting or copying. In the find command, you can use logical operators such as and, or and not to customize the search more precisely.The special -exec parameter in find allows you to pass the search results to another program for further processing. If find is not enough, you can use the pip operator (|) to create complex chains of commands.
Grep is a tool for searching substrings within files, offering many options for customizing the display of results. It allows you to search for text both in a single file and in multiple files and directories according to specified conditions.
The ability to quickly find the right information on a system is one of the key skills of a Linux administrator.
26 Aug 2024, 19:11:08