Comprehensive Reference for File Inspection, Manipulation, Search & Processing in Linux
In Unix and Linux systems, files are the foundational atomic unit of persistent data, configuration, device interaction, and inter-process communication. Mastering terminal-based file manipulation commands is the cornerstone of system administration, backend development, scripting, and DevOps automation. This masterclass examines the core suite of GNU coreutils and Unix text processing tools used daily on production Linux servers.
Ken Thompson and Dennis Ritchie conceived the Unix operating system around a timeless design principle: “Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.” Rather than building bloated, monolithic utilities, Linux supplies dedicated tools like cat, grep, sort, wc, and find that combine seamlessly via pipelines (|) and redirections (>, >>, <).
The cat utility (short for catenate or concatenate) reads files sequentially and writes them to standard output. While frequently used to view file contents, running cat on immense files floods the terminal buffer. Modern Linux administrators choose the right viewer for each task:
| Command | Primary Function | Memory Impact | Key Practical Option |
|---|---|---|---|
cat |
Outputs entire file stream directly to stdout | Streams instantly; fast for small files | cat -n file.txt (numbers all output lines) |
tac |
Outputs file lines in reverse order (bottom to top) | Buffers lines for reverse rendering | tac /var/log/syslog (view newest entries first) |
less |
Interactive terminal pager with two-way scrolling | Minimal; lazy loads chunks on demand | less +F access.log (scrolling with live tail mode) |
nl |
Line numbering filter with format controls | Streams stream line-by-line | nl -ba code.c (numbers all lines including blanks) |
# 1. View file with line numbers
cat -n /etc/hosts
# 2. View non-printable characters and line endings ($ for LF, ^M for CRLF)
cat -A script.sh
# 3. Suppress repeated empty blank lines
cat -s application.log
# 4. Concatenate two files into a combined destination
cat part1.txt part2.txt > combined_report.txt
# 5. Append text directly from terminal input using a heredoc
cat << 'EOF' > server_config.conf
server_name veewom.com;
listen 443 ssl;
ssl_certificate /etc/ssl/cert.pem;
EOF
The touch utility is the universal CLI command for creating empty zero-byte files or refreshing existing file timestamps without altering file contents. Every Linux file inode maintains three core timestamps: atime (Access Time), mtime (Modification Time), and ctime (Change Time).
# 1. Create a single empty file or multiple files simultaneously
touch index.html style.css app.js
# 2. Update only the modification time (mtime) without touching access time
touch -m existing_file.txt
# 3. Update only the access time (atime)
touch -a existing_file.txt
# 4. Set a custom timestamp explicitly (format: [[CC]YY]MMDDhhmm[.ss])
touch -t 202611021430.00 archived_report.txt
# 5. Replicate timestamps from a reference file
touch -r master_template.cfg user_instance.cfg
The cp command duplicates files and directories across directories or filesystems. When copying system configurations or deep directory hierarchies, blindly using cp source dest can destroy ownership, timestamps, and symbolic links.
| Flag | Option Name | Operational Purpose & Best Practice |
|---|---|---|
-r / -R |
Recursive | Copies directories and their entire subtrees recursively. |
-a |
Archive | Equivalent to -dR --preserve=all. Preserves symlinks, file modes, ownership (UID/GID), and timestamps. Mandatory for system backups. |
-i |
Interactive | Prompts for confirmation before overwriting an existing destination file. |
-u |
Update | Copies only when the source file is newer than the destination file or when the destination is missing. |
-v |
Verbose | Displays file copy operations in real-time. |
# Copy a single file with backup confirmation
cp -i database.sql database_backup.sql
# Production backup: Preserve permissions, symlinks, and timestamps
cp -a /var/www/html /backup/www_backup_$(date +%F)
# Sync new changes only with verbose feedback
cp -uv /staging/src/* /production/src/
Unlike other operating systems that separate renaming from moving, Linux treats renaming and moving as the identical operation handled by the mv utility. Behind the scenes, if source and destination are on the same filesystem, mv performs an instantaneous atomic metadata update on the directory entry without copying file data blocks. If moving across different filesystems, mv copies the file to the target and deletes the source original.
# 1. Simple file renaming in the current directory
mv draft_v1.md final_publication.md
# 2. Move file into a specific target directory
mv final_publication.md /var/www/articles/
# 3. Interactive safety prompt to avoid unintentional overwrites
mv -i new_dataset.csv /data/warehouse/dataset.csv
# 4. Never overwrite an existing destination file (-n / --no-clobber)
mv -n incoming_logs/*.log /archive/processed/
The rm (remove) command unlinks and destroys directory entries and frees underlying disk inodes. Linux does not have a built-in terminal Recycle Bin or Trash Can. Once rm deletes a file, the data blocks are marked unallocated and cannot be trivially undone.
# 1. Delete a single file
rm scratchpad.txt
# 2. Prompt interactively before deleting each file
rm -i *.tmp
# 3. Recursive deletion of a directory and all nested content
rm -r /tmp/build_cache/
# 4. Forceful recursive deletion (suppresses errors and confirmation prompts)
# CAUTION: Double check your absolute path before running this!
rm -rf /tmp/test_dir/
When investigating multi-gigabyte server logs or CSV records, loading the entire file into an editor will crash system RAM. The head and tail utilities isolate boundaries:
# 1. View first 10 lines (default)
head /etc/passwd
# 2. View first 25 lines explicitly
head -n 25 /var/log/dpkg.log
# 3. View last 15 lines of a log file
tail -n 15 /var/log/auth.log
# 4. Live follow mode (essential for debugging real-time web traffic or errors)
tail -f /var/log/nginx/access.log
# 5. Follow by file descriptor and retry if log rotates (logrotate support)
tail -F /var/log/syslog
The grep command (Global Regular Expression Print) scans text files or standard input pipelines for lines matching a pattern or regular expression. It is one of the most powerful diagnostic utilities in the Linux ecosystem:
| Option | Description | Syntax Example |
|---|---|---|
-i |
Case-insensitive matching (ignores uppercase/lowercase) | grep -i "error" server.log |
-r / -R |
Recursive search through directories (-R follows symlinks) |
grep -rn "DB_PASSWORD" /var/www/ |
-n |
Prefixes matching output lines with their 1-based line number | grep -n "main()" src/app.c |
-v |
Inverted match (selects all lines NOT containing the pattern) | grep -v "^#" /etc/ssh/sshd_config | grep -v "^$" |
-E |
Extended regular expressions (ERE: +, ?, |, ()) |
grep -E "(404|500)" access.log |
-c |
Outputs count of matching lines instead of line contents | grep -c "Failed password" /var/log/auth.log |
While grep searches within file contents, find traverses directory trees searching for files matching metadata attributes like name, modification timestamp, size, owner, and permissions.
# 1. Search for a file by exact or wildcard name (case-insensitive)
find /var/www -iname "*.php"
# 2. Search only regular files (-type f) or directories (-type d)
find /etc -type f -name "*.conf"
# 3. Find files larger than 100 Megabytes
find / -type f -size +100M -exec ls -lh {} + 2>/dev/null
# 4. Find files modified within the last 24 hours (1 day)
find /home/student/projects -type f -mtime -1
# 5. Find files with insecure 777 permissions and remediate to 755
find /var/www/html -type d -perm 0777 -exec chmod 755 {} \;
Linux includes robust CLI tools to inspect differences and compute statistics on files:
wc (Word Count): Computes newline count (wc -l), word count (wc -w), and byte count (wc -c).
wc -l /etc/passwd
# Example output: 42 /etc/passwd
sort & uniq: Sorts text records alphabetically or numerically, and dedupes adjacent lines:
# Extract client IPs from nginx logs, sort, deduplicate, and count occurrences
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -n 10
diff: Compares two files line-by-line and outputs additions and deletions. The unified format (-u) is used worldwide for software patches:
diff -u original_config.ini updated_config.ini > change_patch.patch
file: Inspects file header magic numbers to identify true file type regardless of filename extension:
file document.pdf
# Output: document.pdf: PDF document, version 1.7
Terminal Lab Exercise: Open your Linux terminal (or Ubuntu WSL) and execute this sequence to practice real-world file manipulation workflows:
# Step 1: Create a dedicated scratch workspace
mkdir -p ~/file_lab && cd ~/file_lab
# Step 2: Create three sample files using touch and echo
echo "Ubuntu,Debian,CentOS,RHEL,Fedora" > distros.txt
echo "Python,C,Java,Go,Rust,Bash" > languages.txt
touch empty_file.log notes.md
# Step 3: Inspect line counts and word counts
wc -l -w distros.txt languages.txt
# Step 4: Make an archived backup copy preserving timestamps
cp -a ~/file_lab ~/file_lab_backup
# Step 5: Search for specific text patterns
grep -i "debian" distros.txt
# Step 6: Find all text files in lab directory
find . -type f -name "*.txt"
# Step 7: Clean up lab directory
cd ~ && rm -rf ~/file_lab ~/file_lab_backup
echo "Lab completed successfully!"
cat dumps the complete content of a file straight to standard output in one continuous stream, which can freeze or flood terminal buffers on massive files. less is an interactive terminal pager that reads files page-by-page, allowing forward and backward scrolling without loading the entire file into memory.
touch does not delete or alter the file's contents. It updates the file's access time (atime) and modification time (mtime) to the current system time.
cp -r recursively copies directories but may alter file ownership, creation timestamps, and resolve symbolic links into normal files. cp -a (archive mode) enables -dR --preserve=all, preserving directory recursion, symbolic links, file permissions, ownership (UID/GID), and original timestamps.
mv is an instantaneous atomic pointer update that rewrites the directory entry to point to the existing inode without moving data blocks. Across different filesystems, mv physically copies the file bytes to the destination filesystem, verifies data integrity, and removes the source file.
-f option stands for follow. It keeps the file stream open and prints newly appended lines to the terminal in real time as processes write to the file.
find traverses the filesystem directory hierarchy to match files and folders based on metadata (filename, size, modification date, owner, permissions). grep opens files and searches inside their text contents for specific strings or regular expressions.
.txt or .exe) are purely cosmetic and not authoritative. The file command inspects the initial byte headers (“magic numbers”) of a file to accurately identify its true binary or text format.
uniq utility only checks for duplicate lines that are adjacent to each other. Running sort first groups all identical lines together so uniq can detect and deduplicate them.
- and added lines with +. It forms the standard patch format understood by Git and the Unix patch command.
-exec rm {} + with -exec ls -ld {} + or run find ... -print first to verify the exact list of matched files before applying destructive commands.
rm. While specialized forensic tools (such as ext4magic or testdisk) can sometimes recover unoverwritten disk blocks, production best practice relies on scheduled backups, snapshots (LVM/Btrfs/ZFS), or using trash-cli utilities.
-N flag: less -N filename.txt. If already inside less, type -N and press Enter to toggle line numbers dynamically.
-E flag interprets the search pattern as an Extended Regular Expression (ERE), which enables advanced regex meta-characters like +, ?, | (alternation), and grouping parenthesis without requiring escape backslashes. It is functionally identical to the legacy egrep utility.
zgrep, zcat, or zless. For example: zgrep "connection timeout" /var/log/nginx/access.log.2.gz searches inside the gzip compressed archive without extracting it to disk first.
head -n -5 outputs the entire file except for the last 5 lines. tail -n +5 outputs the entire file starting from line number 5 all the way to the end of the file.