Introduction to bash shell
Why bash?
Why Bash
Some use cases
Bash is a command line shell File management
Give you access to many UNIX utilities Processing data
Efficiently run complex tasks System administration
Make them repeatable Remote access
Scripting
Unix:
Open Group
IBM aix, Solaris, HHP/UX, MacOS
UNIX,Linux and Mac OS
Gnu/LINUX
Linux kernel, GNU Tools
Considered “Unix-like”
UNIX Philosophy:
BSD:
Write programs that do one thing and do it
well OpenBSD, FreeBSD, NetBSD, Mac OS
Write programs to work together
BSD kernel, BSD Tools
Bash is the default shell on Mac OS and Linux but tools and
utilities differ
Introduction
Check that you are using bash : echo $BASH
Output: “/bin/bash”
Commands and Arguments
Command: the first word on the input line
Argument: Everything after that is argument
Ex: cd downloads
Cd is command, downloads is argument
Options: An argument that starts with dash is called an option
Ex: ls –a, ls –l, ls –l –a, ls –la, ls –la/bin
Basic File Management
Creating files and directories:
Mkdir somefolder – create directory “somefolder”
Mkdir a b c d - creates directories a, b, c, d
Creating an empty file: touch
Touch x - creates empty file x
Listing text files:
Cat- Best for small files
Less-Pager with lot of feature
- man less
-use space to move down a page
- Move back a page wit b
Search with “/”
Exit with “q”
Opening other files in Mac OS
MAC OS:
Open index.html - Opens html file in browser
Open . – Opens current directory in Finder
Open –a preview picture..jpg – Opens Picture.jpg with preview
Absolute paths and relative paths
Absolute paths Relative pathhs
Start with / Don’t start with a /
/ Library
/bin/bash alice
/var/log
/home
Copy, Move and Delete
Copy : cp , cp –R source_dir target_dir
cp -R dir1 dir2 dir3 target_dir
Cp –R dir1 file1 dir2 file2 target_dir
Move: mv
Mv a b
Mv a dir/b
Mv a b c dir
Mv fir/a.
Mv * dir
Remove: rm, rmdir, rm –r
Filtering and Processing texts
Editors: nano, Vi(vim), emacs
Sorting:
Command: sort
-r reverses sort
-n sorts numerically
-k sorts by field
Head and Tail
Head : head show first 10 lines of input by default, -n gives no.of lines
Tail: Show last 10 linnes off i/p by default, -n givesno..of lines
Searching with GREP
Grep searches for text in file – grep string file
Or multiple files – grep string *
Imp Options: -I makes search case-insensitive, -c counts occurences, -l shows linne number of occurrence, -v
inverts the search
Advanced tools
Sed
Stream editor
Transform text
Replace words
Most common use: to replace “old” with “new”
Sed ‘s/old/new/g’
Awk
Complete programming language
Jobs and Processes
Job control
^Z Suspends a running job
Fg – send job to foreground
Bg- send job to background
Background jobs : cannot read i/p rfom user,
Killing
End any program with kill
Kill by job id: kill %2, kill %cp
Kill by process id: kill 1234
Hard kill with -KILL