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.
cat
cat command description: This command concatenates files and prints them to the standard output.
Syntax: cat [options] [filename] Parameters explanation:
Usage examples:
Write an incrementing sequence to test1.txt:
for i in $(seq 1 10); do echo $i >> test1.txt ; doneDisplay 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.
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:
Usage examples:
Write a sequence 1‑5 to test1.txt:
for i in $(seq 1 5); do echo $i >> test1.txt ; doneCompare test1.txt and test2.txt: cmp test1.txt test2.txt The command reports that the first line differs.
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:
Usage example (side‑by‑side output): diff test1.txt test2.txt -y -W 50 The result shows the differing lines in a columnar format.
file
file command description: This command determines the type of a file.
Syntax: file [options] file Parameters explanation:
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.txtfind
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:
Usage examples:
List all ".txt" files in the current directory and sub‑directories:
find . -name "*.txt"Find all zero‑length regular files on the system and list their full paths:
find / -type f -size 0 -exec ls -l {} \;Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
