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…