Code

Find in Linux: A Guide to Search Commands

Find in Linux: A Guide to Search Commands

Practical course: "Linux OS Administration"

Learn more

The standard utility for searching in Linux-based systems searches for files and directories exclusively by their names. To improve search accuracy and filter results, it is recommended to use the find utility. This powerful command is available in distributions such as Ubuntu, Fedora, Mint, and others. The find utility allows for more complex queries, including searching by file size, modification date, and various attributes, making it an indispensable tool for administrators and experienced Linux users.

Content plays a key role in the structure and perception of web pages. It not only helps users navigate information but also contributes to a site's SEO optimization. Properly formatted content allows search engines to better index pages and increases their chances of ranking high in search results. To effectively organize content, use clear headings and subheadings, and structure the text so it's easy to read. Ensure that keywords are placed naturally and logically, which helps improve the visibility of your page. Effective content should be relevant, informative, and responsive to user needs, which in turn will increase engagement and time spent on the site. Optimize your content for your target audience and update it regularly to keep it interesting and relevant.

  • What is find and what query syntax does it use?
  • How to write a query in find
  • How to find files in a directory
  • How to find a file by name
  • How to find files by extension
  • How to search by file type
  • How to exclude search results
  • How to search multiple directories
  • How to show files owned by a specific user
  • How to set up a permissions search
  • How to search by modification date
  • How to set up file search by size
  • How to find empty folders and files
  • How to delete files in find

An expert is a specialist with deep knowledge and experience in a particular field. Experts play a key role in various fields, from science and technology to business and art. Their opinions and recommendations are highly valued, as they are based on thorough analysis and practical experience. Turning to an expert allows you to receive high-quality and well-founded decisions, which is especially important in a rapidly changing world. Expert assessment can significantly impact decision-making, as well as the strategic development of companies and organizations. It is important to select experts based on their qualifications and reputation in the professional community.

Engineer at the Russian Federal Nuclear Center - All-Russian Research Institute of Technical Physics named after Academician E. I. Zababakhin with experience in cybersecurity and Linux operating system administration. I am a Skillbox expert with deep knowledge and skills in protecting information systems and ensuring data security. My experience includes configuring and maintaining Linux-based servers, as well as conducting vulnerability analysis and developing strategies for their elimination.

Find Utility

The Find command is one of the most useful utilities in Linux for users working with a large number of files. It is especially in demand among system administrators. As the name suggests, this command is designed to search for files and directories, and the search can be performed not only by name, but also by various criteria, such as size, modification date, file type, and other parameters. This makes Find an indispensable tool for efficient file management and simplifies system administration tasks.

The find command has a clearly defined structure that allows you to effectively find files and directories in the file system. The basic syntax of the command includes key elements such as the search path, filter criteria, and actions to be performed on the found objects. This allows users to define complex queries to search by various parameters, including file name, type, size, and modification date. Using the find command can significantly simplify the process of file management and optimize workflows, making it an indispensable tool for administrators and users of Linux and Unix systems.

Directory searches are performed using specific parameters and criteria. To perform a search, use the following format: find [directory] [parameters] [search criterion] [pattern]. Make sure that all entered data meets the specified requirements to get accurate results. Correct use of this command allows you to effectively find the required information in large databases or directories.

Let's break it down piece by piece to better understand its structure and content. This will allow us to examine key points in more detail and highlight the main ideas. This approach will provide a deep understanding of the topic and facilitate further analysis. We will focus on each element to uncover their meaning and relationships. This will help create a more complete picture of the subject matter.

The find command is used to search for files and directories in the file system. It allows you to find files by various criteria, such as name, type, size, and modification date. Using the find command simplifies file management and speeds up the search process, which is especially useful in large and complex file structures. Optimizing your search with this command can save time and improve the efficiency of your data work.

A directory is a directory in which files and folders are searched. Unless the user specifies a directory manually, the default path is /home/accountname. This is where all files created by the user are stored. A proper understanding of the directory structure is key to effective data management and searching.

Parameters are an important tool for refining your search. They allow you to search for specific types of objects, such as files or directories. Using parameters significantly speeds up the search process and improves its efficiency, which is especially useful when working with large volumes of data. Properly setting search parameters helps quickly find the information you need, saving the user time and effort.

File search criteria include search features, such as file name, creation date, or extension. These parameters allow you to accurately find the documents you need and simplify the process of working with information. Specifying specific criteria helps reduce search time and improve file management efficiency.

A pattern is a criterion used to filter files based on various attributes, such as modification date, access rights, and other parameters. This tool allows you to effectively organize and manage files, providing a more accurate and targeted search for the information you need. Proper use of patterns significantly simplifies the process of working with large volumes of data, allowing you to quickly find and process only those files that match specified conditions.

