KEMBAR78
Linux Shell Basics | PPTX
GNU/Linux and Bash basics 
Constantine Nosovsky 
1
Table of contents 
History. What Linux and Bash are and what they are not 
File system (structure, files/dirs, links, file types, locations) 
Users, groups, permissions 
Processes (organization, types, states, signals) 
Bash functionality 
GNU utilities and tools 
2 of 24
Bash and Utilities 
User session, bash configuration 
Bash CLI vs. scripts 
Command execution, POSIX arguments 
Variables (usage, environment) 
File streams, pipelines, command substitution 
Text processing 
Program execution, processes 
File system management 
Permissions 
Network 
3 of 24
History 
4 of 24 
1969 – Unix: multi-tasking, multi-user OS in assembly language 
1973 – Unix was rewritten to C 
1983 – GNU: GNU’s Not Unix. GPL. GCC. GRUB. Bash 
1987 – MINIX: educational OS 
1991 – Linux kernel. “I'm doing a (free) operating system (just a 
hobby, won't be big and professional like gnu)” 
1993 – Slackware Linux: the first official distribution
Linux history today 
5 of 24 
15M lines of code (excluding comments, blanks) // ohloh.net 
Around 5k commits per month // ohloh.net 
Some contributor companies: Red Hat, Intel, IBM, Nokia, 
Samsung, Oracle, Google, AMD, Microsoft // linuxfoundation.org 
75% of all kernel development is done by developers who are 
being paid for their work // linuxfoundation.org 
Hundreds of distributions, variety of platforms 
469 of 500 TOP computers use Linux // top500.org
File system (structure, files/dirs) 
Root directory “/” is required to be mounted 
Every directory contains link to itself “.” and parent directory “..” 
“~” is a user’s home directory 
Refer to the file by its 
Absolute path – starting from the root directory: /bin/bash 
Relative path: ~/.bashrc, ./hello.sh, ../hello.sh 
File is a sequence of bytes. 
Directories contain file metadata: length, timestamps, type, 
owner, permissions, etc. 
6 of 24
File system (links) 
Symbolic (soft) link (ln -s) 
Dir1 
File1 metadata 
File2 metadata 
… 
File1 content on disk 
Hard link (ln) 
Dir2 
File3 metadata 
File4 metadata 
… 
File3 content on disk 
File1 content on disk 
Dir1 
File1 metadata 
File2 metadata 
… 
Dir2 
File1 metadata 
File3 metadata 
… 
7 of 24
File system (file types, pseudo fs) 
Regular file (text, binary), directory, symbolic link, socket, pipe 
/dev – Devices related files 
Block, character devices (storage, sound, input, net, tty) 
Pseudo files (urandom, null) 
/proc – Processes related files 
/proc/<PID>/ - information related to the process with PID 
cpuinfo, meminfo, version, modules, uptime, etc 
8 of 24
File system (some locations) 
9 of 24 
/boot – loader, kernel images 
/bin – base programs (shell, system-utils), /sbin – for super user 
/etc – system configuration, service management scripts 
/home – home directories of regular users 
/lib, /lib64 – basic system shared libraries 
/tmp – temporary file system (cleaned after reboot) 
/media, /mnt – mounted storage devices 
/lost+found – used for recovery 
/var – often changed files: logs, cache, locks 
/usr – user space software related: executables, libs, docs, src, 
headers, resources
Users, groups, permissions 
10 of 24 
/etc/passwd,group,shadow – user, group, passwords storage 
File permissions: read, write, execute 
Dir perm: read(metadata), write(metadata), execute(change dir) 
Each file has user and group owners 
Rules applied in order user-group-others (e.g. rwx r-x r--) 
root (UID=0) has all permissions 
SUID/GUID – bits to run as user/group owner 
Sticky bit is used to grand permissions exclusively to the owner 
Default permissions are set up with umask or mounting fs
Processes (organization, types) 
Organization 
Each process has PID and parent PID (PPID) 
Each process belongs to a process group 
Groups are associated with a terminal session 
Types 
11 of 24 
System processes are run from kernel in a special way (e.g. init) 
Daemon – process detached from the session attached 
Regular user space processes
Processes (states, signals) 
Process states (ps) 
R - Running or runnable (on run queue) 
D - Uninterruptible sleep (usually IO) 
S - Interruptible sleep (waiting for an event to complete) 
T - Stopped, either by a job control signal or because it is being 
traced 
Z - Zombie process, terminated but not reaped by its parent 
Signals (kill) 
KILL, TERM, STOP, CONT, CHLD 
12 of 24
User session, bash configuration 
Terminal session is created 
/bin/login is run for authentication 
User data are read from /etc/passwd, group (home dir, shell) 
Specified shell is run for user (parent process of the session) 
Shell is set up with global settings /etc/bashrc, /etc/profile 
Shell is set up with user configuration ~/.bashrc, ~/.bash_profile 
Do your stuff… 
Exiting from shell will end up terminal session 
13 of 24
Bash CLI vs. scripts 
Execute script 
#! /bin/bash 
command1 
command2 
... 
$ ./script.sh 
Run commands 
$ command1 
$ command2 
$ … 
Interpret script 
command1 
command2 
... 
$ bash ./script.sh 
14 of 24
POSIX command line arguments 
15 of 24 
An option is a hyphen followed by a single character, e.g. -o 
An option may require an argument, e.g. 
-o argument or -oargument 
Options that do not require arguments can be grouped, e.g. 
-lst is equivalent to -t -l -s 
Options can appear in any order, e.g. -lst is equivalent to -tls 
Options can appear multiple times. 
Options precede other nonoption arguments: -lst nonoption 
The -- argument terminates options 
grep -RE '^s*<([a-z]+)>.*$' /usr -i --line-number
Variables (usage, environment) 
Usage 
VAR=value – declaration (script scope) 
$VAR – access value 
export VAR – accessible outside the script (session scope) 
Command line arguments may be accessed like $0, $1,… 
Environment variables 
SHELL, USER, HOME, PATH, PWD 
Use env to list all variables and their values 
16 of 24
File streams, pipelines, command 
substitution 
File streams >, >>, < 
echo ‘hello, world!’ > file 
grep hello < file 
find –name file 2>/dev/null 1>find_result 
find –name file 2>&1 
Pipelines stream output of one command to the input of another 
ps aux | grep ^gptest | tr -s ' ' | cut -f2 -d ' ' | xargs 
Command substitution 
echo Current time is `date` 
17 of 24
Text processing 
Use less to view/navigate the large text files 
Count symbols, words, lines with wc 
Print file(s) with cat file1 file2 file3 
Use cut to filter columns out, e.g. cut –f5 –d ‘:’ 
Use grep to match text by pattern 
Use simple text editor nano for interactive file editing 
Use tr and sed for stream editing, e.g. 
sed s/regexp/replacement/ file > file_modified 
Use head/tail for specific line range, e.g. 
head –n 10000 dev.log | tail –n 100 
18 of 24
Program execution, process 
management 
19 of 24 
Use & to run program in background 
Use Ctrl+Z to pause the execution in foreground 
Use bg/fg to continue execution 
Use Ctrl+C to kill program in foreground 
Use disown to detach background program from current 
session. Or run programs in a way nohup <executable> & 
List processes with ps, top, htop 
Send signal to process with kill <signal> <pid> 
Run commands as different user using sudo
File system management 
20 of 24 
Mount volumes to file system like mount /dev/sda1 /mnt/d, 
mount -o loop file.iso /mnt/cdrom 
Get volume/file(dir) size with df –h / du -sh dir 
Create new directories with mkdir 
Change current directory using cd 
Print current directory using pwd 
List directories and access file metadata with ls 
Use find to locate files by name, type, size, timestamps, etc 
Copy files with cp , move/rename with mv, remove with rm 
Create links with ln and soft links with ln -s
Permissions management 
21 of 24 
Set file permissions with chmod, e.g. chmod o+rw dir -R 
Change file user/group owner with chown/chgrp 
Set default permission mask with umask 
Get your identity with id 
Use ls –l to read file permissions, e.g. ls -l file 
-rw-r--r-- 1 root root 30608 Nov 26 2010 file 
Use su, sudo to run as user 
Know your limits: mounting options (/etc/fstab), file types
Network 
Check server availability with ping, traceroute 
Get your network configuration with ifconfig, netstat 
Use download utilities wget, scp 
You do even have a text browser links 
22 of 24
Learn stuff 
23 of 24 
Use man <cmd> and <cmd> --help to read documentation 
Some commands are just obvious: sort, uniq, date 
Google command line solutions for common problems 
“How to capture screen in bash” 
ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 f.mpg
Thanks for you attention 
Questions? 
24 of 24

