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
Create Sample Data in Your Home Folder
PracticeCreate 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.
LAB="$HOME/file-search-lab"mkdir -p "$LAB"cat > "$LAB/app.log" <<'EOF'2026-10-05T09:10:00Z INFO service started2026-10-05T09:11:18Z WARN retrying request2026-10-05T09:12:03Z ERROR database connection failed2026-10-05T09:12:40Z INFO retry succeeded2026-10-05T09:15:22Z ERROR request timed outEOF⯠View Expected Console Output
The lab log is saved at ~/file-search-lab/app.log.Step 2: Read and Search a File with less
Open the Log Without Editing It
Read a Fileless 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.
less "$LAB/app.log"⯠View Expected Console Output
Inside less:/ERROR search for ERRORn go to the next matchq quit and return to the shellStep 3: Find Matching Lines with grep
Show Matching Lines and Their Line Numbers
Search Textgrep -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.
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 failed5:2026-10-05T09:15:22Z ERROR request timed out5:2026-10-05T09:15:22Z ERROR request timed outStep 4: Find Files by Name
Search Only the Lab Directory
Find a Filefind 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.
find "$LAB" -type f -name '*.log' -print⯠View Expected Console Output
/home/sam/file-search-lab/app.logStep 5: Inspect File Type, Size, and Timestamps
Check Metadata Before Further Analysis
File Detailsfile 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.
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 file5 /home/sam/file-search-lab/app.log