Practice: Writing Queries in find

The find utility is a powerful tool for searching files and directories in the file system. We'll start with the basic search method, and then look at how to customize search criteria and use various patterns for more precise results. Using find allows you to effectively find the desired elements, optimizing the process of working with the file system.

When you open the terminal, the default search starts in the /home/account_name directory. If you run the find command without any additional parameters, you'll get a complete list of all the files in this directory. This allows you to quickly view the contents of your account. Using the find command can be useful for searching for specific files or folders, especially if you have a lot of data. To improve the search results, you can add various parameters to the command.

Search result using the find utilityScreenshot: Skillbox Media

Search for files in a specific

Let's open the Documents folder and check what files are in it. To do this, use the following command:

This folder contains three documents.

Search result using the find utilityScreenshot: Skillbox Media

Search results on your device may differ.

Search by name: find name

To search for a file by name in the system, use the find command with the -name parameter. You don't have to specify the full file name; you can use just a few letters by adding the * symbol. For example, instead of the full word «Lake», you can enter «Oze*», which will find all files containing that set of letters in the name. This approach greatly simplifies the search process and allows you to quickly find the documents you need.

We found all the files that contain "Oze" Screenshot: Skillbox Media

To change the command to search all files, excluding those that contain the word "Озе" in their name, you need to add the -not parameter before -name. This will perform a search, ignoring files with the specified word in their name. An example command might look like this: find . -type f -not -name ‘*Озе*’. This will return a list of all files that do not contain the word "Озе" in their name.

Search by extension

To find files with a given extension, enter its name in double quotes in the search bar. For example, to search for JPG images, you can use the following query: «JPG». This will allow you to quickly and efficiently find the files you need by their extension, improving search results and saving time.

Search results are a crucial step in the information retrieval process. They include displaying data that matches the user's query. An effective search result must be accurate, relevant, and up-to-date to satisfy all users. Search result optimization involves using keywords, improving content structure, and applying algorithms that enhance the quality of displayed information. It's also important to consider user experience to make the search process as convenient and intuitive as possible. By improving search results, you can significantly improve user satisfaction and increase traffic to your site or platform.

find found all files with the JPG extensionScreenshot: Skillbox Media

To search for multiple To search for multiple formats at once, such as .jpg and .webp, you can use the -o command. This operator allows you to specify a logical OR condition, which greatly simplifies the search process and expands its capabilities.

The find command allows you to find all files with the .jpg and .webp extensions. This is a useful tool for searching images on your system. Using this method, you can quickly identify and manage graphic files, which will greatly simplify the organization of your media resources.

Screenshot: Skillbox Media

Search by type

File: find file

By default, the Find command searches for both files and directories, which isn't always a convenient solution. To limit your search to just one of these types, use the -type parameter with the appropriate modifier. This will allow you to more efficiently find the desired items in the file system, improving performance and making it easier to work with search results.

  • d — for directories;
  • f — for files.

Let's try to find a directory named .var. It's important to clearly indicate that we are looking for a folder, not a file with that name. This will help avoid confusion and focus on the desired object. Searching for the .var directory can be useful for accessing data and configurations stored in this directory, so it is necessary to accurately determine its location in the file system.

Everything worked out is an expression that reflects the completion of a process or the successful implementation of a plan. It symbolizes the achievement of goals and satisfaction with the result. When we say "everything worked out," we emphasize the importance of persistence and perseverance in achieving success. It's a phrase that inspires and motivates, reminding us that every endeavor requires effort and patience, but ultimately bears fruit. Achieving success is not only a result, but also a path full of lessons and new discoveries.

Screenshot: Skillbox Media

To search for files containing in the title "sdk", you must specify this in your query. Use the appropriate search syntax to narrow your results and find the exact files you need. Make sure you formulate your query correctly to maximize the search capabilities and quickly find the documents and resources you need.

The result is the sdk.properties file. This file contains important parameters and settings for working with the SDK. It is necessary for proper integration and use of the software, allowing developers to easily manage configurations and connections. Proper configuration of the sdk.properties file ensures the stability and efficiency of the application.

Screenshot: Skillbox Media

Search with exclusion

If you have various File formats, such as images of lakes, can be excluded. Let's look at images containing the word "Lake" in the name and filter out all formats except .webp.

To accomplish this, we'll use the well-known logical operator -not, which we'll place before the parameter to exclude it. This will allow us to more accurately filter the required data, improving search results and ensuring a more efficient interaction with the system. Using the -not operator is an important step in optimizing queries and improving the quality of retrieved information.

As a result, we are left with only files with the .jpg extension. These images are a popular format for storing graphics, providing good quality with a relatively small file size. Using .jpg files allows you to effectively optimize page loading and improve user experience on the site.

