Home/Blog/Web Development/Linux Command Line Tutorial: the commands you need to know
Home/Blog/Web Development/Linux Command Line Tutorial: the commands you need to know

Linux Command Line Tutorial: the commands you need to know

Maryam Sulemani
Nov 09, 2020
14 min read
content
What is the Linux Command Line?
Brief History of Linux Command Line
How to open a terminal
Method 1: Activities
Method 2: Keyboard shortcut
How to navigate files
pwd
ls
cd
How to create folders and files
Touch command
Redirection operator
Cat command
Echo command
Keep the learning going.
Most important commands to know
Find command
Locate command
grep command
chmod
ping command
passwd command
free command
gzip
mv
man
How to write your own scripts
What to learn next
Continue reading about productivity
share

Become a Software Engineer in Months, Not Years

From your first line of code, to your first day on the job — Educative has you covered. Join 2M+ developers learning in-demand programming skills.

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.

Master the Bash Shell



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.


How to open a terminal

This section will cover how to open the Linux terminal using two methods.


Method 1: Activities

  1. Click the Activities on the top left of your screen.
widget
  1. Search for terminal, shell, command, or prompt.
widget
  1. Select the terminal icon to launch it. Here you have your terminal. For now, it’s just a dull looking screen.
widget

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.


Method 2: Keyboard shortcut

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.

widget

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.

widget

How to navigate files

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.


pwd

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:~$

ls

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.

# ls
0001.pcap Desktop Downloads index.html install.log.syslog Pictures Templates
anaconda-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.xbel
0001.pcap .config .elinks .gnome2_private .kde .opera .spice-vdagent
anaconda-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.

Options Description
ls -X sort by extension name
ls -t sort by time & date
ls -s list file size
ls -S sort by file size
ls -r list in reverse order
ls -R list recursively directory tree
ls -ls list with long format with file size
ls -lh list long format with readable file size
ls -la list long format including hidden files
ls -l list with long format - show permissions
ls -d list directories - with ’ */’

cd

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 Documents.

To change directories, use:

cd directory_name

Let’s change our directory to Documents using both the relative and absolute paths, respectively.

maryam@maryam-VirtualBox:~$ cd Documents
maryam@maryam-VirtualBox:~/Documents$
maryam@maryam-VirtualBox:~$ cd /home/maryam/Documents
maryam@maryam-VirtualBox:~/Documents$

You can switch back to your previous directory using:

cd-

Your parent directory is the directory immediately above the current one. To navigate to your parent directory, use:

cd ..

To move up two levels use:

cd ../../

To navigate to your home directory, use:

cd ~

How to create folders and files

Creating files and folders is quite easy with Linux. Let’s start with creating a new directory called “Donuts”. You will use the command:

mkdir directory_name

To verify if the directory was created, use ls.

maryam@maryam-VirtualBox:~$ ls
a.out Destination Donuts hello.c Public Source Videos
batstat Directory Downloads Music remotesrcdpc Templates
...

You can also create multiple directories using mkdir, use:

mkdir{directory_name1,directory_name2,directory_name3}

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 Donuts2 in Donuts1 in Donuts, you would use:

mkdir –p Donuts/Donuts1/Donuts2

To view the directory structure, use:

ls –R

Now that we have plenty of directories, let’s create some filed. 
Linux has many methods for creating files, some of them include:

  • The touch command
  • The redirection operator
  • The cat command
  • The echo command

Touch command

To create a new file using the touch command, run touch, followed by the name of the file you want to create.

touch Boston_creme_pie.txt

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.

The asterisk * wild card allows you to match instances of a character. To view all the text files in Donuts, use:

ls *”.txt”

Redirection operator

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:

> filename

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 output.txt.

maryam@maryam-VirtualBox:~/Donuts$ ls > output.txt

To view output.txt, we can print its content to the terminal using:

cat output.txt

Cat command

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 CTRL+D.

Now let’s use cat to append to files. In the example below, we are appending the content from Boston_kreme_pie.txt and glazed_donut.txt to jelly_donut.txt. We then use cat to view the contents of jelly_donut.txt.


Echo command

To create empty files using echo, use:

echo > filename


Keep the learning going.

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.

Master the Bash Shell


Most important commands to know

We covered some basic commands in the previous sections. In this section we will cover some other important commands.


Find command

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 donut.txt.

find . –name ‘*donut.txt’

And this command lists all the file names starting with c.

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

Locate command

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 locate.

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 mlocate database.

sudo updatedb

To look for files using their names, use:

locate filename

Note: Locate is case-sensitive, so CreamPuffs.txt and creamPuffs.txt are not the same.

To ignore case, add –i to your command.

locate –i file name

grep command

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 glazed and Glazed are not the same.

To ignore case, use:

grep string file name –i

To search for something within a directory, use:

grep string . –R

chmod

The 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 d.
  • 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:

  • Read = 4
  • Write = 2
  • Execute = 1
  • No permissions = 0

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 4+2+1=74+2+1=7. Read and execute would be 4+0+1=54+0+1=5, and just read would be 4+0+0=44+0+0=4.


ping command

The ping command is used to check the status of your connection with a server.

ping www.educative.io

You can check if you were able to connect and view the response time. The command keeps running until you stop it with CTRL+C.


passwd command

If you want to change the password of your user account, use the passwd command.

sudo passwd maryam

free command

To view memory usage, use the free command.

free –h

The h makes it human readable otherwise, it shows the data in bytes.


gzip

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.


mv

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

man

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.

man ls
widget

How to write your own scripts

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:

  • csh C shell: the syntax of this shell resembles C
  • sh or Bourne Shell: this shell is available on all Linux systems. The shell is basic and offers few features.
  • ksh or Korn Shell
  • Bash or Bourne Again Shell: this powerful tool is the standard GNU shell. It is flexible and intuitive.

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 comment
echo 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 Hello World!.

Next, we will set the permissions for this file, for example:

maryam@maryam-VirtualBox:~$ chmod 755 hello_world.sh

To run the file, use:

./filename

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: name and age. The script takes the input using read and prints the variable values on the screen.

#!/bin/sh
echo What is your name?
read name
echo Hello, $name! How old are you?
read age
echo You are $age years old!

What to learn next

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:

  • Functions
  • Loops
  • Decision making
  • Flow control
  • Positional parameters
  • Error handling

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.

Happy learning!


Continue reading about productivity