0 ratings0% found this document useful (0 votes) 43 views7 pagesReviewer Chapter 0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Chapter O:
Linux Operating System
Page + 1 backlink
0.1 Shell Commands
Shell commands are used to instruct the linux system. To enter shell commands, we
need to access the shell. For that, we can use a terminal such as the default one
provided by the system.
When a terminal starts up, the first thing you will see is the prompt which looks like
this: user@host:~$ . This is where we enter our shell commands. The user is the
current user logged into the system. The host is the name of system or the
computer itself. Sometimes this is an ip address of a remote system. This is followed
by a colon then the present working directory, which in our case is ~ (this symbol is
called a tilde). Directory and folder means the same and may be used
interchangeably. The prompt ends with the $ symbol where we can enter our
commands. Sometimes we may see the #F symbol instead which means we are
using the root user.
0.2 Displaying the Current Directory
The present working directory is our current location in the filesystem. ~ means that
we are in the currect user's home directory. For us to show our present working
directory, we use the pwd command. For example:
userthost:~$ pwd
» /home/user
The command pwd will display the present working directory: /home/user . The
linux filesystem begins with the root folder / . In our working directory we are in the
user folder inside the home folder of the root folder, each folder separated by
the / symbol. Ina tree structure, it may look something like this:
/
F-— /home
| fuser
| “— other folders inside home
L— other folders inside root0.3 Changing the Directory / Navigating the Filesystem
Now, to navigate to other folders or locations in your filesystem you use the cd
command which means change directory. This command takes one argument which
is the path we want to navigate to. The path may be relative or absolute. Relative
means our point of reference is the current directory while absolute means our point
of reference is the root folder.
Let's assume the folders we will be using already exists in the filesystem. Here is an
example of a relative path: folder_name . When we pass this argument to the cd
command, we will be navigated to a folder inside our currect directory. An example
of an absolute path is /folder_name . When we pass this argument to the cd
command, we will be navigated to a folder inside the root folder. For example:
user@host:~$ cd folder_name
user@host:~/folder_name$ pwd
» /home/user/folder_name
user¢host:~/folder_name$ cd /folder_name
user@host: /folder_name$ pwd
» /folder_name
Notice that in the second prompt ~/folder_name is now shown after the cd
command. We will see this kind of change whenever we change directory as we can
see it also in the last prompt. For the sake of simplicity, we will be using only the $
symbol as our prompt for our next examples.
There are also two other symbols we may use when using a relative path. These are
the . andthe .. symbols. The . symbol is used to reference the current folder,
so we may write either: cd foldex_name or cd ./folder_name . On the other
hand, the .. symbol is used to reference the parent folder or the folder that
contains the current folder.
pwd
/home/usex
cd.
pwd
/home/user # no change, it's the current folder
c
pwd
/home
©
pwd
>/
Bereunre
ee
We can also use multiple instances of this symbol to navigate further up the
filesystem such as: cd ../../.The / at the end is optional but it can give us a
hint that we are navigating to a folder. Given this new insight, we can use this symbol
to access folders or files from a parent directory. For example:
$ pwd
» /home/user
$ cd ../../folder_name$ pwd
» /folder_name
In terms of absolute paths, we can also use two symbols: / and ~ as shorthands
for the root and the current user's home folder respectively. For example:
$ pwd
> /home/user
$d /
$ pwd
of
$ cd ~
$ pnd
>» /home/user
$ cd /folder_name
$ pwd
» /folder_name
$ cd ~/folder_name
$ pnd
» /home/user/folder_name
0.4 Listing Contents of a Directory
Now that we know how to navigate our filesystem, let's start displaying contents of
our directory. To list down the contents of a folder we use the 1s command. We can
also use the dix command for this, but 1s is the preferred command. For example:
$ pwd
> /home/usex
$1s
> Desktop Documents Downloads
$ dix
» Desktop Documents Downloads
The 1s or dix command will display folders we can navigate to or files we can
open from the current directory. Both commands have options like -a or --all to
not ignore entries that start with . suchas ., .., .cache, .config, etc. and
many other options, but or now, we will stick with the basic commands.
We can also pass an absolute or relative path as an argument to these commands to
list down the contents of that path. For example:
$ pwd
> /home/user
$s /
> bin boot cdrom dev .. # folders inside root
$1s
> user # folders inside the parent folder: home0.5 Creating a Directory
Now that we can see contents of a directory, let's create a folder. To create a folder,
we use the mkdix command, i.e. make directory, and we need to provide a name as
an argument. Multiple folders can also be created by passing all names separated by
a single space. This means that in order to create a folder that has a space in its
name, we must enclose the name in single or double quotation marks. For example:
pwd
/home/user
mkdix demo
cd demo
pwd
/home/usex/demo
1s
# nothing is displayed because the folder is empty.
mkdir folder1 folder?
1s
folder1 folder2
mkdir ‘folder 3°
mkdix “folder 4"
ls
folder1 folder2 ‘folder 3' ‘folder 4°
VEER HHL erenure
We can also use paths in our arguments. For example:
pwd
/home/user/demo
mkdix folder1/folder5
ls
foldert folder2 ‘folder 3' ‘folder 4°
cd folder
pwd
/home/user/demo/foldert
1s
folders
mkdix ../foldex6
cd
pwd
/home/user/demo
1s
folder1 folder2 ‘folder 3' ‘folder 4’ folderé
VOL OHH ey earaere
0.6 Renaming a Folder / Moving a Folder
Renaming and moving folder uses the same command my . This command takes two
arguments, the source path and the target path. Both paths can use either absolute
or relative paths.Let's first look at how we can use the command to rename a folder. Whether it's a
folder or a file, renaming is like moving to the same location but with a different
name. Here is an example:
pwd
/home/user
1s
folder1 folder2 ‘folder 3' ‘folder 4' folder6
my ‘folder 3' folder3
1s
folderl folder? folder3 ‘folder 4' folderé
cd foldert
pwd
/home/user/folder1
my '../folder 4' ../foldera
1s
folder1 folder2 folder3 folder4 folder
YORL eur nnrare
Now, let's look at how we can move our folders around the filesystem. Using the
same command, we just need to specify the complete path of the target. For
example:
$ pwd
/home/user/folder1
$1s
> folders
$ mv folders ../folders
$1s
> folderi folder2 folder3 folder4 folder6
In the above example, we can use the command my folder5 ../ where we only
have to specify the target folder, just make sure to add the trailing slash.
0.7 Copying a Folder
Copying a folder involves duplicating the contents of a directory to another location
while retaining the original directory. Similar to moving or renaming a folder, the cp
command is used for copying folders. This command takes two arguments, the
source path and the destination path. Both paths can be specified using either
absolute or relative paths.
Let's delve into how we can use the cp command to copy a folder:
$ pnd
/home/user/
$ 1s
> folder folder2 folder3 folder4 folder6
$ cp -r folder1 folder7
$1s
> folder1 folder? folder3 folder4 folderé folder7cp -x folder? folder1/subfoldert
ls folder1
subfoldert
cp -r folder1 folder2/
1s folder2
foldert
The -x option is used to copy directories recursively, ensuring that all
subdirectories and their contents are also copied.
Remember to exercise caution when copying folders, especially when overwriting
existing files or directories.
0.8 Removing a Folder
In Linux, removing a folder is done using the xm command, which stands for
"remove." This command removes directories and their contents recursively.
However, it's essential to exercise caution when using the xm command as it
permanently deletes files and folders, and they cannot be recovered easily. Here's
how you can safely remove folders:
$ pwd
» /home/user/
ls
folderl folder2 folder3 folder4 folder6 foldex7
am -x foldex7
1s
folderi folder2 folder3 folder4 folder
xm -r folder2/folder1
ls folder2
# nothing is displayed because the folder is empty.
your oure
If you only want to remove the contents of the folder, just use the wildcard * to
match all contents inside the folder. If you want to remove multiple folders, separate
each folder with spaces.
pwd
/home/user
rm -r foldert/*
1s foldert
# nothing is displayed because the folder is empty.
1s
folder1 folder? folder3 folder4 foldex6
am -r folder foldexé
Is
folder1 folder2 folder3
your ernunre
0.9 Working with FilesIn addition to working with folders, Linux provides various commands for creating,
copying, moving, renaming, and removing files. Understanding these commands is,
essential for managing files efficiently. Let's explore each operation
. To create an empty file, you can use the touch command followed by the file
name. You can also create files simultaneously by specifying their names
separated by spaces.
2. To copy a file, you can use the cp command followed by the source file and
destination. Similar to folders, you can copy multiple files simultaneously by
specifying their names separated by spaces.
3. To move a file, you can use the mv command followed by the source file and
destination. Likewise, you can move multiple files simultaneously by specifying
their names separated by spaces.
4, Renaming a file is similar to moving it to the same location with a different name.
You can use the mv command for this.
5. To remove a file, you can use the xm command followed by the file name.
Similarly, you can remove multiple files simultaneously by specifying their names
separated by spaces.
6. You can also use wildcards like * to copy, move, or remove multiple files that
match a pattern.
pwd
/hone/usex
Is
folder1 folder2 folder3
touch [Link] [Link]
1s
[Link] [Link] folder1 folder2 folder3
cp [Link] [Link] foldert
1s foldert
> [Link] [Link]
ny [Link] folder2
Is
[Link] folderl folder2 folder3
my [Link] [Link]
1s
[Link] folder folder2 folder3
xm foldex2/filet. txt
1s foldex2
# nothing is displayed because the folder is empty
xm folder1/*.txt
1s foldert
# nothing is displayed because the folder is empty
Bee enrave
YOOL ear eer ee
Remember to exercise caution when working with files, especially when using
commands like xm .