Chapter 2. Linux Key Commands

In this chapter, we'll explore essential Linux commands for handling directories and files, using the Ubuntu OS running on AWS Lightsail. By the end of this chapter, you'll be able to confidently use commands like pwd
, cd
, ls
, mkdir
, and many others to navigate and manage files within a Linux environment.
What We Cover in This Chapter
The following topics are covered in this chapter:
Setting Up Linux Environment on AWS
In this section, we'll guide you through setting up your Linux environment on AWS Lightsail. Lightsail provides a simplified VPS solution, allowing you to quickly spin up an Ubuntu server and start using Linux without complex configurations. We'll show you how to set up your AWS account, create an instance, and connect to your Ubuntu instance via SSH.
pwd (Print Working Directory)
The pwd
command prints the absolute path of your current working directory. It’s one of the first commands to master when navigating through the Linux filesystem. The command does not require any arguments—simply type pwd
, and it will show you the path of your current directory.
cd (Change Directory)
The cd
command allows you to change your current working directory. In this section, we’ll demonstrate how to use cd
with both absolute and relative paths, as well as special directory characters like /
, .
, and ..
.
ls (List Contents of Directory)
The ls
command is used to list the contents of a directory. You can use various options with ls
to display different levels of detail, such as hidden files and file permissions. We’ll cover the basic usage of ls
and its options like -a
(to show hidden files) and -l
(to show detailed file information).
mkdir (Make Directory)
The mkdir
command is used to create new directories. You’ll learn how to create a directory with both relative and absolute paths, as well as using the -p
option to create nested directories in one command.
rmdir (Remove Directory)
The rmdir
command is used to remove empty directories. In this section, we’ll show you how to use rmdir
to delete an empty directory and also cover the rm -r
command for removing non-empty directories.
touch (Create File)
The touch
command is used to create empty files or update the timestamp of existing files. In this section, we’ll show you how to create a new file using touch
and also how to update a file’s timestamp.
rm (Remove File)
The rm
command is used to remove files or directories. In this section, we’ll cover how to safely delete files and directories using the -r
option for recursive deletion.
mv (Move File and Directory)
The mv
command is used to move or rename files and directories. You’ll learn how to move files between directories and rename files using mv
.
cp (Copy File and Directory)
The cp
command is used to copy files and directories. We’ll show you how to make copies of files, and how to use the -r
option to copy directories.
cat (Display File Content)
The cat
command is used to display the contents of a file. We’ll also cover how to create files with cat
by redirecting text input to a new file.
sort (Sort File Contents)
The sort
command is used to sort the contents of a file. We’ll show you how to sort files in ascending or descending order, and how to redirect the sorted output to a new file.
grep (Global Regular Expression Print)
The grep
command is used to search for specific patterns or strings within files. We’ll introduce regular expressions (regex) and demonstrate how to use grep
to perform pattern searches across multiple files.
Regular Expression
In this section, we’ll dive deeper into regular expressions (regex), showing you how to use regex meta-characters in conjunction with grep
to search for more complex patterns.
find (Find File and Directory)
The find
command is used to locate files or directories under a specified path. We’ll cover how to use find
with options like -name
, -type
, and -size
to refine your search.
Wildcard
Wildcard characters are useful when you want to match a set of files or directories based on a pattern. We’ll show you how to use wildcards like *
and ?
to represent different characters in file or directory names.
ln (Create Link to File and Directory)
The ln
command is used to create links to files or directories. In this section, we’ll explain the difference between symbolic and hard links, and demonstrate how to create both types using ln
.
Learn offline for better focus!
A book for this course is available on Amazon.
Linux Visual Guide
Step-By-Step Manual for Complete Beginners to Mastering Linux Basics, Vim Editor, Command Management, Remote Server Operation, and More.
Get the Book Now