0% found this document useful (0 votes)
5 views72 pages

Module5 1445

Module 05 covers the basics of using the Linux command line, including the command line interface, shell, commands, variables, and command types. It emphasizes the power and efficiency of the CLI for task automation and provides insights into command execution, options, arguments, and command history. Additionally, it explains the use of variables, globbing, and quoting in the Bash shell.

Uploaded by

joud-siraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views72 pages

Module5 1445

Module 05 covers the basics of using the Linux command line, including the command line interface, shell, commands, variables, and command types. It emphasizes the power and efficiency of the CLI for task automation and provides insights into command execution, options, arguments, and command history. Additionally, it explains the use of variables, globbing, and quoting in the Bash shell.

Uploaded by

joud-siraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 05

Command Line Skills


Exam Objective
2.1 Command Line Basics

Objective Description
Basics of Using the Linux Command Line
Introduction
Introduction
● This module will cover the basics of the command line such as:
○ The command line interface
○ The shell
○ Commands
○ Variables
○ Command Types
○ Quoting
○ Control Statements
Command Line Interface
Command Line Interface
● The Linux community promotes the CLI due to its power, speed and ability to
accomplish a vast array of tasks with a single command line instruction.
‫ نظرا لقوته وسرعته وقدرته على إنجاز مجموعة واسعة من المهام من خالل تعليمات‬CLI ‫على‬Linux ‫يعتمد مجتمع‬
‫سطر أوامر واحدة وخاصة التي ال تحتاج إلى واجهات رسومية مثل السيرفرات‬

● The CLI provides more precise control, greater speed and the ability to automate
tasks more easily through scripting.
‫تحكما أكثر دقة وسرعة أكبر والقدرة على أتمتة المهام بسهولة أكبر من خالل البرمجة النصية‬CLI ‫يوفر‬
‫والتي تكون أخف على الجهاز‬

● By learning the CLI, a user can easily be productive almost instantly on ANY flavor or
distribution of Linux
Linux ‫ يمكن للمستخدم بسهولة أن يكون منتجا ً في توزيع المهام وإنجازها لنظام التشغيل‬،CLI ‫من خالل تعلم‬
The Shell
The Shell
● Once a user has entered a command the terminal then accepts what the user has
typed and passes to a shell.
shell ‫ يتم تمريره إلى‬terminal ‫بمجرد إدخال أمرفي أل‬
● The CLI provides more precise control, greater speed and the ability to automate
tasks more easily through scripting.
‫ دقة وسرعة أكبر وقدرة على أتمتة المهام‬CLI ‫يوفر‬
● The shell is the command line interpreter that translates commands entered by a
user into actions to be performed by the operating system.
‫ هو المترجم الذي يترجم األوامر التي يدخلها المستخدم إلى إجراءات يقوم بها نظام التشغيل‬a shell i

● The Linux environment allows the use of many different shells.


shell ‫باستخدام العديد من‬Linux ‫تسمح بيئة‬
● The most commonly used shell for Linux distributions is called the Bash shell.
history‫ النه يتميز بوجود ال‬Bash shell ‫اسم‬Linux ‫يطلق على القشرة األكثر استخداما لتوزيعات‬
The Shell
● The Bash shell also has many popular features, a few of which are listed below:‫المميزات‬
○ Command line history
○ Inline editing‫التعديل في السطر‬
○ Scripting ‫هو ملف فيه اكواد برمجية يفهمها الباش‬
■ The ability to place commands in a file and then interpret (effectively use Bash to execute the
contents of) the file, resulting in all of the commands being executed.

○ Aliases ‫البدائل لبعض األوامر الطويله‬


■ The ability to create short nicknames for longer commands.

○ Variables ‫المتغيرات‬
■ Used to store information for the Bash shell and for the user.
The Shell
● When a terminal application is run, and a shell appears, displaying an important part
of the interface — the prompt.
● Typically the prompt contains information about the user and the system. Below is a
common prompt structure:

sysadmin@localhost:~$

● The prompt shown contains the following information:


○ Username (sysadmin)
○ System name (localhost)
○ Current Directory (~)

The ~ symbol is used as shorthand for the user's home directory.


sysadmin@localhost:~$

● The prompt shown contains the following information:


○ Username (sysadmin)
○ System name (localhost)
○ Current Directory (~)

