Internet is full of basic Linux commands you probably are familiar with such as ls for listing files inside a directory, pwd for printing the current working directory, cd to navigate through directories and many other useful commands, but it is not that important to write them all in here.
I guess you are tired of seeing the same command in ten different internet websites related to the Linux distribution. You should be!
Command line skills are great when it comes to solving real problems in Linux such as killing a process which is consuming the entire memory for example, downloading a video from Youtube at the best quality, getting information on a specific file and even browsing the internet when it is needed to save the bandwidth.
All this comes from experience. An upcoming Linux ninja spends hours of his life in front of the terminal trying to make it work.
How do I make use of the cat command in full
Most novice Linux users think the cat command is used only for displaying the content of a file in the console, but that for sure is not true. Of course it is very practical to use this command to display a .json file in the console especially when you have crafted JSON data for a request to a rest api. For example I display contents of a file with the cat command like shown below.
cat temperature.json
The output of the above command is shown below.
{ "measured_at":"2015-12-25 21:55:57.188743+00:00", "sensor_id":"somekey", "temperature":14 }
I will use the temperature.json file for a POST request with the curl command line tool on my Linux machine, but we will discuss it another time.
So almost everyone knows how to use the cat command for displaying the content of a file. But have you ever used it to create a new file?
Probably not. The following command makes use of this case.
cat >newfile.txt
Now let me use the cat command to copy the contents of one file to another one.
cp oldfile.txt >newfile.txt
The above command copies the content of the oldfile.txt to the newfile.txt. It is very practical for copying files and doing backups. I prefer this over the cp command.
A very nice option I like to make use of when playing around with the cat command is the -n option which comes in play when you want to display the line number in front of each line. For example the following command helps me to know how many lines of JSON code is there in temperature.json file.
cat -n temperature.json
And the following output is displayed on my console after running the above command.
1 { 2 "measured_at":"2015-12-25 21:55:57.188743+00:00", 3 "sensor_id":"somekey", 4 "temperature":14 5 }
And if you try the following command
tac temperature.json
the file is going to be displayed in reverse. I am sure many of novice users do not know about this cool trick.
} "temperature":14 "sensor_id":"somekey", "measured_at":"2015-12-25 21:55:57.188743+00:00", {
How do I make use of the ls command in very practical ways
The ls command looks pretty simple to most Linux users, but there is so much power in it. For example if you want to know if some file exist in the directory a pipe can be created like shown below.
ls | grep somefile
The above command is a combination of two commands: the ls command and the grep command which is basically used to grep text. So the ls command lists the files inside the directory and passes the output as standard input for the grep command through the | operator.
The grep command tells us if the file exists or not by printing the text on the console if file is founded or by printing nothing if there is no file which we are looking for.
And If I want to display hidden files inside a directory I use the following command.
ls -a
Note: The files that start with a . in their name are considered by Linux as kind of private files and they are hidden from the normal eye. You have to run the above command to view them.
There is also another option I like to teach to you guys. The t option which is being used to sort files based on the last time of their modification.
ls -t
The above command is nice to use and helpful. But if you add also the l option to it it becomes more nice and more human readable.
ls -lt
Have you ever tried to determine the kind of files you have inside your directory, but the ls command does not help?
For example when doing just ls like shown below it is real hard to know the type of files inside your directory. Are you dealing with a subdirectory? Are you dealing with an executeable file or a just a normal one?
ls
The output of the above command is shown below.
normal.txt unixmen
This is the moment where the F option comes in play. Let me give it a try.
ls -F
The output of the above command is shown below.
normal.txt unixmen/
As you can see from the above output it is for sure different than the one printed on the console when not using the ls -F command but just ls.
Linux uses the / to tag directories and nothing for normal files. So based on this we can analyze the above output and say that unixmen is directory and normal.txt is just a normal file.
It is real important to practice every command you learn in this tutorial. I have mentioned this in the past and will do till the moment I stop writing tutorials.
How do I know that my internet connection is not dead
There are many times when I want to visit a website it is not possible to reach in there. Is a problem on their part or mine?
I find it real hard to believe that Google will go offline anytime soon so I fire up the terminal on my Ubuntu machine and use the ping command to ping the website.
ping www.google.com
If there is a reply back then it is for sure the problem is not on my part.
How do I edit files from the terminal
I have installed this amazing text editor called vim which helps me to edit and study normal files from the console. I find it real useful when I want to write some JSON data for a POST request for example. If you use Ubuntu you can install vim with the following command.
sudo apt-get install vim
Then to open a file for editing just type the following command
vim file2editinhere
and hit Control and the i on your keyboard. Once you have finished writing some content to the file just hit Control and then w for writing.
And if you want to quit just hit Control and then q.
Conclusion
In this first part of using the terminal like a pro we took a look at some real practical examples that are very useful and new to novice Linux users. In the second part we will take a look at some advanced stuff such as validating and correcting and html file and even uploading content on Youtube via the command line.