Icons made: Dave Gandy, Source- https://www.flaticon.com/authors/dave-gandy"

Introduction to Linux: Part 2 — Time To Draw Swords

Sarthak Pranesh
VIT Linux User Group

--

In the last part we went through understanding what Linux is and different Linux commands to help you use the system more efficiently. If you have not gone through the first part then here is the link “Introduction to Linux: Part 1 — First Blood , do give it a read.

In this part we will understand what are packages and package managers. We will also go through some of the commands that will help us manage packages (software) in your Linux system and some other commands that will help you use your system more efficiently. So why wait, lets dig in.

What are Packages?

A package format is a type of archive containing computer programs and additional metadata needed by package managers — from Wikipedia

In simple words, Packages are pieces of software that can be installed on the Linux System. The term package refers to a file archive containing all the files that come with a particular application. Just like we have .exe executable for Windows based system in the similar manner we have different packing formats in the Linux System. Some of these packaging formats are deb, rpm, snap, etc.

What are Package Managers?

A package manager keeps track of what software is installed on your computer, and allows you to easily install new software, upgrade software to newer versions, or remove software that you previously installed. As the name suggests, package managers deal with packages: collections of files that are bundled together and can be installed and removed as a group — from Debian.org

As the name suggests, Package Manager helps you make sure you are able to install the required pieces of packages that you need on your Linux System. It also helps you to upgrade the packages which are already installed or remove the once which you don’t require. Therefore the Package Managers are an essential component that completes the Linux System.

Just like we have many different Linux Distributions, similarly we have many different Package Managers that achieve the same goal but with different concepts, packaging formats and techniques. We won’t go in to much dept about these concepts and techniques but will discuss about the Ubuntu package manager in the next section.

APT Package Managers

APT or Advance Package Tool is the most popular package manager in the world of Debian based distributions such as Ubuntu, Pop OS, etc. It is free software user interface aimed to work with core libraries for installation, removal and management of packages on the Linux system. It is the main package manager in Ubuntu and is developed under ‘The Debian Project’.

Lets start with some commands that would help you keep your system up-to date and running smoothly with the apt commandline interface.

Update & Upgrade

These two commands are the most essential and basic commands of the APT package manager. The apt update command updates the list of packages that can be upgraded in the Linux operating system. Whereas the apt upgrade command is responsible for the actual fetching of the newer version of the package and installing them on the system. So lets start with updating our system packages

The last line tells us about how many packages are their currently on the system that can be upgraded. We can clearly see my system has 19 packages as of now that can be upgraded to newer version.

For seeing these packages we can simply run apt list --upgradable and it will list all the packages that can be upgraded on the system.

Here we see both the package name and the new version of the package with some other details

Now as we are well informed about what packages we are going to upgrade and to what versions, we simply run upgrade using sudo apt upgrade command. It might give you a prompt in the terminal asking if you want to continue, press ‘y’ and hit enter.

… and the system starts fetching the packages over the internet and installs them on the system

Note: We need to use sudo command while we upgrade the packages on the system as we are making modifications to the system which requires admin level privileges.

Autoremove

It is used to remove packages that were automatically installed to satisfy
dependencies for other packages and are now no longer needed. This can be achieved by running sudo apt autoremove command. Running this command will also ask us if we want to continue, so press ‘y’ and hit enter.

This command essentially helps you get rid of any packages that are just lying around in the system and are of no further use

Search

This comes in very handy if you are not sure about a package or software existing in the list of the given packages or not. It uses regex to find packages that match the given name. Here we will try to search for the popular game called SuperTux 2. We achieve this by running apt search supertux

We get 4 packages that match the keyword supertux and can be installed. We want to install the first one.

Install

As the name suggests it is used to install packages or software in your Linux System right from the terminal. As searched earlier we would like to install the package named supertux, this can be done by using the command sudo apt install supertux

Here you can clearly see that the install command also downloads and installs any other dependencies required by the supertux package

Just like the upgrade command the packages are first downloaded and then installed onto the system.

Note: While we where searching for a package we did not use sudo command because we were just retrieving information from the system but not modifying the system in any manner. But while installing, removing and upgrading a package we use sudo command as we are modifying our Linux System and doing so requires Admin level privileges.

After the package is successfully installed you can type supertux2 in the terminal and enjoy the game :) but make sure you don’t close the terminal or the game will close too ( this is because of a foreground process, more on that in the next part of the series )

Remove

I hope you liked the game, don’t worry if you didn’t because now we are going to remove it ( you can choose not too if you really liked it ). To achieve this we use sudo apt remove supertux

If you read the output of the command you’ll find out that the command tells some of the packages where automatically installed and that they can be removed using sudo apt autoremove, so go head and remove them as they are no longer needed.

Please be careful about what packages you remove by using the remove command because if you removed a package that your system relies on then you’ll end up in trouble. One hard and fast rule that I follow while using my Linux System is to never touch packages that I did not install or I don’t know about. By using the above 6 commands you’ll be able to manage the installation, upgradation and removal of packages efficiently on your Linux System.

Back To Terminal Basics!

In the previous blog tutorial we discussed about some basic commands that we can run on the terminal. Here we discuss more such commands that can help us navigate and use our system more efficiently.