The ~ symbol is used as shorthand for the user's home directory.


Commands
Commands
● A command is a software program that when executed on the CLI, performs an action
on the computer. ‫هي الجمل البرمجية التي نكتبها لتنفيذ مجموعه من المهام‬

● To execute a command, the first step is to type the name of the command.

● If you type ls and hit Enter. The result should resemble the example below:

sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
Commands
● Some commands require additional input to run correctly.

● This additional input comes in two forms: options and arguments.


○ Options are used to modify the core behavior of a command.
○ Arguments are used to provide additional information (such as a filename or a username).

● The typical format for a command is as follows:


command [options] [arguments]
Arguments
Commands
command [options] [arguments]

● An argument can be used to specify something for the command to act upon.

● If the ls command is given the name of a directory as an argument, it lists the


contents of that directory:
sysadmin@localhost:~$ ls /etc/ppp
ip-down.d ip-up.d

● Some commands (such as ls) accept multiple arguments:

sysadmin@localhost:~$ ls /etc/ppp /etc/ssh


Options
Options
command [options] [arguments]

● Options can be used with commands to expand or modify the way a command behaves.

● For example, using the -l option of the ls command results in a long listing, providing additional
information about the files that are listed.
sysadmin@localhost:~$ ls -l

total 0

drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Desktop

drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Documents

Output Omitted...

● Often the character is chosen to be mnemonic for its purpose, like choosing the letter l for long
or r for reverse.‫الرموز تسير لغرض االستخدام‬
Options
● Options can be used in conjunction with other options: ‫نقدر نستخدم اكثر من خيار‬

sysadmin@localhost:~$ ls -lr

● Options are often single letters; however, sometimes they are words or phrases as well.
● Typically, older commands use single letters while newer commands use complete words for
options.
○ Single-letter options are preceded by a single dash - character, like the -h option.‫للحرف‬

○ Full-word options are preceded by two dash -- characters like the full-word form of the -h option, the --
human-readable option‫للكلمة‬
Commands
uname
uname [options]
whoami
Command History
● When a command is executed in the terminal, it is stored in a history list.

● This makes it easy to execute the same command later eliminating the need to
retype the entire command.
● Pressing the Up Arrow ↑ key displays the previous command on the prompt line.
● To view the entire history list of a terminal, use the history command:

sysadmin@localhost:~$ history

1 date

2 ls

3 cal 5 2030

4 history
Command History
● If the desired command is in the list that the history command generates, it can be executed
by typing an exclamation point ! character and then the number next to the command (i.e., !3)
!3‫اختيار امر معين للتنفذ مره اخرى‬
● If the history command is passed a number as an argument, it outputs that number of previous
commands from the history list(history 3) ‫ أوامر‬3 ‫لتنفيذ آخر‬
sysadmin@localhost:~$ history 3

6 date

7 ls /home
8 history 3

● To execute the most recent command type !! and hit Enter: ‫العادة نفس االمراالخير‬
● To execute the most recent iteration of a specific command, type !command and hit Enter. ‫العادة‬
‫االمر الذي يتضمن معطيات‬
Command Types
Command Types
● The type command can be used to determine information about command type.

type command

● There are several different sources of commands within the shell of your CLI:
○ Internal commands
○ External commands
○ Aliases
○ Functions
Internal Commands
● Also called built-in commands, these commands are built into the shell itself.
shell ‫هي أوامر مدمجة في نفس ال‬

● A good example is the cd,ls (change directory) command as it is part of


the Bash shell.

● The type command identifies the cd command as an internal command:


‫يستخدم هذا االمر لمعرفة نوع االمر‬
sysadmin@localhost:~$ type cd
cd is a shell builtin
External Commands
● External commands are stored in files that are searched by the shell.
‫أوامر يتم البحث عنها في مجلدات أخرى وليست في نفس الشل‬
● It can be beneficial to know where the shell is finding the command or which version
it is using.

‫أين االمر‬

● The which command searches for the location of a command by searching


the PATH variable.
sysadmin@localhost:~$ which ls
‫أي أمر‬
/bin/ls
Aliases
● An alias can be used to map longer commands to shorter key sequences.

● To determine what aliases are set on the current shell use the alias command:

● To delete the alias command can type unalias commands


