Operations 7 min read

Master Linux File Management: cat, cmp, diff, file & find Commands Explained

Learn how to use essential Linux file management commands—cat, cmp, diff, file, and find—including their syntax, key options, and practical examples such as concatenating files, comparing contents, displaying file types, and searching directories, all illustrated with clear code snippets and screenshots.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux File Management: cat, cmp, diff, file & find Commands Explained

cat

cat command description: This command concatenates files and prints them to the standard output.

Syntax: cat [options] [filename] Parameters explanation:

image.png
image.png

Usage examples:

Write an incrementing sequence to test1.txt:

for i in $(seq 1 10); do echo $i >> test1.txt ; done

Display the contents of test1.txt: cat test1.txt Write test1.txt with line numbers to test2.txt: cat -n test1.txt > test2.txt Show test2.txt contents: cat test2.txt Empty test1.txt using /dev/null: cat /dev/null > test1.txt After emptying, cat test1.txt shows no output.

image.png
image.png

cmp

cmp command description: This command compares two files byte by byte and reports the first difference. If the files are identical, it produces no output. It can also read from standard input when no file names are given.

Syntax:

cmp [-clsv] [-i <bytes>] [--help] [file1] [file2]

Parameters explanation:

image.png
image.png

Usage examples:

Write a sequence 1‑5 to test1.txt:

for i in $(seq 1 5); do echo $i >> test1.txt ; done

Compare test1.txt and test2.txt: cmp test1.txt test2.txt The command reports that the first line differs.

image.png
image.png

diff

diff command description: This command compares files line by line and reports differences. When directories are given, it compares files with the same name in each directory but does not recurse into sub‑directories.

Syntax: diff [options] file1 file2 Parameters explanation:

image.png
image.png

Usage example (side‑by‑side output): diff test1.txt test2.txt -y -W 50 The result shows the differing lines in a columnar format.

image.png
image.png

file

file command description: This command determines the type of a file.

Syntax: file [options] file Parameters explanation:

image.png
image.png

Usage examples:

Show the type of test1.txt: file test1.txt Output indicates "ASCII text".

Show the type of test2.txt without the filename:

file -b test2.txt
image.png
image.png

find

find command description: This command searches for files and directories that match given criteria within a directory hierarchy.

Syntax: find [options] [path] [expression] Parameters explanation:

image.png
image.png

Usage examples:

List all ".txt" files in the current directory and sub‑directories:

find . -name "*.txt"
image.png
image.png

Find all zero‑length regular files on the system and list their full paths:

find / -type f -size 0 -exec ls -l {} \;
image.png
image.png
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CATfile-managementdiffcommand-linefindCMP
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.