Linux Shell Basics

  • 1.
    GNU/Linux and Bashbasics Constantine Nosovsky 1
  • 2.
    Table of contents History. What Linux and Bash are and what they are not File system (structure, files/dirs, links, file types, locations) Users, groups, permissions Processes (organization, types, states, signals) Bash functionality GNU utilities and tools 2 of 24
  • 3.
    Bash and Utilities User session, bash configuration Bash CLI vs. scripts Command execution, POSIX arguments Variables (usage, environment) File streams, pipelines, command substitution Text processing Program execution, processes File system management Permissions Network 3 of 24
  • 4.
    History 4 of24 1969 – Unix: multi-tasking, multi-user OS in assembly language 1973 – Unix was rewritten to C 1983 – GNU: GNU’s Not Unix. GPL. GCC. GRUB. Bash 1987 – MINIX: educational OS 1991 – Linux kernel. “I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu)” 1993 – Slackware Linux: the first official distribution
  • 5.
    Linux history today 5 of 24 15M lines of code (excluding comments, blanks) // ohloh.net Around 5k commits per month // ohloh.net Some contributor companies: Red Hat, Intel, IBM, Nokia, Samsung, Oracle, Google, AMD, Microsoft // linuxfoundation.org 75% of all kernel development is done by developers who are being paid for their work // linuxfoundation.org Hundreds of distributions, variety of platforms 469 of 500 TOP computers use Linux // top500.org
  • 6.
    File system (structure,files/dirs) Root directory “/” is required to be mounted Every directory contains link to itself “.” and parent directory “..” “~” is a user’s home directory Refer to the file by its Absolute path – starting from the root directory: /bin/bash Relative path: ~/.bashrc, ./hello.sh, ../hello.sh File is a sequence of bytes. Directories contain file metadata: length, timestamps, type, owner, permissions, etc. 6 of 24
  • 7.
    File system (links) Symbolic (soft) link (ln -s) Dir1 File1 metadata File2 metadata … File1 content on disk Hard link (ln) Dir2 File3 metadata File4 metadata … File3 content on disk File1 content on disk Dir1 File1 metadata File2 metadata … Dir2 File1 metadata File3 metadata … 7 of 24
  • 8.
    File system (filetypes, pseudo fs) Regular file (text, binary), directory, symbolic link, socket, pipe /dev – Devices related files Block, character devices (storage, sound, input, net, tty) Pseudo files (urandom, null) /proc – Processes related files /proc/<PID>/ - information related to the process with PID cpuinfo, meminfo, version, modules, uptime, etc 8 of 24
  • 9.
    File system (somelocations) 9 of 24 /boot – loader, kernel images /bin – base programs (shell, system-utils), /sbin – for super user /etc – system configuration, service management scripts /home – home directories of regular users /lib, /lib64 – basic system shared libraries /tmp – temporary file system (cleaned after reboot) /media, /mnt – mounted storage devices /lost+found – used for recovery /var – often changed files: logs, cache, locks /usr – user space software related: executables, libs, docs, src, headers, resources
  • 10.
    Users, groups, permissions 10 of 24 /etc/passwd,group,shadow – user, group, passwords storage File permissions: read, write, execute Dir perm: read(metadata), write(metadata), execute(change dir) Each file has user and group owners Rules applied in order user-group-others (e.g. rwx r-x r--) root (UID=0) has all permissions SUID/GUID – bits to run as user/group owner Sticky bit is used to grand permissions exclusively to the owner Default permissions are set up with umask or mounting fs
  • 11.
    Processes (organization, types) Organization Each process has PID and parent PID (PPID) Each process belongs to a process group Groups are associated with a terminal session Types 11 of 24 System processes are run from kernel in a special way (e.g. init) Daemon – process detached from the session attached Regular user space processes
  • 12.
    Processes (states, signals) Process states (ps) R - Running or runnable (on run queue) D - Uninterruptible sleep (usually IO) S - Interruptible sleep (waiting for an event to complete) T - Stopped, either by a job control signal or because it is being traced Z - Zombie process, terminated but not reaped by its parent Signals (kill) KILL, TERM, STOP, CONT, CHLD 12 of 24
  • 13.
    User session, bashconfiguration Terminal session is created /bin/login is run for authentication User data are read from /etc/passwd, group (home dir, shell) Specified shell is run for user (parent process of the session) Shell is set up with global settings /etc/bashrc, /etc/profile Shell is set up with user configuration ~/.bashrc, ~/.bash_profile Do your stuff… Exiting from shell will end up terminal session 13 of 24
  • 14.
    Bash CLI vs.scripts Execute script #! /bin/bash command1 command2 ... $ ./script.sh Run commands $ command1 $ command2 $ … Interpret script command1 command2 ... $ bash ./script.sh 14 of 24
  • 15.
    POSIX command linearguments 15 of 24 An option is a hyphen followed by a single character, e.g. -o An option may require an argument, e.g. -o argument or -oargument Options that do not require arguments can be grouped, e.g. -lst is equivalent to -t -l -s Options can appear in any order, e.g. -lst is equivalent to -tls Options can appear multiple times. Options precede other nonoption arguments: -lst nonoption The -- argument terminates options grep -RE '^s*<([a-z]+)>.*$' /usr -i --line-number
  • 16.
    Variables (usage, environment) Usage VAR=value – declaration (script scope) $VAR – access value export VAR – accessible outside the script (session scope) Command line arguments may be accessed like $0, $1,… Environment variables SHELL, USER, HOME, PATH, PWD Use env to list all variables and their values 16 of 24
  • 17.
    File streams, pipelines,command substitution File streams >, >>, < echo ‘hello, world!’ > file grep hello < file find –name file 2>/dev/null 1>find_result find –name file 2>&1 Pipelines stream output of one command to the input of another ps aux | grep ^gptest | tr -s ' ' | cut -f2 -d ' ' | xargs Command substitution echo Current time is `date` 17 of 24
  • 18.
    Text processing Useless to view/navigate the large text files Count symbols, words, lines with wc Print file(s) with cat file1 file2 file3 Use cut to filter columns out, e.g. cut –f5 –d ‘:’ Use grep to match text by pattern Use simple text editor nano for interactive file editing Use tr and sed for stream editing, e.g. sed s/regexp/replacement/ file > file_modified Use head/tail for specific line range, e.g. head –n 10000 dev.log | tail –n 100 18 of 24
  • 19.
    Program execution, process management 19 of 24 Use & to run program in background Use Ctrl+Z to pause the execution in foreground Use bg/fg to continue execution Use Ctrl+C to kill program in foreground Use disown to detach background program from current session. Or run programs in a way nohup <executable> & List processes with ps, top, htop Send signal to process with kill <signal> <pid> Run commands as different user using sudo
  • 20.
    File system management 20 of 24 Mount volumes to file system like mount /dev/sda1 /mnt/d, mount -o loop file.iso /mnt/cdrom Get volume/file(dir) size with df –h / du -sh dir Create new directories with mkdir Change current directory using cd Print current directory using pwd List directories and access file metadata with ls Use find to locate files by name, type, size, timestamps, etc Copy files with cp , move/rename with mv, remove with rm Create links with ln and soft links with ln -s
  • 21.
    Permissions management 21of 24 Set file permissions with chmod, e.g. chmod o+rw dir -R Change file user/group owner with chown/chgrp Set default permission mask with umask Get your identity with id Use ls –l to read file permissions, e.g. ls -l file -rw-r--r-- 1 root root 30608 Nov 26 2010 file Use su, sudo to run as user Know your limits: mounting options (/etc/fstab), file types
  • 22.
    Network Check serveravailability with ping, traceroute Get your network configuration with ifconfig, netstat Use download utilities wget, scp You do even have a text browser links 22 of 24
  • 23.
    Learn stuff 23of 24 Use man <cmd> and <cmd> --help to read documentation Some commands are just obvious: sort, uniq, date Google command line solutions for common problems “How to capture screen in bash” ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 f.mpg
  • 24.
    Thanks for youattention Questions? 24 of 24