File Display Commands / Filters / Text Processing Input/shaare/Bpec7A
File Display Commands
cat → Show entire content
cat -A → Show non-printable characters
more → Paginate output
less → Same as more but allows navigation with arrow keys
head → Show top lines (default: 10)
tail → Show bottom lines (default: 10)
head -2 myfile
tail -2 myfile
Filters / Text Processing Input
cut→ Cut inputawk→ List by columnsgrep&egrep→ Search by keywordsort→ Sort in alphabetical orderuniq→ Remove duplicate lineswc→ Word count (including lines)
cut Command Examples
cut -c1 filename→ First letter of each linecut -c1,2,4 filename→ Characters 1, 2, and 4cut -c1-3 filename→ Range: characters 1 to 3cut -c1-3,6-8 filename→ Ranges: characters 1-3 and 6-8cut -b1-3 filename→ Byte range 1 to 3cut -d: -f6 /etc/passwd→ Field 6 using:delimitercut -d: -f6-7→ Fields 6 and 7ls -l | cut -c2-4→ Extract character range 2 to 4 fromlsoutput
awk Command Examples
awk '{print $1}' file→ Print 1st column of filels -l | awk '{print $1, $3}'→ Print 1st and 3rd columns fromls -lls -l | awk '{print $NF}'→ Print last columnawk '/jerry/ {print}' file→ Search for "jerry" and print matching linesawk -F: '{print $1}' /etc/passwd→ Use:as delimiter, print 1st fieldecho "hello tom" | awk '{$2="adam"; print}'→ Replace column 2 with "adam"awk 'length($0) > 15' file→ Print lines longer than 15 characters
grep Command Examples
grep keyword file→ Search for keywordgrep -c keyword file→ Count matching linesgrep -i keyword file→ Ignore casegrep -n keyword file→ Show line numbersgrep -v keyword file→ Invert match (everything except keyword)grep keyword file | awk '{print $1}'→ Pipe grep to awk to extract 1st columnls -l | grep keyword→ Filterls -loutput by keywordegrep -i "keyword1|keyword2" file→ Search for multiple keywords (case-insensitive)
sort Command Examples
sort file→ Sort alphabeticallysort -r file→ Sort in reverse ordersort -u file→ Sort and remove duplicatessort file | uniq→ Same as above with explicit uniqsort file | uniq -d→ Show only duplicate linessort file | uniq -c→ Count duplicatessort -k4 -n file→ Sort by 4th field (numeric)ls -l | sort -k4 -n→ Sort by size fromls -l
wc Command Examples
wc -c filename→ Byte countwc -w filename→ Word countwc -l filename→ Line countls -l | wc -l→ Count lines fromls -lwc -l filename→ Count number of lines in file
cmp / diff
diff→ Compare files line by linecmp→ Compare files byte by byte
tar / gzip
tar cvf export.tar somedir→ Compress directorytar xvf export.tar→ Extract tar archivegzip export.tar→ Compress with gzipgzip -d export.tar.gz→ Decompress gzip archive
truncate Command
truncate -s10 filename→ Chop file to 10 bytestruncate -s60 filename→ Extend file to 60 bytes
Combining & Splitting Files
cat file1 file2 file3 > fileN→ Combine multiple files into one
(19)