If you’re new to the Linux command line, then you might be wondering how to find and search for files in Linux from the command line itself. It’s actually quite simple! In this guide, I’ll explain how to use the Linux find command, one of the most powerful commands in the Linux command line toolkit. Let’s get started!
The Linux command line can seem daunting at first. But once you get the hang of the basic commands, it’s a powerful tool that can help you be more productive. One of those basic commands is the find command, which allows you to search for files on your computer. Here’s how it works Open up the terminal and type find. Next, type in one or more file extensions. For example, if I wanted to find all the mp3s on my computer I would type find .mp3.
Here are some other things you might want to look for when using this command:
-Pictures – jpg, png
-Documents – docx, pdf
-PDFs – pdf
-Programs – exe
-Linux Packages – deb -Compressed archives – tar.gz, zip
-Video formats – avi, mkv
-Audio formats – mp3, wav -Text files – txt, csv
-Source code – py, html -Spreadsheets – xlsx, ods
-Images – bmp, gif , jpeg, png
-Archives – tar.bz2, zip
-Postscript files – ps
-HTML pages – htm, html.
Finding files by name
If you’re new to the Linux command line, one of the first things you’ll want to learn how to do is search for files. Fortunately, this is a relatively simple task. In this guide, we’ll show you how to search for files by name using the find command.
1) Type the following in your terminal to see a list of files that contain test in their name and then hit Enter: find . -name *test*
2) Next, type ls to see all of the directories (folders) on your computer and hit Enter. You should now see a list that includes both directories and files.
3) Finally, type find . -name *test* xargs ls less
4) Press q to exit out of less when you are finished reading through it’s output.
5) Repeat steps 1-4 substituting any other word (e.g., apple, banana). Now go ahead and type find . -name *banana* or find . -name *apple*. Now try typing find . -name *tofu*. Notice anything? As we’ve seen before, sometimes the results may not be what you expect. Remember that ls lists all directories in the current directory. When searching for files containing banana, those results would be filtered by our use of xargs with ls.
However, if you typed find without specifying which directory to look at, the system would search every directory on your computer! The lesson here is don’t forget to specify where you want to look! You can either just type find and enter some file names, or you can tell it explicitly what folder to search by typing find . after the previous command.
For example, if I wanted to search my home directory for files named banana instead of having to enter ./home/elijah/. After entering cd ~ (to move into my home directory), I could type: find . -name *banana* or find . -name *apple*. Now try typing find . -name *tofu*. Notice anything? As we’ve seen before, sometimes the results may not be what you expect. Remember that ls lists all directories in the current directory.
When searching for files containing banana, those results would be filtered by our use of xargs with ls. However, if you typed find without specifying which directory to look at, the system would search every directory on your computer! The lesson here is don’t forget to specify where you want to look! You can either just type find and enter some file names, or you can tell it explicitly what folder to search by typing find . after the previous command.
Finding content in a file
When you’re looking for a specific piece of content in a file, the grep command is your best friend. Grep searches through each line of a file and outputs any lines that contain the search term you specify. For example, let’s say you have a file called todo.txt that contains a list of tasks. To find all lines containing the word work, you would use the following command:
grep work todo.txt
This would output every line in the file that contained the word work. You can also use grep to search through multiple files at once. To do this, we need to first collect the list of files that we want to search from. Then we run grep on those files using what’s known as an or operation. The syntax looks like this:
grep term *.txt
This will tell grep to look for term in all txt files (*.*). Any line containing term will be printed out on screen. Note that the asterisk (*) matches zero or more characters and the dot (.) matches one character only.
Find a certain amount of text in a file
Have you ever wondered how to search for text within a file using the Linux command line? Maybe you need to find a certain string of characters in a log file. Or maybe you want to find all files that contain a certain word. Either way, grep is the tool you need.
In this tutorial, we’ll cover the basics of using grep to search for text in files. We’ll start by learning how to use grep to find simple patterns of text. Then we’ll move on to more advanced topics like searching for text across multiple files and recursively searching directories. By the end of this tutorial, you’ll be able to use grep like a pro!
Find invisible characters (like tabs and newlines)
One of the first things you’ll want to learn how to do when using the Linux command line is how to search for files. This can be a bit tricky, since there are invisible characters like tabs and newlines that can make your search results come up empty. But don’t worry, we’ll show you how to find them. To search for these invisible characters in your current directory (i.e., what’s on your screen), use the following:
$ sed -n ‘/^*$/p’ *
This will print any lines starting with a tab or a newline character. In addition, if you’re looking for files in other directories, just add the path after * as well. For example:
$ sed -n ‘/^*$/p’ * /home/username/Documents/* This will display any lines starting with a tab or newline character in every file within your Documents folder.
For more information about searching for specific patterns on the Linux command line, check out our Advanced Linux 101 post!
How to read man pages on the command line
If you’re new to the Linux command line, one of the first things you’ll want to learn is how to read man pages. Man pages are documentation pages that provide information about commands and other system tools. They can be a bit daunting at first, but once you get used to them, they’re an invaluable resource. Here’s a quick guide to getting started. 1) First, go to your terminal window and type man, followed by the name of the command or tool whose documentation you want to read.
For example, typing man grep will show you all of the documentation for grep (which we’ll cover in detail later). Typing man cd will show you all of the documentation for cd (which will also be covered in detail later). Typing man ls will show you all of the documentation for ls (as in list directory contents), which we’ll also cover in detail later.