- Linux Command Line Tutorial: the commands you need to know
- Learn advanced BASH commands and scripts
- What is the Linux Command Line?
- Brief History of Linux Command Line
- Linux Command-Line for Absolute Beginners
- Quick History of the Linux Command-Line
- Starting the Linux Command-Line
- Some Linux Basic Commands
- 1. pwd (print current directory)
- 2. cd (change directory)
- 3. echo (output something)
- 4. mkdir (make directory)
- 5. touch (create empty file)
- 6. cat (output file contents)
- 7. ls (list files and directories)
- Conclusion
Linux Command Line Tutorial: the commands you need to know
The Linux command line, also called a terminal, shell, or console, is a text interface for your computer. With this computer program, you can interpret commands and write your own scripts.
It may look intimidating at first, but once you get used to it, it is easy to use. This article will walk you through some of the basics of the Linux command line, along with some real-world examples. We assume you have little or no prior knowledge.
Each Linux distribution comes with a command line. This article uses Ubuntu 20.04, the commands should work with most distributions.
Here’s what we will be covering today:
Learn advanced BASH commands and scripts
This course is for anyone interested in understanding the BASH shell. You’ll learn all the intricacies of advanced BASH scripting.
What is the Linux Command Line?
The Linux terminal gives you an interface where you can input commands and see the result printed as text. You can use the terminal to accomplish tasks like moving files or navigating a directory, without the use of a GUI. Terminals just transfer information. You need a shell for the operating system to understand this information.
With the Linux command line, you can execute commands manually by typing on the terminal. You can also automate commands using Shell Scripts.
Brief History of Linux Command Line
Let’s briefly go through the Linux command line history. Back in the early days of computing, there was an operating system called Unix, which was designed to run on mainframe computers as a multi-user system. Users connected to it remotely through terminals.
These terminals were very basic, as they could only send keystrokes and display data on the screen.
Since text is light on resources, users could interact quickly and efficiently. Every task the users performed was through this textual interface. To coordinate the execution of different programs, users would connect to single master programs.
The user commands were wrapped in “shell” programs. To make complex tasks easier, users could write shell scripts that automated a long series of shell commands.
Linux is a descendant of Unix. Its core part is designed to behave similarly to a Unix system. The Bourne Again Shell (BASH) was developed by Stephen Bourne and released in 1979 in the Version 7 Unix release. Over time, it has become the default shell for Linux systems.
Linux Command-Line for Absolute Beginners
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 my experience most people are uncomfortable with it. When I was younger and I first came in contact with this black screen, I was terrified. I always thought that hackers use this for hacking, so it can only be operated with experienced users.
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
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 ~ ).
The $ 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)
The 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 pwd .
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.
Tip 1: We can use Tab to autocomplete our typing. Like I want to go to Downloads, I will type D and 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.
Tip 2: Linux commands and hierarchy, all are case sensitive. So if I type d instead of 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:
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.
Congratulations! We’ve created our first folder via Linux command line.
Tip 3: You can just press the up arrow and previously executed commands will be displayed on the terminal.
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:
Note: The touch command is used for more than creating empty files, however this just happens to be a frequent use of it. You can find out more in our article on the touch command.
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! cd .
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.
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
Now for viewing the content, again use cat.
Tip 4: Use the man command. man stands for manual. If you come across any command you don’t know, just type man to get yourself familiar with the command. For example, you may have heard the commands rm, sed, awk, ls, or others. Just go through the manual page of these commands.
Press 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 command. ls means list file and directory.
We are now in our user xyz’s home directory. How to check? Yes! “ pwd .
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 ls .
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.
Conclusion
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.