echo -e
(print back to terminal)
$ echo foo bar
$ echo "foo bar"
# using escape sequences (needs quotes "")
$ echo -e "foo\nbar"
cat
(print contents of files inorder)
$ cat fileA fileB ...
-n display with line numbers for all lines
less
(pager used for viewing text in the terminal itself)
PageUp, PageDown turn pages
Up, Down, Left, Right arrows navigate text
g jump to beginning of the text file
G jump to end of the text file
/searchtext search forwards
?searchtext search backwards
q quit
h help
head
tail
(reading part of files)
$ head file (first 10 lines)
$ tail file (last 10 lines)
$ head -n 15 file (space between -n15 doesn't matter)
$ head -15 file (no need of -n flag)
$ head -c 20 file (print the first 20 characters from the file)
$ head -n -2 file (print all but the last 2 lines)
$ tail -n +2 file (skip first two lines; print the rest)
$ tail -f file (follows the last lines of the file in real-time update)
cut
(print selected characters or fields from the file to console)
$ cut
-c character, starts from 1
-f field
-d specify delimiter (default is TAB)
$ cut -c 2 file (only print 2nd character of all lines)
$ cut -f 1 file
$ cut -f1 -d',' file
$ cut -f 2,5,7 file
$ cut -c 2,7 file
$ cut -c 5-10 file (both positions are included)
$ cut -c 5- file (from 5 till end)
$ cut -c -5 file (from start till 5)
tr
(translate; replace text) (waits for input, if run without a file input)
$ tr SET1 SET2
$ tr "a" "z"
$ tr "()" "[]" (elements of the SET1 are replaced with corresponding element from SET2)
$ tr a-z A-Z
$ cat file | tr a-z A-Z (translate and print)
$ cat file | tr a-z A-Z > file (tr and write) (alternatively | cat > file)
$ tr -c 'a' 'z' (complement of set1 replaced by set2. Ex. abcd -> azzz)
$ tr -d 'a' (delete all of set. Ex. abcda -> bcd)
$ tr -s [:space:] (squeeze multiple occurances of set into one. Ex. baaaad -> bad)
sort
(default separator is not TAB but space here, specify delimited as TAB using -t $'\t'
)
-r reverse sort
-n numerical sort, 9 > 43 in normal, but correct with sort -n
-f fold-case: ignore-case; both upper and lower case of a letter are equal in sort comparison
+X ignores first X fields, X = any number
-k n key; sort only on the nth field of text
-t 'char' use char as field delimiter (ex: -t '|')
-u unique; show only distinct lines
-m list1 list2 merge already sorted files; do not sort
uniq
(only adjacent duplicate lines are detected unlike sort -u since data here can be “unsorted”)
-c display lines with count of occurance
-u only unique lines
-d only duplicate lines
-i ignore case
-fN skip the first N fields in comparison
rev
(reverse text)
$ echo hello | rev
olleh
paste
(display lines as columns (newspaper-like) from file(s), shows corresponding lines with TAB delimiter between them by default)
$ paste file equivalent to cat command
$ paste -s file combine all lines in file into one, TAB separated
$ paste file1 file2 prints corresponding lines from both the files side-by-side on terminal
$ paste -d: file1 file2 custom delimiter ":" for output
$ paste -d")" file1 file2 other chars need double-quotes
$ paste - - < file fold two adjacent lines into two columns
$ paste -d ";" - - - < file1 fold three adjacent lines into three columns
expand
(convert TAB to group of spaces) (doesn’t modify file in-place but prints to console)
unexpand -a
(convert group of all spaces in file to TAB) (doesn’t modify file but prints to console)
join
(join fields from multiple files; based on a common field)
# joins by field = 1 by default
$ join file1 file2
# (-1 is file1.txt, -2 is file2.txt) (field 2 in file1.txt = field 1 in file2.txt)
$ join -1 2 -2 1 file1.txt file2.txt
split
(split lines from a file into multiple files)
# splits into different files after threshold line, default = 1000, created file name = x**
$ split file.txt
# split first 5 lines from file1.txt and files named fileaa,fileab, ... are created
$ split -l 5 file1.txt file
wc
(display the number of lines, words, and bytes respectively)
$ echo hello | wc
1 1 6
-l only lines
-w only words
-c only bytes
-L length of longest line in file
nl
(displays line with numbers at the left side; starts with 1) (works exactly like cat -n
)