24
GNU/Linux and Bash basics Constantine Nosovsky 1

Linux Shell Basics

Embed Size (px)

DESCRIPTION

High level overview of what Linux OS is and short introduction into Bash and utils

Citation preview

Page 1: Linux Shell Basics

GNU/Linux and Bash basics

Constantine Nosovsky

1

Page 2: Linux Shell Basics

Table of contentsHistory. 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

Page 3: Linux Shell Basics

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

Page 4: Linux Shell Basics

History

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

4 of 24

Page 5: Linux Shell Basics

Linux history today

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

5 of 24

Page 6: Linux Shell Basics

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/bashRelative 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

Page 7: Linux Shell Basics

File system (links)

Symbolic (soft) link (ln -s)

Hard link (ln)

File1 content on disk

Dir1File1 metadataFile2 metadata

Dir2File3 metadataFile4 metadata

File3 content on disk

File1 content on disk

Dir1File1 metadataFile2 metadata

Dir2File1 metadataFile3 metadata

7 of 24

Page 8: Linux Shell Basics

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

Page 9: Linux Shell Basics

File system (some locations)

/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

9 of 24

Page 10: Linux Shell Basics

Users, groups, permissions

/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

10 of 24

Page 11: Linux Shell Basics

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

System processes are run from kernel in a special way (e.g. init)

Daemon – process detached from the session attached

Regular user space processes

11 of 24

Page 12: Linux Shell Basics

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

Page 13: Linux Shell Basics

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

Page 14: Linux Shell Basics

Bash CLI vs. scripts

Execute script#! /bin/bashcommand1command2...

$ ./script.sh

Run commands

$ command1

$ command2

$ …

Interpret scriptcommand1command2...

$ bash ./script.sh

14 of 24

Page 15: Linux Shell Basics

POSIX command line arguments

An option is a hyphen followed by a single character, e.g. -oAn option may require an argument, e.g.-o argument or -oargumentOptions that do not require arguments can be grouped, e.g.-lst is equivalent to -t -l -sOptions can appear in any order, e.g. -lst is equivalent to -tlsOptions can appear multiple times.

Options precede other nonoption arguments: -lst nonoptionThe -- argument terminates options

grep -RE '^\s*<([a-z]+)>.*$' /usr -i --line-number

15 of 24

Page 16: Linux Shell Basics

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

Page 17: Linux Shell Basics

File streams, pipelines, command substitution

File streams >, >>, <echo ‘hello, world!’ > filegrep hello < filefind –name file 2>/dev/null 1>find_resultfind –name file 2>&1

Pipelines stream output of one command to the input of anotherps aux | grep ^gptest | tr -s ' ' | cut -f2 -d ' ' | xargs

Command substitutionecho Current time is `date`

17 of 24

Page 18: Linux Shell Basics

Text processing

Use less to view/navigate the large text files

Count symbols, words, lines with wcPrint file(s) with cat file1 file2 file3Use 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_modifiedUse head/tail for specific line range, e.g.

head –n 10000 dev.log | tail –n 100

18 of 24

Page 19: Linux Shell Basics

Program execution, process management

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, htopSend signal to process with kill <signal> <pid>Run commands as different user using sudo

19 of 24

Page 20: Linux Shell Basics

File system management

Mount volumes to file system like mount /dev/sda1 /mnt/d,mount -o loop file.iso /mnt/cdromGet volume/file(dir) size with df –h / du -sh dirCreate new directories with mkdirChange current directory using cdPrint current directory using pwdList directories and access file metadata with lsUse find to locate files by name, type, size, timestamps, etc

Copy files with cp , move/rename with mv, remove with rmCreate links with ln and soft links with ln -s

20 of 24

Page 21: Linux Shell Basics

Permissions management

Set file permissions with chmod, e.g. chmod o+rw dir -RChange file user/group owner with chown/chgrpSet default permission mask with umaskGet your identity with idUse ls –l to read file permissions, e.g. ls -l file-rw-r--r-- 1 root root 30608 Nov 26 2010 fileUse su, sudo to run as user

Know your limits: mounting options (/etc/fstab), file types

21 of 24

Page 22: Linux Shell Basics

Network

Check server availability with ping, traceroute Get your network configuration with ifconfig, netstatUse download utilities wget, scpYou do even have a text browser links

22 of 24

Page 23: Linux Shell Basics

Learn stuff

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

23 of 24

Page 24: Linux Shell Basics

Thanks for you attention

Questions?

24 of 24