tr (translate)
The tr command is used to translate or delete characters.
$ cat sample.txt | tr '[a-z]' '[A-Z]'
$ cat sample.txt | tr -d ' '
cut (extract characters/fields)
The cut command is used to extract characters or fields from each line of a file or stdin.
-c
Extracts specified characters
-f
Extracts specified fields
-d
Specifies the delimiter (default is tab)
$ cut -c 1-5 sample.txt
$ cut -d ':' -f 1,3 /etc/passwd
head and tail
The head command displays the first part of a file, and tail displays the last part.
$ head -5 sample.txt
$ tail -10 sample.txt
$ tail -f logfile.log
cmp (compare two files byte by byte)
The cmp command compares two files byte by byte and reports the first difference.
$ cmp file1.txt file2.txt
comm (compare two sorted files line by line)
The comm command compares two sorted files line by line and produces three columns of output:
- Column 1: Lines unique to file1
- Column 2: Lines unique to file2
- Column 3: Lines common to both files
$ comm file1.txt file2.txt
$ comm -12 file1.txt file2.txt
wc (word count)
The wc command counts lines, words, and characters in a file.
-l
Counts the number of lines
-w
Counts the number of words
-c
Counts the number of characters
$ wc sample.txt
$ wc -l sample.txt
sort
The sort command sorts the lines of a text file.
$ sort sample.txt
$ sort -r sample.txt
$ sort -n numbers.txt
$ sort -u sample.txt
grep (Global Regular Expression Print)
The grep command searches for patterns in files and prints matching lines.
-n
Shows line numbers along with matching lines
-i
Ignores case (case-insensitive search)
-v
Inverts the match (shows non-matching lines)
$ grep "error" logfile.log
$ grep -n "warning" logfile.log
$ grep -i "ERROR" logfile.log
$ grep -v "success" logfile.log