Master Essential Linux Shell Tools for Text Processing and File Management
This guide walks you through the most frequently used Linux shell utilities—find, grep, xargs, sort, uniq, tr, cut, paste, wc, sed, and awk—explaining their core options, practical examples, and how to combine them for powerful text and file manipulation tasks.
1. find File Search
Examples of using find to locate *.txt and *.pdf files, regex search, negation, depth control, type, time, size, permission, user, and actions such as -delete and -exec for copying or changing ownership.
find . \( -name "*.txt" -o -name "*.pdf" \) -print
find . -regex ".*\(\.txt|\.pdf\)$"
find . ! -name "*.txt" -print
find . -maxdepth 1 -type f
find . -type d -print
find . -atime 7 -type f -print
find . -type f -size +2k
find . -type f -perm 644 -print
find . -user weber -type f -print
find . -type f -name "*.swp" -delete
find . -user root -type f -exec chown weber {} \;
find . -mtime +10 -name "*.txt" -exec cp {} OLD \;2. grep Text Search
Basic grep usage with common options: -o (only matching part), -v (invert match), -c (count), -n (line number), -i (ignore case), -l (list filenames), and recursive search.
grep -c "text" filename
grep -R -n "class" .
grep -e "class" -e "virtual" file
grep -lZ "test" * | xargs -0 rm3. xargs Argument Conversion
xargsturns input data into command‑line arguments, enabling pipelines such as grep + find. Useful flags include -d (delimiter), -n (max args per command), -I {} (replace token), and -0 (null delimiter).
cat file.txt | xargs -I {} ./command.sh {}
find source_dir -type f -name "*.cpp" -print0 | xargs -0 wc -l4. sort Sorting
Sorting options: -n (numeric), -d (dictionary), -r (reverse), -k N (column N), and examples.
sort -nrk 1 data.txt
sort -bd data.txt5. uniq Duplicate Removal
Remove duplicate lines, count occurrences, or list only repeated lines.
sort unsort.txt | uniq
sort unsort.txt | uniq -c
sort unsort.txt | uniq -d6. tr Character Translation
Common tr usages: substitution, deletion, complement, squeezing, and character classes.
echo 12345 | tr '0-9' '9876543210'
cat file | tr '\t' ' '
cat file | tr -d '0-9'
cat file | tr -c '0-9' '
'
cat file | tr -s ' '7. cut Column Extraction
Extract specific fields or characters with cut, set delimiter with -d, and choose units ( -b, -c, -f).
cut -f2,4 filename
cut -f3 --complement filename
cut -d ";" -f2 filename
cut -c1-5 file8. paste Column Merging
Combine files column‑wise; default delimiter is a tab, customizable with -d.
paste file1 file2
paste -d "," file1 file29. wc Word/Line/Byte Count
Count lines ( -l), words ( -w), or bytes ( -c).
wc -l file
wc -w file
wc -c file10. sed Stream Editing
Single‑line ( s/text/replace/) and global ( s/text/replace/g) replacements, in‑place editing with -i, removing empty lines, back‑references, and inserting characters.
sed 's/text/replace_text/' file
sed 's/text/replace_text/g' file
sed -i 's/text/replace_text/g' file
sed '/^$/d' file
sed 's/^.{3}/&\//g' file11. awk Data‑Stream Processing
Awk script structure with BEGIN, main block, and END. Built‑in variables ( NR, NF, $0, $1 …), pattern matching, field printing, aggregations, external variable passing, loops, and implementing head / tail functionality.
awk 'BEGIN{print "start"} {print} END{print "end"}' file
awk '{print NR":"$0"-"$1"-"$2}' file
awk 'END{print NR}' file
awk 'NR<=10{print}' file # head
awk '{buffer[NR%10]=$0} END{for(i=0;i<10;i++) print buffer[i]}' file # tail12. Iterating Lines, Words, Characters
Loop over file lines with while read or awk, iterate words with for word in $line, and iterate characters using Bash parameter expansion.
while read line; do echo $line; done < file.txt
awk '{print}' file.txt
for word in $line; do echo $word; done
for ((i=0;i<${#word};i++)); do echo ${word:i:1}; doneSigned-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.