Screenshot: Skillbox Media

Search in several Directories

The files you want to find may be located in multiple directories. To avoid retyping the search command, you can specify multiple folders, separating them with spaces. This greatly simplifies the process and saves time.

We'll search for all files with the .jpg and .docx extensions located in the Documents and Pictures directories. To do this, specify the folder names after the find command., not forgetting to add the / symbol before the directory names.

As a result of executing the request, we get a list of files located in two different directories.

Screenshot: Skillbox Media

Files Owned by User

You can use one of two commands to search for files owned by a specific user. These commands allow you to effectively identify and manage files on your system. By using the appropriate parameters, you can get accurate results and simplify the file administration process. Using these commands correctly will help you quickly find the data you need and streamline your workflow.

Rework the text, maintaining the main topic and avoiding adding unnecessary details. Optimize it for SEO and expand the content if necessary. Avoid using emojis and unnecessary characters, and do not add structured sections such as 1, 2, 3, or *. Please provide plain text.

The second option is great for searching within a specific directory. This allows you to more accurately find the data you need and streamline the search process. Selecting a specific directory helps avoid unnecessary information and focus only on relevant results.

These commands can also be used to work with groups of users. To do this, simply replace the -user parameter with -group. This allows you to effectively manage access rights and settings for entire groups, simplifying administration and increasing system security.

Let's examine a query to search the Documents folder of the fovpc user.

The folder contains three files belonging to this user. These files may contain important information or data that the user uses in their work. Proper organization and management of files in a folder play a key role in maintaining order and efficiency.

Screenshot: Skillbox Media

Search by rights

File search in Linux can be performed taking into account user permissions. To do this, it is important to understand how the access rights system functions in this operating system. Proper management of user rights allows you to effectively find the necessary files and ensure data security.

A file can have access rights, for example, 0664, which corresponds to the notation -rw-rw-r—. The first two letters rw indicate that the owner of the file has read and write permissions. The next two letters rw mean the same rights for a group of users. The last element r- refers to the category of others, who can only read the file but do not have permission to modify it. The - characters located at the beginning and end of the line, as well as between access rights indicator, act as separators. Proper understanding and setting file access rights are important for ensuring security and data management on the system.

Decoding permissions written in numerical format requires understanding the meaning of each digit. Each digit represents a specific access level or permission on the system. To correctly interpret these values, it is necessary to take into account that each digit is responsible for specific actions: reading, writing, and executing. Correctly deciphering access rights allows you to optimize the security and manageability of the system, and prevent unauthorized access. The main focus should be on how exactly these rights affect the functioning of the system and user interactions with it.

  • 1st digit - permission to execute as a program.
  • 2nd digit - permissions for the file owner.
  • 3rd digit - permissions for the user group.
  • 4th digit - permissions for all other users.

Each of the indicators can take numeric values ​​in the range from 0 to 7. This allows for a more accurate evaluation of the results and their analysis. The numeric scale from 0 to 7 provides flexibility in data interpretation, which contributes to a deeper understanding of the situation. Using this scale helps identify trends and patterns, as well as compare different metrics.

To search for files with specific access rights, use the -perm parameter. This parameter allows you to specify a pattern to search for files matching the specified permissions. Proper use of this tool significantly simplifies file management and allows you to effectively find the desired elements in the system. Using -perm, you can specify both exact permissions and patterns, which expands the search capabilities and simplifies working with the file system.

The final output will generate a significant number of files.

List of files available for reading and writing to a user with permissions 0664Screenshot: Skillbox Media

To search for permissions, you need to use letter designations. First, you need to determine whose permissions you need to find.

  • /u - user;
  • /g - group;
  • /o - other.

This section specifies the specific access rights that are required to perform tasks. This allows you to clearly define what levels of access users need to work effectively with systems and resources. Correct distribution of access rights helps improve security and optimize processes.

  • r - read;
  • w - write;
  • х - execute;
  • X - execute, if the file is a directory or already has similar rights for some user;
  • s - SUID or SGID bit;
  • t - sticky bit. In this case, only the owner of the file and directory will be able to delete it.

Let's check which programs in the /usr directory a user can temporarily run with the privileges of another user, usually the owner. To do this, use the find command with the /u=s parameter. This approach allows you to determine which programs can be executed, which can be useful for analyzing access rights and system security.

Accessing the /usr directory requires the use of the sudo command, which allows you to elevate your privileges. This is necessary to perform operations that require administrative rights, ensuring security and control over system files.

List of executable files for which the user is temporarily allowed elevated privileges. Screenshot: Skillbox Media

Search by Modification Date

