Created by Christopher Lawrence GS '24
Hello and welcome back to our Terminal tutorial. Today we are going to learn how to: check network connectivity, download files from the internet, check our computers processes, and do some command line magic. Lets get started.
If you've ever lost connection to the internet or have been plagued by slow speed's, you may have asked yourself: "Is my computer even connected to the internet?"? "Is it me?" "Has [Website] crashed"?. Well there is a simple command line utility that can answer this question for you, ping.
ping or (Packet Internet Groper) is a command used to check network connectivity of computers, websites, databases, etc. It can also be used to check the speed of your internet connection, and see if other devices are connected to your wifi/router.
The name comes from sonar terminology, where a pulse of sound is sent out and the echo is used to detect objects. When the echo returns to it is registered as a "ping". Lets try it out.
ping -c 5 www.askjeeves.com
Use Ctrl + C to quit the first command. The -c flag, takes a number for input and will execute that number of pings, before printing out a summary of the connection statistics.
Once we have established that our computer is connected to the internet and other devices, we might want to download data from these places, or websites. For this we can use the wget.
I pronounce this command as widget, others pronounce it as w-get. The others are wrong! wget is pre-installed on most Linux distributions today. To check whether the wget package is installed on your system, open up your console, type wget, and press enter.
wget is a non-interactive downloader used to download files remotely. wget has been designed for strength, so if a download fails due to a network problem, it will keep retrying until the whole file has been retrieved. Hows that for dedication? It can be used to download many types of files, documents, and images if you know their location on the inter-webs. For example.
wget, ping, and many of the other commands we have learned up to this point are commands that we submit to our computer. When our computer receives these commands, they become processes that our computer must carry out. This also applies to the commands we submit using the mouse. So browsing the internet, listening to music, writing a document, opening and closing files, all can be visualized as processes or jobs that our computer must carry out. To view current processes and those active or running we will use the top command.
top provides a real-time view of all the current processes running on the system. This command provides a summary of all the tasks, tells you who is running them, the percentage of the cpu (your computers brain) being used, available memory, and much much more. It can be overwhelming, especially in the beginning. It is imperative that you know where the information you are looking for can be found on this screen. Much of this information can be found at the top, and the individual commands below the header row (shown in white) on the far right.
To exit we can simply type q. Or you can use Ctrl + C.
Differences between Ctrl + C and Ctrl + Z
When I first started using the command line, I would often write programs that would go indefinitely and use up a lot of my computers time and resources. On top of that I had no idea how to stop a command, a job, or a program. I had seen others use Ctrl + Z to stop a command, so I started using this command. Well, it doesn't completely stop the job, rather it suspends it. Say for instance, I have a top command running, but instead of using q or Ctrl + C to exit, I use Ctrl + Z, we can see that the job is suspended.
Ctrl + Z
To view the current or suspended processes we will use the job command. Its simpler than top, but not as generally informative. jobs can be used to see the status of certain jobs if you know their number or PID. This can be seen in the first column on the left in the gif above. To resume this job, we will use fg.
fg stands for foreground. We are telling our computer to resume the application, bring it up to the foreground so we can see it. 1 corresponds to the job id that we want to resume. In our case it is 1.
To exit, use q or Ctrl + C
While Ctrl + C may stop a running command and free up our terminal, we might not always be able to use Ctrl + C to stop a command or a application. We might be using an application with the mouse and it becomes unresponsive, freezes on us, and eventually crashes. When we try to start it again, nothing happens and the application may tell us that it is already running. The only solution here is to..........Ex-terminate it.
Luckily, Linux has some commands to do just that.
pkill/ kill/ killall
The main difference between these commands is the information required by to kill certain processes.
kill will terminate processes based on the PID (Process ID number). We saw this briefly when we ran top. The PID for running processes can be found in the first column on the left, in the top screen. Seen above. We could also use pidof (pid of) to find the pid of a process. pidof is case sensitive, so make sure you have the spelling correct!
35051 35029 34992 34983 34958 34957 34877
killall and pkill
These commands terminate processes based on their names and their attributes. For example, say zoom crashes, or you're tired of being on zoom.
killall will do the same thing as kill, except instead of the pid we are using the common name for the process. Its kind of like its own language.
Side Note: You can only stop processes of which you are the user. The user of a certain process and the common name of the process can found using the top command as well.
Command Line Magic
Up arrow (Last command)
Through out this tutorial, I have been typing these commands into the terminal, letter by letter, often repeatedly because I screw up, or cant get the recording right. This is very similar to what working on the command line is like for computational biologists, maker space hobbyists, and command line lovers. However, we don't have to type our command each time. Instead, its much simpler, to just use the up arrow to view past commands we have submitted. Like so.
exit with Ctrl + C
Hit the up arrow.
These commands go back pretty far. But it can be a hassle to remember them chronologically in our head. To get around this, we can use the history command.
This command will show us a list of all the commands we have submitted during this session and as far back as it can remember.
Instead of retyping the same command, we can use the command number (shown in the first column) to re-run a process.
![insert command # here]
The history command is useful because it provides a database of past commands, that with the right tools, can be searched and parsed accordingly. And it also saves you the hassle of retyping the same command over and over again.
When it comes to working with files and doing things with them, it can be equally as repetitive to type the complete path to the file. Say for instance, we want to change directory to the desktop. We might type
But, programmers are notorious for counting key strokes and when in doubt automate it yadda, yadda, yadda, so instead of typing it out.............we can just hit tab to autocomplete. Lets try that again, but use tab to autocomplete.
type cd De (then hit tab)
It autocompletes as seen above. This allows you to get to files quickly, without having to know the complete file path off the top of your mind.
Lastly, running all these commands on the terminal can crowd the screen, making it difficult to read the output of commands. Especially if those outputs are several lines long, with multiple columns and headers. To clean up our terminal screen we can use clear and Ctrl + L.
ping -c 5 www.askjeeves.com
ping -c 5 www.google.com
Ctrl + L
clear will clear the terminal, providing a fresh terminal environment for working in. If typing clear is too many key strokes for you, you can also use Ctrl + L
This completes this tutorial.
In the next tutorial we will learn how to install new programs and packages that our computer or we may need to perform certain operations. We only have two more tutorials left, then you will be a command line aficionado!!