pushd & popd

We discussed how to use cd tool in the previous tutorial. Now lets say that we had to just temporally go to a particular directory to find some information and then we have to go back to the previous directory. This can be achieved by using cd two times, but in some cases it gets tough to remember what exact directory we were in. For this purpose we use the pushd and popd tools.

Lets say we are in our Home directory of the logged in user and we want to navigate to /etc directory, run some commands and then navigate back to our Home directory. For doing this we run pushd /etc

Here we use the pwd command both before after running pushd command to see where we are in the Linux System

As we can see that we have successfully navigated to the /etc directory, so now we can run whatever commands we wanted to run and gather any information that we need. After we are done, we use the popd tool to navigate back to the previous directory.

Here when we use the popd command we are able to navigate back to the home directory of the user

Note: We used the word tool instead of command to indicate that both pushd and popd are builtin command line tools just like cd.

which

Their are times when we aren’t sure about if a particular command exists or not, or we want to locate the command in the file system. For doing so we run the which command. Lets say we wanted to find out where is the ls command being executed from, then for doing so we can run which ls in the terminal and it will output the file path from where the ls command is being executed.

cal

Ever wondered what it will be like to see the Calendar on a terminal itself. Yes you can and the cal command helps you with that

Pretty neat haan

history

Their are instances when we want to see what commands we ran in the terminal. This can be helpful in many cases, one being we want to undo something what we did previously but don’t remember exactly what command we ran. For seeing the list of commands that we have run previously we simple type history in the terminal and hit enter.

whatis

In the previous blog we discussed about the man command and its need in the Linux System. But sometimes the output of the man command is just too overwhelming or we actually don’t want the whole documentation and only want a short but precise description of the said command. For this purpose we have the whatis command that gives us one line description about the command passed to it. For example lets run whatis cat to get the one liner description of the cat command.

This command is most useful when we want to confirm what a particular command does and by no means is supposed to replace the man command which gives us all the documentation we need to learn these commands along with there different options.

apropos

Lets say we don’t know some commands but we know what we want that command to work on, for example we want to delete a user but we don’t know the exact command to do so. In such a case we want to search all the system commands and their manuals to find the command we can use to modify a user. This is exactly what apropos command allows us to do. To find commands that can help us delete a user we simply run apropos user in the terminal. It outputs all commands with their short description that can help us with a user.

If we scroll up the list we find the deluser command, that we used in the previous tutorial to delete a user.

nano

This is a handy and simple text editor, that lets you edit files in the terminal itself. It is super simple to use, all we have to do is run nano somefilename and it will open that file for editing and if the file does not exist it will create the file for us.

For Example lets run: nano somefilename.txt and hit enter, as soon as we do that the command will create the new file if it doesn’t exist and will open a text editor in the whole terminal window. In this window we can write all the content we want to store in the file.

Now after we are done typing stuff we want to save it to the file. We do this by pressing and holding the ctrl key and then hitting the letter O on the keyboard. This will then ask you if you want to write to the file with filename shown as below

Press enter and it will show the number of line it wrote ( basically saved ) to the file.

To exit the nano text editor we press and hold the ctrl key and hit X on the keyboard.

cp

As simple as it can be the cp command is used to copy one file to another. Lets create a text file named file1.txt and store some text content in this file. By running the following commands in the terminal ( please read the blow screen shot content carefully )

As highlighted above we see that the file named file.txt has been created

Here we have used the cat command to generate and store some text in the file named file.txt by using a concept in Linux called Redirection ( hence the “>” symbol ). More about redirection will be discussed later but in simplicity here it is allowing us to use the cat command to append new content in the file. If you want to use nano text editor please feel free. As discussed in the previous tutorial, we can simply run cat file.txt in the terminal to make sure our text content was successfully stored.

Now finally we can use the cp command to copy the content form file.txt to another file named file2.txt and then verify the content of file2.txt using the cat command

The general syntax of the cp command is as follows :

cp fileToCopy destinationFile

mv

This would be the last command we’ll cover in this blog tutorial. This command is used mostly for two purpose: for moving files and for renaming files. To move one file to another directory we simply run:

mv sourceFilePath destinationFilePath

For example we create a file called temp.txt in the home directory of our user and then move it to the Downloads directory using the mv command.

Now we can try to rename this file too, lets rename it too somefile.txt. We do so by just running mv temp.txt somefile.txt

As we see above our file was renamed successfully.

Tip: For a deeper knowledge about the Linux commands discussed in this blog tutorial make sure you read their documentation/manual thoroughly by using man command

Summary

Packages are software that can be installed on the Linux System. There are different packaging formats like deb, rpm, snap, etc and similarly there are different package managers which utilize different packaging formats and techniques.

This blog further proves how much easy it is to manage your Linux System, packages and perform simple tasks. We’ll cover more basic commands, techniques such as Redirection and other concepts like Linux File System in the upcoming blog tutorials so stay tuned. Till then happy learning ;)

--

--

Sarthak Pranesh
VIT Linux User Group

Full Stack | React Native | Gopher | Open Source and Linux Enthusiast | Indian | Hindu