Terminal Usage
As a backend engineer, working with the terminal is a common practice and a skill you should learn. Some many commands and utilities can help you achieve your tasks more efficiently when using Linux.
The best way to learn these commands is to practice them. On August 28, 2017, I switched to Linux without prior knowledge and learned a long way by practicing, failing, and trying again.
You can start by reading through simple documentation about each command using the man
command, e.g. man grep
, man awk
, etc. This command is like quick help and will get you started while you practice and improve over time.
Basic Linux Commands
Learning the common utilities or commands will go a long way if you want to use the Linux operating system. This lesson will review some basic to advanced Linux commands for backend engineers to help you get the most out of your Linux distro.
Before starting the lesson, you must install Linux and your terminal. You can just read through how to install Linux and learn how to use the Terminal.
Sudo Command
This command is short for “superuser do”. sudo is one of the most used commands that lets you perform tasks requiring administrative or root privileges and permissions.
The system will prompt the user to re-authenticate with a password when using the sudo command. Next, the Linux system will log a timestamp to track how long a user is supposed to use the command before authenticating again. Every root user can run the sudo command for 15 minutes/session by default.
Using the sudo command without authenticating yourself, the system will log the activity as a security event and display errors.
Here is the syntax for the sudo command:
sudo (command)
You can also add options, such as:
-k or —reset-timestamp: This option invalidates the timestamp file.
-g or —group=group: This option runs commands as a specified group name or ID.
-h or host=host: This option runs commands on the host.
cd command
The cd
command is one of the most used commands in Linux. To navigate through the Linux files and directories, use the cd
command. Sometimes. Depending on your current working directory, it requires either the full path or the directory name.
Let’s say you’re in /home/username/Downloads
and want to go to Videos
, a sub-directory of Downloads
. To do so, enter the following command:
cd Videos
Also, if you want to switch folders completely, you can enter the directory's full path as shown below: Let’s assume you want to visit the Documents
folder.
cd /home/username/Documents
# or you can do this as a shortcut.
cd ~/Documents
Here are some shortcuts to help you navigate faster:
cd ...
: It helps you move one directory up.cd ~[username]
: It helps you go to another user’s home directory.cd -
: It helps you move to your previous directory.
ls command
The ls
command lists files and directories within a system or a directory. If you run this command without a flag, parameter, or option, it will show the current working directory’s content.
To see the content of any directory, navigate the directory and type the ls
command or type the ls
command followed by your desired path as shown below: To view the content of the Videos
directory:
// Navigate
cd /home/username/Downloads/Videos
// Type the command
ls
OR
// Type the command followed by the path
ls /home/username/Downloads/Videos
Here are some options you can use with the ls
command:
ls -a
: It shows all files including all the hidden filesls -R
: It shows all the files including files in sub-directoriesls -lh
: It shows the file sizes in easily readable formats such as MB, GB, etc.
cat command
The cat
command is one of the most used commands in Linux. It lists, combines, and writes file content to the standard output. Type the cat
command followed by the file name and its extension to run the command. For example,
cat filename.txt
Here are other ways you can use the cat
command:
Typing
cat > filename.txt
creates a new file.Typing
cat filename1.txt filename2.txt > filename3.txt
merges both files tofilename3.txt
.Typing
tac filename.txt
displays the content in reverse order.
Files and Directory manipulation commands
Here we will discuss commands such as:
mkdir
cp
rmdir
mv
mkdir command
The mkdir
command is used to create one or multiple directories at once and set permissions for each of them. If you’re executing this command, you must have permission to create a new folder in the directory or you may receive a permission denied error.
Here is a basic syntax:
mkdir [option] directory_name
Here are some examples:
mkdir videos
To create a directory called videos
mkdir Documents/PDFs
To create a new directory called PDFs
inside Documents
directory.
The mkdir
command accepts some options such as:
-p or —parents which creates a directory between two existing folders. For instance,
mkdir -p Documents/PDFs/Books
will make a newPDFs
directory.The
-m
option sets the file permissions. For instance, running the commandmkdir -m777 Documents/Books
will set the permission to full read, write and execute to all users.The
-v
option prints a message for each created directory.
cp command
The cp
command is use to copy files or directories and it’s content from one location to the other.
To copy a file from the current directory to the other,enter the cp
command followed by the file name and the destination directory.
For instance:
cp filename.txt ~/Documents
You can copy multiple files to the same location as shown below:
cp file1.txt file2.txt ~/Documents
To copy the content of a file to another file in the same directory. Enter the following command:
cp file1.txt file2.txt
To copy an entire directory, use the -R
option before typing the source directory followed by the destination as shown below:
cp -R ~/Documents ~/Documents/All
mv command
The mv
command is used to move files or directories from source to destination or rename files and directories.
To use the mv
command, simply type mv
followed by the filename and destination directory as shown below:
mv file.txt ~/Documents
You can also rename files using the mv
command as shown below:
mv old_file.txt new_file.txt
Lastly, you can also move a full directory including all the files and sub-directory to a new destination using the mv
command with the -rf
flag as shown below:
mv -rf ~/Documents/drectory1 ~/Documents/new_derectory
The -f
uses force to achieve it and you should careful when using f
and lastly the -r
is recursive meaning it will visit every sub-directories recursively.
rmdir command
The rmdir
command is use to delete an empty directory permanently. You should run this command using the sudo privileges.
For instance, you want to remove an empty subdirectory named empty_folder
, you can use the rmdir
command as shown below:
rmdir empty_folder
rm command
The rm
command is used to delete files within a directory. However, the before you execute this command you must use the sudo
command for permission. Also note the files or directories deleted can’t be undone.
Here’s the general syntax:
rm filename.txt
To delete multiple files, enter the following command:
rm filename1.txt filename2.txt filename3.txt
Here are some acceptable options you can add:
Adding the
-i
option prompts system confirmation before deleting a file.Adding the
-f
allows the system to remove without a confirmation.Adding
-r
deletes files and directories recursively.
touch command
The touch command allows you to create an empty file or generate and modify a timestamp in the Linux command line.
To create a new file use the following command:
touch ~/Documents/index.html
nano, vi, jed command
With these commands such as nano, vi, jed, you can edit and manage files in Linux via a text editor. nano
and vi
comes pre-installed with Linux while jed
has to be installed.
The nano
command can work with most languages. To use it, enter the following command:
nano [filename]
vi
uses two operating modes to work — insert mode and command mode. The insert
mode is used to edit and create a text file while the command
mode performs operations such as saving, opening, copying, and pasting a file.
To use vi
on a file, enter:
vi [filename.txt]
jed
has an interface that allows users to perform actions without entering keyboard combinations or commands. It also has modes to load modules or plugins to write specific texts like vi
. To open jed
program, simply type the jed
command on your terminal.
grep command
The greb
command means global regular expression print. It allows you to search through all the texts in a specific file. Once it finds the match, it prints all lines that contain the specific pattern. This command helps filter through large log files.
For example, you want to search for the word red in the filename.txt file:
grep red filename.txt
The command’s output will display lines that contain red.
Permission Commands
Here we will discuss commands such as:
chmod
chown
chmod command
The chmod
command is used to modify the read, write, and execute permission of a file or directory. In Linux, each file is associated with three user classes – owner, group member, and others.
For instance, here’s a general syntax:
chmod [option] [permission] [file_name]
While the owner of a file is the only one with full permissions for any file, you can give different permissions to different users or group using the chmod
command.
For example, to allow group members and others to read, write, and execute the file, change it to the -rwxrwxrwx permission type, whose numeric value is 777 using the command below:
chmod 777 filename.txt
This command supports many options, including:
Use
c
or–changes
to display information when a change is made.Use
f
or–silent
to suppress the error messages.Use
v
or–verbose
to display a diagnostic for each processed file.
chown command
The chown
command means change ownership. It allows you to change the ownership of a file, symbolic link to a specified username, or directory.
Here’s a general syntax:
chown [option] owner[:group] file(s)
For example, you want to make user2 the owner of filename2.txt:
**chown user2 filename2.txt**
Downloading Commands
Here we will discuss commands such as:
wget
apt-get
wget command
The wget
command allows you to download files from the internet, it works in the background without hindering other running processes. The wget command retrieves files using HTTP, HTTPS, and FTP protocols and can perform recursive downloads.
Enter the following command to use:
wget [option] [url]
For example, to download a file from Mastering Backend. Assuming this is latest.zip
file to download. Do this:
wget <https://masteringbackend.com/latest.zip>
apt-get command
The apt-get
command is a tool for handling Advanced Package Tool (APT) libraries in Linux. It lets you retrieve information and bundles from authenticated sources to manage, update, remove, and install software and its dependencies.
It requires the use of sudo
command when running the apt-get
command. Here’s the main syntax:
apt-get [options] (command)
These are the most common commands you can add to apt-get:
Use
update
to synchronize the package files from their sources.Use
upgrade
to install the latest version of all installed packages.Use
check
to update the package cache and check broken dependencies.
I need you to practice all the above commands and reach out to more commands to solidify your knowledge of the terminal. In the next lesson, we will explore important concepts to learn in operating systems as a backend engineer.