Date searches are performed using special patterns. These patterns allow you to effectively filter and extract information based on timestamps. Using the right patterns for date searches significantly simplifies the data analysis process and helps you find the information you need quickly. Patterns can vary depending on the date format you need to process and can include both standard and customized formats. Proper use of date search patterns is a key aspect of data processing and information analysis.

  • -mtime — files that were modified a certain number of days ago;
  • -atime — files opened a specified number of days ago;
  • -cmin — files modified a specified number of minutes ago.

In the examples given, we refer to the / path. This symbol denotes the root directory, which contains all files, including system files. In order for the find command to access the contents of system folders and find the necessary files, you must use superuser privileges by adding sudo before the command. This allows for full access to the file system and ensures that searching is efficient and effective.

To display files that have been modified within the last 30 days, use the following command:

To get a list of files opened in the last 30 days, use the following query:

To display files that have been modified in the last 30 minutes, use the following command:

You can search for files within a specific date range. For example, to filter files modified between 30 and 60 days ago, use the following query:

It is important to remember that following rules and guidelines in any process is critical to achieving the desired results. Correct execution of tasks, adherence to established standards, and attention to detail contribute to the successful completion of projects. Misunderstanding or ignoring these aspects can lead to errors and negative consequences. Therefore, it is important to always pay attention to key points and follow the instructions to ensure maximum efficiency and quality of the work performed.

Pay attention to the symbols preceding the number of days. The symbol «+» indicates a search for files that were modified more than the specified number of days ago, while the symbol «-» indicates a search for files that were modified less than the specified number of days ago. These symbols will help you fine-tune your search parameters and effectively find the files you need.

In this context, we are talking about files that were modified between 30 and 60 days ago.

We are looking for files that were modified in the last 10 days. This process will allow you to quickly identify relevant changes in your file system or project. Use the appropriate commands or tools to filter data by modification date. This will help you effectively manage your files and keep them up-to-date.

Screenshot: Skillbox Media

Find by file size: find size

Using the find command You can search for files of a specific size. The -size criterion is used to set file size limits. The symbol M is used to specify the size in megabytes, and the symbol G is used for gigabytes. This simplifies the search process and allows you to quickly find the files you need in the file system.

We search for files up to 100 MB in size. This will allow you to effectively manage your storage and optimize disk space. When searching for such files, it's important to consider their purpose and storage needs. Optimizing space will help improve system performance and make it easier to access important data. You can use various tools and commands to search and filter files by size, which will help you quickly identify and remove unnecessary items.

The list turned out to be quite extensive and does not fit on one screen.

Screenshot: Skillbox Media

Finding empty files and directories

The -empty parameter is used to search for and display information about empty files and folders in the operating system. This parameter can be used to effectively find empty folders.

Screenshot: Skillbox Media

When searching through files, use the f modifier. This option allows you to refine your query and limit your search results to files only, making the process more efficient and targeted. Using the f modifier allows you to quickly find the documents you need and improves the organization of your data.

Screenshot: Skillbox Media

Deleting files

To delete found files or You can use the rm command in combination with the find function to delete folders. To do this, add the -exec rm -f {} \; parameter to the find command. Let's see how this is implemented in practice.

To delete all images with the .jpg extension from the "Images" directory, you need to use the following query:

To avoid files accumulating in the downloads folder that are larger than 200 MB, it is recommended to regularly clean this folder. For this purpose, you can use automatic tools that will help you monitor and delete large files. It is also worth adjusting the download settings in your browser to save files to specific folders or cloud services. This will not only help organize the space on your device, but also make it easier to access the files you need. Make sure you have backups of important documents before deleting. Regular file management practices will help you maintain order and use disk space efficiently.

Basic operations with the find utility involve using a combination of parameters, criteria, and patterns. This approach allows for flexible query customization and makes working with find similar to building a construction kit or using regular expressions. A proper understanding of these elements greatly simplifies searching for files and directories in the file system, which is especially useful for administrators and developers. Using find, you can effectively filter results, increasing the productivity of your data work.

If you want to delve deeper into the utility's capabilities and learn how to customize search results using all available parameters and criteria, we recommend reading the official documentation. It will provide you with a complete understanding of the utility's functionality and help you use it optimally for the best results.

Learn more about the world of programming and code in our Telegram channel. Subscribe to stay up-to-date with the latest news, useful tips, and interesting materials!

Read also:

  • What is Linux: A Guide to the Most Free Operating System
  • Test. Curse Like Linus Torvalds
  • Basic Linux Terminal Commands and Hotkeys

Learn more about coding and programming in our Telegram channel. Subscribe to stay up-to-date with the latest news and useful content!

Linux OS Administration

You will learn about Linux OS and its advantages, learn how to work with the command line and manage network connections. You can become a Linux administrator, configure servers, and maintain them.

Find out more