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:
This course is for anyone interested in understanding the BASH shell. You’ll learn all the intricacies of advanced BASH scripting.
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.
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.
If you are using other versions of Linux, the terminal will usually be located at the same place as other application launchers. If you can’t find it, use the next method.
This is the easiest way to access your Linux terminal. Press
CTRL+ALT+T to instantly open the terminal.
Though this is the default shortcut, you have the option of changing it. Go to Settings > Keyboard Shortcuts.
You can view all the keyboard shortcuts here, as shown below.
Click the shortcut next to Launch Terminal, enter your new shortcut in the Set Shortcut window, click Set, and you’re good to go.
For example, I changed the shortcut to
CTRL+T, as you can see below.
Now that you have seen the terminal, let’s try out a few basic commands. Commands are the instructions you give to your computer to do something.
Click the window to ensure your keystrokes go there. The odd text you see on the terminal is called prompt. It is the computer’s way of saying that it’s ready to accept a command.
Type in the command below and hit Enter.
When you launch the terminal, you are in the home directory of your user. This usually looks something like “/home/username”. The
pwd, print working directory command lets you know which directory you are in.
The path it gives is the absolute path. The absolute path starts from the root, which is the base of the Linux file system. This is denoted by the slash
maryam@maryam-VirtualBox:~＄ pwd /home/maryam maryam@maryam-VirtualBox:~＄
Another common command is
ls. This command is used to display all the directories and files in your current directory, type
ls and hit enter.
# ls0001.pcap Desktop Downloads index.html install.log.syslog Pictures Templatesanaconda-ks.cfg Documents fbcmd_update.php install.log Music Public Videos
To view any hidden files, add
–a to the command.
# ls -a. .bashrc Documents .gconfd install.log .nautilus .pulse-cookie.. .cache Downloads .gnome2 install.log.syslog .netstat.swp .recently-used.xbel0001.pcap .config .elinks .gnome2_private .kde .opera .spice-vdagentanaconda-ks.cfg .cshrc .esd_auth .gtk-bookmarks .libreoffice Pictures .tcshrc.bash_history .dbus .fbcmd .gvfs .local .pki Templates.bash_logout Desktop fbcmd_update.php .ICEauthority .mozilla Public Videos.bash_profile .digrc .gconf index.html Music .pulse .wireshark
You can add more options to the command to view specific information.
||sort by extension name|
||sort by time & date|
||list file size|
||sort by file size|
||list in reverse order|
||list recursively directory tree|
||list with long format with file size|
||list long format with readable file size|
||list long format including hidden files|
||list with long format - show permissions|
||list directories - with ’ */’|
Next, we’ll look at the
cd, change directory command. Your current directory is where you are currently working, in our case
/home/maryam. Before we start with changing directories, let’s go over what absolute and relative paths are.
An absolute path starts from the system root and a relative path from your current directory. So the absolute path for Documents will be
/home/Maryam/Documents, whereas the relative path will be
To change directories, use:
Let’s change our directory to Documents using both the relative and absolute paths, respectively.
maryam@maryam-VirtualBox:~$ cd Documentsmaryam@maryam-VirtualBox:~/Documents$
maryam@maryam-VirtualBox:~$ cd /home/maryam/Documentsmaryam@maryam-VirtualBox:~/Documents$
You can switch back to your previous directory using:
Your parent directory is the directory immediately above the current one. To navigate to your parent directory, use:
To move up two levels use:
To navigate to your home directory, use:
maryam@maryam-VirtualBox:~$ lsa.out Destination Donuts hello.c Public Source Videosbatstat Directory Downloads Music remotesrcdpc Templates...
You can also create multiple directories using
Do not add any spaces between the names as doing so will include space in the names of the created directories.
You can create subdirectories within a directory by adding
–p. Let’s say you want to create
Donuts, you would use:
mkdir –p Donuts/Donuts1/Donuts2
To view the directory structure, use:
Now that we have plenty of directories, let’s create some filed. Linux has many methods for creating files, some of them include:
To create a new file using the touch command, run touch, followed by the name of the file you want to create.
To create multiple files at the same time, separate the file names by space.
touch glazed_donut.txt chocolate_frosted_donut.txt
You could use
ls to list all these files by adding a wild card. What’s a wild card? Wild cards allow you to search for files given certain criteria.
* wild card allows you to match instances of a character. To view all the text files in Donuts, use:
The redirection operator
> allows you to overwrite an existing file, and the
>> operator lets you append output to a file. When creating a file using
>, be sure to not overwrite an important existing file.
To create a file, use:
Let’s look at how you can use
> to redirect output to a new file. We will list all the files and directories in Donuts, and have the result redirected to
maryam@maryam-VirtualBox:~/Donuts＄ ls > output.txt
To view output.txt, we can print its content to the terminal using:
We looked at how the cat command can be used to read files. Now let’s see how we can use it to create one. Run the command:
cat > filename
Hit enter, followed by the text you want to add to the file, and then press
Now let’s use cat to append to files. In the example below, we are appending the content from
jelly_donut.txt. We then use cat to view the contents of
To create empty files using echo, use:
echo > filename
Learn how to write your own advanced BASH scripts without scrubbing through videos or documentation. Educative’s text-based courses are easy to skim and feature live coding environments, making learning quick and efficient.
We covered some basic commands in the previous sections. In this section we will cover some other important commands.
You can use the find, locate, and grep commands using the find command.
The find command searches through your disk for the files. It allows you to look for files and directories based on user groups, when the files were modified or accessed, file permissions, dates, and size.
find –name filename
To ignore case, use:
find –iname file name
We talked about wild cards before. Here we use them to look for particular files.
This command lists all the file names ending with
find . –name ‘*donut.txt’
And this command lists all the file names starting with
find . –name ‘c*’
You can look for files based on size using:
find –size filesize –type f
Use the command below to look for files based on when they were last modified:
find -iname "*.txt" –mtime -days
To find a directory, use the command:
find –name filename –type d
Compared to find,
locate works faster, as it reads through the file paths stored in the
mlocate.db database. Before you get started, you need to install
The command for installing locate is:
sudo apt-get install mlocate
The first step is entering your password. Once you enter your password, the installation starts.
After the installation, you need to update the search database.
Locate relies on this database, so updating it regularly ensures efficiency. You need super user privileges to update the
To look for files using their names, use:
Note: Locate is case-sensitive, so
creamPuffs.txtare not the same.
To ignore case, add –i to your command.
locate –i file name
Global Regular Expression Print (grep) is used in Linux to find strings in a particular file. If grep finds those characters, it prints the line. The command is:
grep string file name
You print out the line numbers along with the matching string by adding
–n to the command:
grep string filename –n
Note: Grep is case-sensitive, so
Glazedare not the same.
To ignore case, use:
grep string file name –i
To search for something within a directory, use:
grep string . –R
chmod, change mode command, is used to view the read, write, and execute permissions associated with a file. You have three different user types, a user (the owner), group, and others (everyone else), who can either read, write, or execute the file.
To view the permissions associated with a file, use:
ls filename –l
Let’s see what an output means:
-means it it’s a regular file. If it was a directory, it would have been
rw-rw-r--shows the file permissions.
Let’s look at the file
hello.c. Say we want the user to read, write, and execute the file, the group to just read and execute it, and others to only read it. You will use:
chmod u=rwx,g=rx,o=r filename
You can view the updated permissions using:
ls –l hello.c
You can also use the chmod command with the numeric method. The permissions have the following numeric values:
The permissions that a certain user gets is a sum of these numbers, so if you want to give the permission to read, write, and execute, it would be . Read and execute would be , and just read would be .
The ping command is used to check the status of your connection with a server.
You can check if you were able to connect and view the response time. The command keeps running until you stop it with
If you want to change the password of your user account, use the
sudo passwd maryam
To view memory usage, use the free command.
h makes it human readable otherwise, it shows the data in bytes.
To compress files through the terminal, use the
gzip command. If you want to keep both the original file and the compressed version, add
–k, otherwise, it will get rid of the original file.
To move files from one directory to another, use mv. You can also rename files using this command.
mv current_location new_location
To rename the file, use:
mv old_name new_name
To rename the file as you move it, use:
mv current_location new_location/new_name
To know more about any command, use
man. It takes you to the manual where you can view the description and all the options for a command.
Let’s look at the ls command in more detail.
Now that you are familiar with some of the basics, let’s move onto shell scripts. Shell scripting is an open-source program designed to run on Linux. You write a series of commands, and the shell executes them.
First things first, what is a shell? Shell is an interface that allows you to communicate with an operating system. You access the shell through the terminal, enter your commands, the shell processes them and gives you an output.
Linux has the following shells:
For more on BASH, Read our cheatsheet on the top 25 BASH commands
Let’s get started on our first script. Open the text editor and write the code below.
#!/bin/sh# This is a commentecho Hello World!
Save the file as
hello_world.sh. We will be printing out
Hello World!. The first line is a directive, which means that whatever follows should be interpreted by the Bourne shell. The second line is a comment. The third command prints
Next, we will set the permissions for this file, for example:
maryam@maryam-VirtualBox:~$ chmod 755 hello_world.sh
To run the file, use:
There you have it. Your very first shell script!
Let’s create another one,
name_age.sh that uses variables. Below, we have two variables:
age. The script takes the input using read and prints the variable values on the screen.
#!/bin/shecho What is your name?read nameecho Hello, $name! How old are you?read ageecho You are $age years old!
Congratulations on making it to the end. I hope you now have a solid understating of the Linux command line. But this is only the beginning. There are a lot of other Linux commands out there that you may need. You can explore the following next:
To learn more about the Bash Shell, check out Educative’s course Master the Bash Shell. This course will teach you all you need to know about Bash with a hands-on coding environments and exercises.
Join a community of more than 1.6 million readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.