Functions
● Functions can also be built using existing commands to:
script‫عبارة عن مهام مدمجة في نفس األمر ومانتعامل معاها بشكل مباشر في الشل لكن يتم التعامل معها في ملف ال‬

○ Create new commands


○ Override commands built-in to the shell or commands stored in files

● Aliases and functions are normally loaded from the initialization files when the shell
first starts.
Variables
‫‪Variables‬‬
‫‪● A variable is a feature that allows the user or the shell to store data.‬‬
‫يتم حفظ البيانات في المتغيرات‬
‫‪● Variables are given names and stored temporarily in memory.‬‬
‫هذه المتغيرات يتم تسميتها بأسماء وتحفظ مؤقتا ً في الذاكرة‬
‫‪● There are two types of variables used in the Bash shell, local and‬‬
‫‪environment or global.‬‬
‫يوجد نوعين من المتغيرات‪:‬‬
‫● ‪local‬تحفظ مؤقتا في شاشة ألـ ‪Terminal‬وبمجرد إغالق الشاشة السوداء يتم فقد قيم جميع المتغيرات‬
‫والبيانات التي تحفظها‬
‫● ‪environment‬تكون محفوظة في الملفات البرمجية التي تكون محفوظة في النظام اليمكن فقدها ويصل لها‬
‫أي شخص على النظام مثل ملفات ألـ ‪script‬‬
Local Variables
● Local or shell, variables exist only in the current shell. When the user closes a terminal window or
shell, all of the variables are lost.

● To set the value of a variable, use the following assignment expression.


variable=value

● The following example creates a local variable named and assigns it


a value of Something:
sysadmin@localhost:~$ variable1='Something'

● To display the value of the variable, use a dollar sign $ character followed by the variable name
as an argument to the echo command:

sysadmin@localhost:~$ echo $variable1


Something
Environment Variables ‫متغيرات النظام‬
● Environment variables, also called global variables, are available system-wide.

● Examples include the PATH, HOME, and HISTSIZE variables.

● The command in the example below displays the value of the HISTSIZE variable:

sysadmin@localhost:~$ echo $HISTSIZE

1000

● The env command outputs a list of the environment variables.


‫يظهر جميع المتغيرات الموجودة في النظام‬
Path Variable
● One of the most important Bash shell variables to understand is
the PATH variable.
● The PATH variable lists all the places that the system can look for programs
to execute.

● The following command displays the path of the current shell:


sysadmin@localhost:~$ echo $PATH
/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/
games
$variable
Glopping
Globbing
● Glob characters are often referred to as wild cards. * ‫؟‬
)‫وتمثل ؟ حرف واحد أما * تمثل من صفر الى مجموعة حروف او ارقام (تستخدم للبحث عن الملفات‬
● These are symbol characters that have special meaning to the shell.
‫القلوبنق هي حروف يعاملها الشل معاملة خاصه‬
● Globs are powerful because they allow you to specify patterns that match
filenames in a directory: ‫???نمط اسم الملف ونوع الملف‬.*
○ Instead of manipulating a single file at a time, you can easily execute commands
that will affect many files.
Glopping

( [ ] , ‫ ؟‬, * ( ‫هي‬glopping ‫أحرف ألـ‬


echo
Globbing - The Asterisk *
● The asterisk character is used to represent zero or more of any character in
a filename.

● For example, suppose you want to display all of the files in the
/etc directory that begin with the letter t:
sysadmin@localhost:~$ echo /etc/t*
/etc/terminfo /etc/timezone

● The pattern t* matches any file in the /etc directory that begins with the
character t followed by zero or more of any character.
Asterisk *
Asterisk *
Asterisk *
Globbing - The Question Mark ?
● The question mark character matches exactly one character, no more and no less.

● Suppose you want to display all of the files in the /etc directory that begin with the
letter t and have exactly 7 characters after the t character:
sysadmin@localhost:~$ echo /etc/t???????
/etc/terminfo /etc/timezone

● The asterisk and question mark could also be used together to look for files with
three-letter extensions by running the echo /etc/*.??? command:

sysadmin@localhost:~$ echo /etc/*.???


/etc/[Link] /etc/[Link]
Question mark ?
Question mark ?
Question mark ?
Question mark ?
Square brackets [ ]
Globbing - Brackets [ ]
● Brackets are used to match a single character by representing a range of characters
that are possible match characters.

● For example, echo /etc/[gu]* will print any file that begins with either
a g or u character and contains zero or more additional characters:
sysadmin@localhost:~$ echo /etc/[gu]*
/etc/[Link] /etc/groff /etc/group /etc/group- /etc/gshadow /etc/gshadow-
/etc/[Link] /etc/udev /etc/ufw /etc/update-motd.d /etc/[Link]

● Brackets can also be used to a represent a range of characters by using the -


character (i.e., any letter between and including a and d):

sysadmin@localhost:~$ echo /etc/[a-d]*


Globbing - Exclamation Point !
● The exclamation point is used in conjunction with the square brackets to
negate a range.

● For example, the command echo /etc/[!a-t]* will display any file
that does not begin with an a thru t:
sysadmin@localhost:~$ echo /etc/[!a-t]*
/etc/[Link] /etc/udev /etc/ufw /etc/update-motd.d /etc/[Link] /etc/vim
/etc/wgetrc /etc/xml
Square brackets [ ]
Square brackets [ ]
Quoting
Quoting
Quoting includes:

• Double Quotes " "

• Single Quotes ‘ ‘

• Backquotes ``
Double Quotes ‫االقتباس‬
● Double quotes stop the shell from interpreting some metacharacters, including glob
characters.
Glob characters, also called wild cards, are symbols that have special meaning to the shell (i.e, *, ?).

● This is useful when you want to display something on the screen that is normally a
special character to the shell.
● In the example below, the Bash shell doesn't convert the glob pattern into filenames
that match the pattern (like it normally does):

sysadmin@localhost:~$ echo "The glob characters are *, ? and [ ]"

The glob characters are *, ? and [ ]

● Double quotes still allow for command substitution, variable substitution, and permit
some other shell metacharacters (i.e., the PATH variable)
Double Quotes " "
Single Quotes
● Single quotes prevent the shell from doing any interpreting of special characters,
including globs, variables, command substitution and other metacharacters.

sysadmin@localhost:~$ echo The car costs $100

The car costs 00

sysadmin@localhost:~$ echo 'The car costs $100'


The car costs $100
Single Quotes ‘ ‘
Backslash Character

● A technique to essentially single quote a single character is to use the backslash character \.
● If the phrase below is placed in single quotes, $1and $PATH are not variables:
sysadmin@localhost:~$ echo "The service costs $1 and the path is $PATH"

The service costs and the path is


/usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
:/usr/games

● What if you want to have $PATH treated as a variable and $1 not?


● In this case, use a backslash \ character in front of the dollar sign $ character to prevent the
shell from interpreting it:

sysadmin@localhost:~$ echo The service costs \$1 and the path is $PATH

The service costs $1 and the path is


/usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bi
n:/usr/games
Backslash Character \
Backquotes
● Backquotes, or backticks, are used to specify a command within a command, a
process called command substitution.

● Note the output of the echo Today is date command line:


sysadmin@localhost:~$ echo Today is date

Today is date

● To execute the date command so the output of that command is sent to


the echo command, put the date command inside of two backquotes:

sysadmin@localhost:~$ echo Today is `date`

Today is Mon Nov 4 03:40:04 UTC 2030


Backquotes ` `

sysadmin@localhost:~$ echo Today is `date`

Today is Mon Nov 4 03:40:04 UTC 2030


Control Statements
Control Statements
● Control statements allow you to use multiple commands at once or run additional
commands.

● Control statements include:


○ Semicolon (;)
○ Double ampersand (&&)
○ Double pipe (||)
Control Statements
● The semicolon can be used to run multiple commands, one after the other:
sysadmin@localhost:~$ cal 1 2015; cal 2 2015; cal 3 2015

● The double ampersand && acts as a logical "and" if the first command is successful, then the
second command (to the right of the &&) will also run:

sysadmin@localhost:~$ ls /etc/xml && echo success

catalog [Link] [Link] [Link]

success

● The double pipe || is a logical "or". It works similarly to &&; depending on the result of the first
command, the second command will either run or be skipped:

sysadmin@localhost:~$ ls /etc/junk || echo failed

ls: cannot access /etc/junk: No such file or directory

failed
Semicolon ;
Double ampersand &&
Double ampersand &&
Double pipe ||

You might also like