cat
The cat command concatenates and prints the contents of files.
cat <filename>
will read the contents of a file and print them out. For example,cat instructions.txt
will read in from the instructions.txt file and then print the contents out to the screen.If we provide cat with multiple files, it will concatenate their contents and output them.
cat peanutbutter.js jelly.css
will output peanutbutter.js first and immediately after print the contents of jelly.csscat <filename> cat <file1> <file2>
less
The
less
command displays the contents of a file, one page at a time. We can navigate forwards and backwards through the file, which is especially useful with very large files.less somefile.txt
will display the contents of somefile.txt using less.When viewing a file using less :
Press space to go to the next page of the file
Press b to go back to the previous page
Press Enter or Down arrow to scroll by one line
to search, type forward slash / followed by a pattern
press q to quit
less <filename>
tac
tac (cat spelled backwards) will concatenate and print files in reverse. It prints each line of a file, starting with the last line. You can think of it as printing in reverse "vertically"
tac file.txt
rev
The
rev
command prints the contents of a file, reversing the order of each line. Think of it as a "horizontal" reverse, whereas tac is a "vertical" reverse.rev file.txt
head
The head command prints a portion of a file, starting from the beginning of the file. By default, it prints the first 10 lines of a file.
head warAndPeace.txt
would print the first 10 lines of the warAndPeace.txt fileWe can also specify a number of lines for head to print using the -n option (or --lines) followed by an integer.
head -n 21 warAndPeace.txt would print the first 21 lines of the warAndPeace.txt file
We can also use an even shorter syntax to specify a number of lines:
head -3 filename.txt
will print the first 3 lines of the file.head filename.txt head -n 13 filename.txt
tail
The tail command works similarly to the head command, except it prints from the END of a file. By default, it prints the last 10 lines of a file.
tail warAndPeace.txt
would print the last 10 lines of the warAndPeace.txt fileThe same -n option with head also works with the tail command.
tail filename.txt
wc
The word count command can tell us the number of words, lines, or bytes in files. By default, it prints out three numbers: the lines, words, and bytes in a file.
We can use the -l option to limit the output to the number of lines.
The -w option limits the output to the number of words in the file.
wc -l students.txt
sort
The sort command outputs the sorted contents of a file (it does not change the file itself). By default, it will sort the lines of a file alphabetically.
sort names.txt
would print each line from names.txt, sorted in alphabetical orderThe -r option tells the sort command to sort in reverse order.
sort names.txt -r
would print each line from names.txt, sorted in REVERSE alphabetical order.sort filename.txt sort -r filename.txt
Sorting Numerically
The -n option tells the sort command to sort using numerical order.
sort -n prices.txt
would print each line from names.txt, sorted in numerical order.We could also reverse it with
sort -nr prices.txt
The -u option tells the sort command to ignore duplicates and instead only sort unique values
sort -n prices.txt sort -u prices.txt