Skip to content

Search and Inspect Files on Linux

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.


Step 1: Prepare a Practice Log

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
❯ View Expected Console Output
The lab log is saved at ~/file-search-lab/app.log.

Step 2: Read and Search a File with less

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"
❯ View Expected Console Output
Inside less:
/ERROR search for ERROR
n go to the next match
q quit and return to the shell

Step 3: Find Matching Lines with grep

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"
❯ View Expected Console Output
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

Step 4: Find Files by Name

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
❯ View Expected Console Output
/home/sam/file-search-lab/app.log

Step 5: Inspect File Type, Size, and Timestamps

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"
❯ View Expected Console Output
/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