If you are a beginner in Unix/Linux, you might have heard the term terminal or command prompt. We’ll use these terms interchangeably in this tutorial.
You may have experienced it yourself, a black screen with cursor beeping for you to enter some commands.
In this article I’ll try to convince you why the command line isn’t that scary, and how to start getting comfortable with it by practicing some basic Linux commands.
In this article we’ll talk about the Linux command line, known as terminal or shell. Other command lines of Windows, Mac etc. are almost the same, some syntax or commands may differ.
Table of Contents
- Quick History of the Linux Command-Line
- Starting the Linux Command-Line
- Some Linux Basic Commands
Quick History of the Linux Command-Line
The black interface you see to interact with the operating system, is a text-based interface known as command-line, shell, or terminal, console, prompt etc.
The first-generation computers were not that smart like today, and most of them were mainframe, big giant computers. Moreover, the computing power and network speed was low, so text based was the only option for many users to work faster and simultaneously. The initial shell for Unix was The Thompson shell, written by Ken Thompson. Having its own limitation, replacement came from Stephen Bourne in 1979 known as the Bourne shell. In 1989, Brian Fox created the Bourne Again shell, in short “bash”, as a free software. This is the default shell for most Linux operating systems nowadays.
Don’t get bored. Let’s start to ease into using the command-line.
Starting the Linux Command-Line
If you’re using a computer without a graphical user interface (usually a server), then you’re already using the command line.
If you’re using Linux with a graphical user interface, such as a desktop computer, you should be able to launch it by pressing Ctrl+Alt+t or you can find it in your menu.
Linux has multiple desktop environments, and these menus vary. If you have the possibility to search through your installed apps, you can search terminal, command, prompt or shell and the black icon will appear.
In this example we’re using Red Hat Enterprise with the GNOME desktop environment. As you can see, in GNOME you can click on Activities and you should see a shortcut with an icon resembling a miniature terminal.
If you click on it, a terminal will be opened and it will look like below.
An empty terminal window provides you with three bits of information.
- Current username, [ here xyz is the current user ]
- The hostname, [ here the hostname is localhost]
- Current directory (by default, your home folder, indicated by a
$ marks the end of the prompt. After this sign, you will be prompted to enter commands, and press Enter to execute your commands.
Let me now write any arbitrary text in the shell.
The output shows that the command is not found.
So as you can probably tell, to execute something you have to know the correct text command. Your system won’t be damaged if you write random text. It can be damaged if you run certain commands, however. Like deleting files and folders or overwriting sensitive files.
Some Linux Basic Commands
Now, let’s practice some basic commands.
1. pwd (print current directory)
pwd command stands for print current directory. It will print or show where you are right now in your directory hierarchy of your system.
As we can see, we are inside the /xyz directory which is inside /home.
2. cd (change directory)
cd command stands for change directory. If you are in a directory and want to go to another, you have to use cd.
The syntax for the cd command is:
As I want to change directory to Downloads, the command for that is:
Let me explain what we’ve done here.
First, we’ve checked where we are now using
We are in the xyz directory. Now we want to go to our directory Downloads.
I want to change my current directory to the Downloads directory. For this we have to write the path where we want to go, with cd in front.
You can again check whether you’ve landed in the right place or not. How? We’ve just learned about using pwd. We executed the pwd command again and our current directory is printed.
Dand press Tab. It will show all the options available, if there is only one then it will be auto completed.
Here I am shown three options, all of them start with
D. So now I write Dow and press Tab, my type will auto complete with Downloads, as there is only one directory Downloads that starts with Dow.
D, it will start searching for directories starting with
d. Again, if you write PWD or Pwd or anything other than
pwd, error occurs.
3. echo (output something)
You could say the
echo command is somewhat self-explanatory. Whatever you want to print, this command will echo it in the terminal. For example, I want to print hello world in the terminal. The command is:
echo "hello world"
4. mkdir (make directory)
mkdir stands for make directory. Let’s create a folder.
To create a folder, the command is simple:
First let me check where I am. What command to use? Yes! pwd.
I am in the /xyz directory. Here I will create a folder named my_folder.
So the command will be:
Congratulations! We’ve created our first folder via Linux command line.
5. touch (create empty file)
Like folder creation, we can create files also. For file creation the command is touch. The full command will be:
Let us create a file in our previously created directory my_folder.
As we’ve learned earlier, we should check first what is our current directory using
pwd. Then for changing directory what command to use? Yes!
We will now change our current directory to my_folder. The command is:
Let us again check if we are in the right place using
pwd. Right, our current working directory is /home/xyz/my_folder.
We will now create a file named myfile.txt and the command will be:
6. cat (output file contents)
As of now we’ve created a file, but it is empty. We can view the content of a file using cat command.
cat here does not mean fluffy kitty, it means concatenation.
The command syntax:
Let me view the content of myfile.txt. The command will be:
As the file is empty, nothing is shown on the terminal. Let’s write something in the file using echo and save it in myfile.txt. The command is
echo "something" > <filename>
Now for viewing the content, again use cat.
mancommand. man stands for manual. If you come across any command you don’t know, just type
q to quit from the page and return into the terminal.
7. ls (list files and directories)
We have created files and folders via terminal. If we want to view what file and folders are in our current location, we will use the
ls means list file and directory.
We are now in our user xyz’s home directory. How to check? Yes! “
We can enter the
ls command, and can see the files and folder in home location.
Here we can see our own created directory my_folder. Let’s explore what file and folder it contains.
We will first change our directory to my_folder using
cd. Then enter the command
Here we can see that it contains one file and it is our own created myfile.txt.
In short, I hope I can give you an idea of how the command line works. It’s not as scary as we initially think it is!
On a GUI (Graphical User Interface) based system, you are limited to what options the GUI provides. But the command line is like anan open world. Your options are unlimited here. And you can do much more than what we’ve shown you in our examples.
You can do wonders with using the command line. You can run one command or multiple commands together. You can do repetitive command tasks by simply writing a script containing all the commands and executing the scripts again and again. On some level, you have to SSH into another server or into a production server, where in the data center there is no GUI or monitor.
Now why are most people afraid of it? To simply put it, it’s the rigidness of ours to new things. We often fear to embrace new things – it’s pretty much fear of the unknown. It’s not what we are used to, so naturally we should be a little intimidated by it.
I am not saying that GUI is bad and I am not saying the command line is the best way of doing things all the time. Nowadays, most modern Linux distros are offering interactive GUI for users to ease their operation. But having a clear concept over the terminal will be hugely beneficial for you in the long run.
The best way to do that is to start using the terminal for everything. Then slowly you will get over your fear of the command line and start using it like a swiss army knife.
In this tutorial we covered some of the basics of the command line. We hope it helped you out, even if just a little. If you have any feedback or questions then please feel free to leave a comment and we’ll get back to you as soon as possible.