How to Find a File in Linux from the Command Line

HomeLinuxHow to Find a File in Linux from the Command Line

How to Find a File in Linux from the Command Line

Linux is a free operating system that consists of programs with which the user performs various actions. Linux is an alternative to windows but is more used by web servers due to its simplicity and versatility. The Linux control panel is used to control the operating system. Using the Linux control panel, system administrators work with files.

To automate and further simplify the work with a web server based on Linux, web hosting control panels are used. For example, such popular hosting panels as ISPmanager and cPanel are based on Linux.

In this article, we will directly analyze how you can generally search in Linux. There are many programs for these purposes, but some of them have already become classic searches in Linux. We emphasize that in this article we will focus exclusively on Linux. This means that even if you encounter utilities with the same name on Solaris or BSD-based operating systems, the tips given may not always work. This also applies to busy box, which implements only a small part of the functionality of the original utilities.

So, let’s touch on the 3 most popular commands: locate – provides a search by file name in a pre-prepared database, find – works on a real file system and provides incredible opportunities for searching files and folders by various parameters. We will also talk briefly about grep, a utility for finding text in files.

Finding a File by Name Using the Locate Utility

To simply search for a file by name in Linux, it is more logical to use the locate command – due to a pre-prepared base, it will quickly give the desired result:

$ locate myfilename

If you get a message that the command was not found, you may need to install it. For Debian-based distributions (Ubuntu, Mint) you can install it with the command:

# apt-get install mlocate

RedHat, CentOS, and Fedora users should run.

# yum install mlocate

After installation, you need to create or update the database, the file that contains the “snapshot” of the file system that makes locate so fast:

# updatedb

That is, instead of going through the entire file system each time, locate trusts updatedb to create a file containing all the names and paths of the file system and search it. It is important to keep it up to date, otherwise, you may accidentally find long-deleted files, or not find new ones. Therefore, updatedb is “hung” on CRON during installation and is executed automatically – you can verify this by looking at the CRON job files.

Searching Linux with the Find Utility

A much more powerful and flexible tool for finding files and folders in Linux is the find command. Its syntax is simplified like this:

find [path…] [expression]

Finding a File by Name

In Linux, to search for a file by name throughout the entire file system, you need to enter:

# find / -name myfilename

Please note that in this case the search will go on the file name exactly corresponding to the given one. To mimic the behavior of the locates command for find, type:

# find / -name "part_of_name"

“*” means any number of any characters. This gives us the ability to also search for files by extension:

# find / -name "*.jpg"

Searching for a File by Date

Here is an example of how to search a file in Linux by date:

# find /home -mtime -5

This command will list all files in the /home directory that have been modified no more than 5 days ago.

Finding a File by Size

And so, for example, you can search for files by size:

# find /home -size +1G

This command will list all files in the /home directory that are larger than one gigabyte. Very handy for finding large files in Linux.

Folder Search

Searching for a folder in Linux is no different than searching for a file since a folder is a file. However, find gives us the option to specify the type. For example, to explicitly search only folders whose names contain the word, such as mysql, run:

# find / -type d -name "mysql"

Here the “-type” key indicates the type. Find currently supports the following types:

  • b – block device
  • c – character device
  • d – folder (directory)
  • p – named pipe
  • f – normal file
  • l – symbolic link
  • s – socket
  • D – door (only for Solaris)

You can read more about the various find search options by typing the command:

$ man find

Finding text in Linux Using the grep Utility

Grep is a good command when it comes to finding text in files in Linux by pattern. Let’s make a reservation right away that it is better to search for text in files in plain text format. It is unlikely that it will be possible to efficiently search through Word and Excel files with a grep.

So, to search for a string across all files in a directory, for example,/etc, you should enter.

# grep -lri root /etc/*

Such a command will display the names of all files in which the treasured 4 letters “root” can be found. Here, the “-l” switch causes grep to output only filenames. That is, if you want to see also the very line where you managed to find the requested word, then “-l” should not be specified. The “-r” switch searches not only /etc but all files in all subfolders. And finally, the “-i” option says not to take the case into account, i.e. grep will look for more than just the word “root”.

Of course, grep supports regular expressions. To search for a string that starts with the same word, for example, type:

# grep -lri ^root /etc/*

The trailing cap at the beginning of the word “root” forces grep to output only those files where the word “root” appears at the beginning of the line.

As you can see, finding files in Linux using the command line is quite simple, the main thing is to choose the command you need. However, if you are running a web server and want to make the process even easier, we recommend using a web host. Such as ISPmanager, cPanel, Plesk, and DirectAdmin. In particular, the ISPmanager linux control panel can be highlighted, as it provides convenient tools for creating users, sites, domains, email addresses, and more.

This page may contain affiliate links, which help support bettertechtips.com. Learn more

hand-picked weekly content in your inbox

.

related posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here