Working with Files
Shivm Soaini
Math Major Turned AI Enthusiast: Mastering Computer Vision | Building Robust Python Code | Delving Deep into the Math of AI | Sharing Knowledge Through Writing | Ready to Transform Ideas into Reality
The cat command
The cat command concatenates and prints the contents of files.
cat filename will read the contents of the 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.
cat <filename>
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.css file.
cat <file1> <file2>
The less command
The less command displays the contents of a file, one page at a time. We can navigate forwards and backwards through the file using arrow keys, which is especially useful with very large files. Press q to get out of it.
less somefile.txt will display the contents of somefile.txt using less.
less <filename>
When viewing a file using less..
In the example below, I have opened the bash_history file using less ~/.bash_history command.
The tac command
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".
The rev command
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.
The head and tail commands
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 file.
Otherwise, you have a -n option which will print the first n lines of the file.
head warAndPeace.txt would print the first 10 lines of warAndPeace.txt file.
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.
The -n option works here too. But instead of printing the first n, it prints the last n lines of the file.
tail warAndPeace.txt would print the last 10 lines of the warAndPeace.txt file.
The wc command
The word count command wc 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 of the file.
In the example below, our file readinglist.txt contains 7 lines, 17 words and 127 bytes.
The sort command
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 order.
sort filename.txt
The -r option tells the sort command to sort in reverse order.
sort -r names.txt would print each line from names.txt, sorted in REVERSE alphabetical order.
sort -r filename.txt
We can use the -n option to sort a file containing numerical contents.
Standalone sort command would not work as we want it to, because it'll sort the contents based on the first digit only.
Let's demonstrate this with a file "prices.txt".
The -u option tells the sort command to ignore the duplicates and instead only sort the unique values.
We can specify a particular "column" that we want to sort by, using the -k option followed by a field number.
In this example, sort -nk 2 data.txt tells sort to use the numeric sort and to sort using the 2nd field.
Thanks for Reading : ). Hope you find it useful.