Everyday Guidebooks
Cover of Linux Command Line for Text Search

Cybersecurity & IT

Linux Command Line for Text Search

A Practical Guide to Finding Files and Searching Their Contents with find, grep, and Regular Expressions

by Todd Michael

Available now

Know the text is somewhere on your system, but not where to look? Learn to find it with a focused, practical guide to the linux command line.

Linux Command Line for Text Search takes you from locating a file by name to investigating matches across directories of source files and documents. You’ll practice using find to filter by name, type, size, and modification time; grep to search lines and interpret results; and regular expressions to describe patterns rather than exact words.

Step-by-step examples explain recursive searches, shell quoting, filenames with spaces, and the difference between a filename pattern and a text pattern. Exercises and worked solutions help you diagnose searches that return too much, too little, or nothing at all.

Whether you’re developing everyday Linux skills or reinforcing command-line topics for an IT certification, you’ll finish with a repeatable approach to finding files and the information inside them.

Contents
  1. 1. A Searcher's Linux Workspace
  2. 2. Locate Files by Name and Type
  3. 3. Filter by Size, Time, and Depth
  4. 4. Combine find Conditions
  5. 5. Quoting and Filename Safety
  6. 6. Find Text with grep
  7. 7. Search Directories Recursively
  8. 8. Read Matches in Context
  9. 9. Regular Expression Foundations
  10. comment
  11. 10. Extended Patterns and Practical Choices
  12. 11. Connect File Selection and Content Search
  13. 12. Investigate Source Files
  14. load_timeout_backup was removed from this module.
  15. Configuration
  16. 13. Search Documents and Troubleshoot Results
  17. Operations handoff
  18. 14. Search Challenges and Worked Solutions
  19. The old timeout was 30 seconds.
  20. Check REQUEST_TIMEOUT in a future test.
  21. Other Books by Todd Michael

Read a sample: 1. A Searcher's Linux Workspace

A useful search starts with a question. Sometimes the question is, “Which file has this name?” At other times it is, “Which file contains this sentence?” Those questions may lead you through the same directories, but they call for different kinds of search.

Before learning the options that make find and grep powerful, you need a place to practice and a reliable picture of where your commands operate. In this chapter, you will build a small directory tree, move around it, inspect its files, and run your first searches. You will also see how permissions and the choice of a starting directory affect what you can discover.

The examples use a terminal and an ordinary Linux user account. Commands shown in code blocks are meant to be typed at the shell prompt; the prompt itself is omitted so that you can copy the commands.

Start by moving to your home directory and creating a directory for the exercises:

bash cd mkdir -p linux-search-practice cd linux-search-practice pwd

The cd command with no argument moves to your home directory. The mkdir -p command creates the named directory if it does not already exist. The second cd makes that directory your working directory, the location from which relative paths are interpreted.

The output of pwd will depend on your account. It might look like this:

text /home/morgan/linux-search-practice

Throughout the book, your home directory and username may differ from those in example output. The structure after your home directory is what matters here.

Create several directories:

bash mkdir -p notes projects/demo logs

Now create a handful of plain-text files:

bash printf 'Project meeting on Monday\nReview the backup plan\n' > notes/meeting.txt printf 'Buy coffee\nReview the backup plan\n' > notes/tasks.txt printf 'backup enabled\nretention days: 7\n' > projects/demo/config.txt printf 'def start():\n print("Service ready")\n' > projects/demo/app.py printf 'Service ready\nBackup completed\n' > logs/service.log

Each printf command writes the specified text. The \n sequences create line breaks. The > operator directs the output into a file. It creates the file if necessary and overwrites it if it already exists. Use these commands in the practice directory, not in a directory containing files you need to keep.

The workspace now has this shape:

text linux-search-practice/ ├── logs/ │ └── service.log ├── notes/ │ ├── meeting.txt │ └── tasks.txt └── projects/ └── demo/ ├── app.py └── config.txt

You do not need a special tree-viewing program to work through this book. This diagram is a map of the directories and files you just created.

Use ls to inspect the top level:

bash ls

You should see the directory names logs, notes, and projects. Their display order or formatting may vary with your environment.

To look inside one directory, give ls a path:

bash ls notes

You should see meeting.txt and tasks.txt. To read a short file, use cat:

bash cat notes/meeting.txt

The output is:

text Project meeting on Monday Review the backup plan

These files are deliberately small. Later, the same search habits will help when reading every file by hand is impractical.

A command does not have to operate only in the working directory, but the working directory provides the starting point for a relative path. Check it whenever you are unsure:

bash pwd

If you are still in linux-search-practice, this command reads the file you just created:

bash cat notes/meeting.txt

The path is relative because it starts with notes, not with a slash. The shell and the programs you run interpret it relative to the current working directory.

Now move into notes:

bash cd notes pwd

From here, the same file can be addressed simply as:

bash cat meeting.txt

But cat notes/meeting.txt would now look for a directory named notes inside the current notes directory. That path does not identify the file you intended.

Two short path components are especially useful:

- . refers to the current directory. - .. refers to the parent directory.

While inside notes, both of these commands read meeting.txt:

bash cat ./meeting.txt cat ../notes/meeting.txt

The first starts at the current directory. The second goes up to linux-search-practice, then back down into notes. The extra movement is unnecessary here, but it shows how path components work.

You can also reach a neighboring directory through the parent:

bash cat ../logs/service.log

Return to the workspace root before continuing:

bash cd .. pwd

An absolute path starts at the filesystem root, written as /. For example, if the workspace is /home/morgan/linux-search-practice, the absolute path to the meeting file is:

text /home/morgan/linux-search-practice/notes/meeting.txt

Unlike a relative path, that path identifies the same location regardless of your working directory. Your actual absolute path may be different. You can combine the output of pwd with the names in the workspace diagram to understand it; you do not need to type another user's example path.

The distinction matters during searches. A command starting at . searches from wherever you are now. A command starting at notes uses the notes directory relative to wherever you are now. Before interpreting an unexpected result, check both the working directory and the starting path.

Suppose you remember that a file is named meeting.txt, but you do not remember which directory contains it. From the root of the practice workspace, run:

bash find . -name 'meeting.txt'

The result should be:

text ./notes/meeting.txt

Read the command from left to right:

- find walks through a directory hierarchy. - . tells it to start in the current directory. - -name 'meeting.txt' selects…

More Cybersecurity & IT

Cover of The White Hat Bible, Volume 1The White Hat Bible, Volume 1Available nowCover of The White Hat Bible, Volume 2The White Hat Bible, Volume 2Available nowCover of The White Hat Bible, Volume 3The White Hat Bible, Volume 3Available nowCover of The White Hat Bible, Volume 4The White Hat Bible, Volume 4Available nowThe White Hat Bible, Volume 5Available nowCover of The White Hat Pocket GuideThe White Hat Pocket GuideAvailable now

About Todd Michael

Todd Michael is an American author and the founder of ToddMichael.tv. He writes practical, plain-English books on money, health, preparedness, technology, games, humor and more.

About the author Todd Michael · Todd Michael’s author site

Part of the ToddMichael.tv network.