Mastering Linux: Easy Tips for Locating Files Folders and Text

oluwatobi2001 - Jun 6 - - Dev Community

Introduction

I see a world where every device will utilize Linux in the nearest future. It’s currently the driving force for open-source development globally and it’s cemented its relevance in today’s world as it is the backbone of many applications and services. Apt knowledge of the use of this operating system and ability to execute programs with it gives the developer an advantage.
This article which is the first of many, serves as to guide the developer on how to perform basic search commands using Linux. This is needed to locate file directories and texts.
As a popular programmer stated, the best way to learn Linux is to use it. Having Linux installed and running is a prerequisite to this tutorial so facilitate easy comprehension and practice. With this completed, let’s get started.

Linux search commands

Searching for files can be cumbersome and complex for the newbie Linux user especially as it’s a sharp contrast from the Windows OS. Thankfully, newer Linux distros include beautiful graphical user interfaces to enhance user interaction and experience on Linux but still, the mastery of Linux search commands via the command line is still relevant, hence the need for this tutorial. Right now we will be introducing the various Linux commands that are used in files and text searches. They include

  • Locate
  • find
  • Grep

The find and locate commands are specifically designed in the search for files and directories within the Linux file system while the Grep is more suited to locate texts within various text files in the Linux file system. Details regarding their use cases and relevant examples will be provided in the subsequent sections.

Locate command

This command alongside the find command mentioned earlier is used to search for files using the file names within the Linux file system directories but how is this command different from the find command seeing that they are quite synonymous?
Firstly, the Locate command is executed to locate the details of a file being searched from a database of filenames which is updated automatically daily.
This implies it has a faster search time than the find command. However, it has a minor flaw in that the data only gets updated at a specified time daily or when executed. Hence files saved after the database has been updated at a particular time won’t be shown in the search results.
Here is a command to search for the file “laptop.txt” using locate.
Locate laptop

Image description

As you an see, the file name is been searched from the plocate db which doesn't have the file name indexed.
With that, we have successfully discussed the locate command. Up next is the find command

Find Command

The find command as highlighted briefly in the previous paragraph can also be used to locate file names within a given directory. However, unlike the locate command which searches through an indexed database containing filenames, the find command searches through the entire Linux file system to locate the files in question. This results in a much slower response time compared to the locate command. However, it has an advantage over the locate command by providing all files matching the search query at any time indicated irrespective of the time the file was saved unlike the locate command.
Here is a command to search for the file “laptop.txt” using find.
Find laptop
Image description

Grep

This is a command used in Linux to search for words/phrases within various text file outputs. It’s an acronym which stands for Global regular expression print. It goes beyond locating the files which contain the text in question but also highlights the lines where these texts are found. Here is an example of how Grep can be used.
grep laptop
Image description
The above code searches for the text “laptop” in the Linux directory. It then outputs any files where this can be found. By default, the Grep command is highly case-sensitive, matching the texts only in the specified case format. However, due to its inherent flexibility, it can also output non-case-sensitive matches too. To achieve this, the –I command is added to the line text.
Here is a command to search for the text “laptop” eliminating the case sensitivity using grep.
grep –i laptop laptop.txt

Image description
This will output all matching text ignoring the case format of the text matches.
Also, Grep provides an inverse searching feature which highlights all other lines without the text to be matched in question and ignores the lines containing the text being matched in question.

grep –vi laptop
So far, these commands can be used to navigate through the Linux file directory to locate various files and text within the Linux OS. It is also essential to gain mastery of other Linux file commands such as ls, mv, rm and rmdir which can be used in file navigation and file structure modification.

Conclusion

With this, we have come to the end of the tutorial. We hope you’ve learned essentially about Linux commands, how to use them and their pros and cons. Feel free to drop any questions or comments in the comment box below. You can also reach out to me on my blog also check out my other articles here. Till next time, keep on coding!

. . . . . . . . . . .