03 Shell Basics PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

/

Linux system’s starting point is /

It is computer or sysroot directory

Every file or directory resides inside /


1st working directory in CLI
[root@server~]# or [user@server~]$

# - Super user

$ - Regular user (can be sudoer)

~ - Home directory

/root - Home directory of root

/home/user (default) - Home directory of regular user


Shell
It is a program which gives CLI

eg:

sh

ksh

tcsh

bash

Verify: echo $SHELL


Basic shell command
whoami clear

hostname history

pwd exit

tty poweroff

reboot
Absolute path and relative path
Absolute path - path name starts with /

eg: cd /var/log/audit

Relative path - path name never starts with /

eg: ls Documents/dir
Creating file using vi(m)
vi(m) filename -----> command mode -----> i or insert -----> insert mode

Esc

<-----

:w - write

:q - quit if already saved

:q! - quit without saving

:wq - write and quit


Basic shell command
cd

ls

touch

vi(m)

mkdir

file
Command
It is a predefined program

/bin --> /usr/bin

/sbin --> /usr/sbin

Custom commands

~/bin
Syntax
<command/subcommand> <options> <arguments>

command - predefined program

options - changes the behaviour of command

arguments - target of command


How options looks
eg:

-a

or

--apple
To find options
command --help

or

man command
Globbing/Wildcard
. - Current directory * - any characters

.. - Parent directory [ab] - include the character

~ - Home directory [a-j] - range of inclusion

~user - User’s Home directory [!ab] - exclude the character

- - Previous working directory [!a-j] - range of exclusion

? - Single character {1,2,3} - separated characters

?? - Two character {1..10} - range


Operators
; - separates multiple linux commands, executes all command in order

eg: whoami ; date ; ls

&& (AND) - only if first command is success then executes the next command

eg: whoami && date

|| (OR) - only if first command is failed then executes the next command

eg: whoami || date


Variable
It is a container which holds some value

eg:

name=testing

mkdir $name
Basic shell command
cp

mv

rmdir

rm

ln
CLI shortcuts
ctrl+A - start of the command ctrl+D - Logout(CLI) or Exit(GUI)

ctrl+E - end of the command ctrl+R - Reverse search

ctrl+-> - end of current word

ctrl+<- - start of previous word

ctrl+U - clear to the left history shortcut

ctrl+K - clear to the right !150 - re-execute the 150th


command from history
ctrl+L - clear screen
!host - re-execute the last command
starts with host
Basic shell command
cat

head

tail

less

more

wc
Redirection
> - redirects standard output to a file

>> - redirects standard output by appending

2> - redirects standard error to a file

2>> - redirects standard error by appending

&> - redirects standard output and error to same file

&>> - redirects standard output and error to same file by appending


Tee
|tee - gives output in CLI and redirects

|tee -a - gives output in CLI and redirects by appending


Pipe
| - Output of command1 is given as input to command2

eg:

history | head
Grep
It is used to filter keyword

eg:

grep home /etc/passwd

ps aux | grep systemd

You might also like