Linux Commands Series 4 : Directories and Files 2
When working with files, after creating them (as we practiced in the previous tutorial), we want to be able to display what is inside them. In this regard, Linux offers many commands to make their content directly visible into the console, without having to use an external text editor. In this tutorial we will present 5 of these commands. So let's start !
1. cat : this command will print all of the content of the file into the console
if you want to display the number of the line before each line, you can use the option -n :
cat command stands normally for concatenate, its job is to concatenate the contents of many files and output it to the standard output, Let's showcase this behaviour :
create a file test1.txt with 3 lines of text
create a file test2.txt with 5 lines of text
apply cat command on them both
2. head : when the file has a lot of content, and you just want to display some lines from the top, you can use the head command :
by default it will write the first 10 lines to the console, if you want to specify the number of line you want it to display, use head -n :
so for example head -5 file1.txt will write the first 5 lines of the file into the console, let's show this :
3. tail : this command behaves like head command, but it displays content from the end of the file, for example :
will display the last three lines of file1.txt :
4. less : this command will print a part that fills the console, and wait for you to hit ENTER to display line by line, until you reach (END), at that point you should hit the key 'q' to go back to the terminal console :
we have modified file1.txt so as to add more content, so the upper command will give following output :
you hit ENTER to display line by line, until you reach that (END) marker, hit 'q' (quit)
5. more : this command is similar to less, it will fill the terminal with content from the file, with the percentage of the file that has been displayed, you can hit ENTER to display more, until that percentage is 100%, which will make the more command exit automatically back to the console :
will give following output :
In this tutorial we learned how to display contents of files in Linux in many flavors. Next time we will look at a command that allows us to modify the contents of a file directly from the console !
If you found the post useful, don't forget to share it around you.
Happy learning !