Introduction to Linux: Part 2 — Time To Draw Swords
By: Sarthak Pranesh
Tags: Linux, Bash, Package, Ubuntu, Ubuntu Server

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