Skip to content

Search and Inspect Files on Linux: A Beginner Guide

Admin
October 5, 2026
7 min read
Beginner
πŸ’» Target OS: Ubuntu 24.04+ / Debian 12+ / RHEL 9+

Before you begin

  • Applies to:Linux terminal exercises using less, grep, find, file, and stat.
  • Where to start:Open a terminal with your regular account. Use the practice folder in your home directory.
  • What you need:Have write access to your home directory for the sample files. Start searches in the specific folder you need.
Guide tools
Guide tools
0/0 Steps (0%)

Linux administrators often need to locate a file, inspect its metadata, or find a line in a log. This guide introduces less, grep, find, file, and stat using a small practice folder in your home directory.

Search a specific directory first. Broad searches through / can take a long time, return permission errors, and read files you do not need.


01

Create Sample Data in Your Home Folder

Practice

Create a dedicated lab folder and a short text file. The here-document writes the lines literally; the quoted EOF marker prevents shell expansion. Re-running this block replaces only the named practice file.

Terminal window
LAB="$HOME/file-search-lab"
mkdir -p "$LAB"
cat > "$LAB/app.log" <<'EOF'
2026-10-05T09:10:00Z INFO service started
2026-10-05T09:11:18Z WARN retrying request
2026-10-05T09:12:03Z ERROR database connection failed
2026-10-05T09:12:40Z INFO retry succeeded
2026-10-05T09:15:22Z ERROR request timed out
EOF
❯ Illustrative Output (Results Vary)
The lab log is saved at ~/file-search-lab/app.log.

02

Open the Log Without Editing It

Read a File

less lets you scroll through a file without editing it. Search forward by typing /ERROR and pressing Enter; use n for the next match and q to quit. It may not be installed in minimal systems; the package manager can install it when approved.

Terminal window
less "$LAB/app.log"
❯ Illustrative Output (Results Vary)
Inside less:
/ERROR search for ERROR
n go to the next match
q quit and return to the shell

03

Show Matching Lines and Their Line Numbers

Search Text

grep -n prints line numbers and matching lines. Add -i to ignore case. Put patterns in quotes so the shell passes them to grep as one search term. No matches normally produces no output and an exit status of 1.

Terminal window
grep -n -i 'error' "$LAB/app.log"
grep -n 'timeout' "$LAB/app.log"
❯ Illustrative Output (Results Vary)
3:2026-10-05T09:12:03Z ERROR database connection failed
5:2026-10-05T09:15:22Z ERROR request timed out
5:2026-10-05T09:15:22Z ERROR request timed out

04

Search Only the Lab Directory

Find a File

find searches a directory tree. Start at a known directory, select regular files with -type f, and quote wildcard patterns so the shell does not expand them before find receives them.

Terminal window
find "$LAB" -type f -name '*.log' -print
❯ Illustrative Output (Results Vary)
/home/sam/file-search-lab/app.log

05

Check Metadata Before Further Analysis

File Details

file examines content to identify a likely file type; an extension alone is not proof of format. stat shows size, ownership, and timestamps. These commands read metadata and do not edit the file.

Terminal window
file "$LAB/app.log"
stat "$LAB/app.log"
wc -l "$LAB/app.log"
❯ Illustrative Output (Results Vary)
/home/sam/file-search-lab/app.log: ASCII text
File: /home/sam/file-search-lab/app.log
Size: 226 Blocks: 8 IO Block: 4096 regular file
5 /home/sam/file-search-lab/app.log

References

Comments