You are on page 1of 28

Tutorial 1: Shell

 Primary – man(manual) pages.


man <command> shows all information about the
command
 <command> --help shows the available options
for that command
Basic Commands
 ls
 $ ls -l  ".." (dot dot) -
 $ ls -a refers to the parent
 $ ls -la directory
 $ ls -l --sort=time  "~" (Tilda) or "~/" -
 $ ls -l --sort=size -r refers to user's home
 cd directory
 $ cd /usr/bin  “*” (star) -
 pwd refers to any file
 $ pwd names
 ~
 $ cd ~
 ~user
 $ cd ~mydir
 What will “cd ~/mydir” do?
Basic Commands (cont)
 echo  rm
 $ echo “Hello World”  $ rm foo
 $ echo -n “Hello World”  $ rm -rf foo
 cat  $ rm -i foo
 $ cat /etc/motd  $ rm -- -foo
 $ cat /proc/cpuinfo
 cp
 $ cp foo bar
 $ cp -a foo bar
 mv
 $ mv foo bar
 mkdir
 $ mkdir foo
Basic Commands
 tar
 $ tar cvfp lab1.tar lab1
 gzip
 $ gzip -9 lab1.tar
 untar & ungzip
 $ gzip -cd lab1.tar.gz |
tar xvf –
 $ tar xvfz lab1.tar.gz
 touch
 $ touch foo
 $ cat /dev/null > foo
Basic Commands

 Disk usage
$ df -h /
 File space usage
$ du -sxh ~/
 quota – display disk usage and limits
Usage: quota [OPTION]
eg. quota -v
 Delete
Vim – dd (delete a line)
 2 modes – d10d (delete 10 lines)
 Input mode – d$ (delete till end of line)
 ESC to back to cmd mode – dG (delete till end of file)
 Command mode – x (current char.)
 Cursor movement
 Paste
– h (left), j (down), k (up),
l (right) – p (paste after)

– ^f (page down) – P (paste before)

– ^b (page up)  Undo


^ (first char.)
– – u
– $ (last char.)  Search
– G (bottom page)
– /
– :1 (goto first line)
 Save/Quit
 Swtch to input mode
– :w (write)
– a (append)
– :q (quit)
– i (insert)
– o (insert line after – :wq (write and quit)

– O (insert line before) – :q! (give up changes)


A Pattern is an  ab*cd matches
expression that anything that starts
describes a set of with ab and ends with
strings which is used to cd etc.
give a concise
description of a set,  ls *.txt – prints all text
without having to list all files
elements.
Linux File Permissions
 3 types of file permissions – read, write, execute
 10 bit format from 'ls -l„ command
1 234 567 8 9 10
file type owner group others
eg. drwxrw_r__
 Means owner has all three permissions,
 group has read and write, others have only read
 permission read permission – 4, write – 2, execute 1
rwxrwr=
673 =
Linux File Permissions
 chmod – change file access permissions
Usage: chmod [OPTION] [MODE] [FILE]
eg. chmod 744 calculate.sh

 chown – change file owner and group


Usage: chown [OPTION]... OWNER[:[GROUP]]
FILE...
eg. chown remo myfile.txt
Process Management
 ps – report a snapshot of the current processes
Usage: ps [OPTION]
eg. ps, ps –el

 kill – to kill a process(using signal mechanism)


Usage: kill [OPTION] pid
eg. kill -9 2275
Process Management
 bg – make a foreground process to run in
background
Usage: type 'ctrl+z' and then 'bg <job id>'
 fg – to make background process as foreground
process
Usage: fg [jobid]
 jobs – displays the names and ids of background
jobs
Usage: jobs
Bash commands
 pushd

 popd
Bash commands
 alias
alias dog=tac
alias c='clear„
alias rm='rm -i'

 How to undo an alias


Bash commands
 ; semicolon
two or more commands on the same line separated by a semicolon ; . The
shell will scan the line until it reaches the semicolon. All the arguments
before this semicolon will be considered a separate command from all the
arguments after the semicolon. Both series will be executed sequentially
with the shell waiting for each command to finish before starting the next
one.
echo Hello ; echo World

 & ampersand
sleep 20 &
 && double ampersand
cd mydir && ls
Bash commands
 || double vertical bar
echo first || echo second ; echo third
rm file1 && echo It worked! || echo It failed!
 # pound sign
 \ escaping special characters
echo hello \; world
Bash commands
 $ dollar sign: Environment variables
$HOSTNAME, $USER, $UID, $SHELL, and
$HOME.
echo This is the $SHELL shell
echo The userid of $USER is $UID
 creating variables
MyVar=555
echo $MyVar
 quotes: difference between “ and ‘
 How to remove a variable
Bash commands
 shell history
repeating the last command !!
repeating other commands !ca
history 10
 The bash shell ~/.bash_history.
file commands
 Hard, soft (symbolic) link

 Symbolic links refer to a symbolic path


indicating the abstract location of another file
ln -s {/path/to/file-name} {link-name}
ln -s firefox-2.0.0.3 firefox

 Hardlinks, refer to the specific location of


physical data.
ln vmlinuz-2.6.24.4 vmlinuz
everything is a file
 A directory is a special kind of file, but it is still a
(case sensitive!) file.
 Each terminal window (for example /dev/pts/4),
any hard disk or partition (for example /dev/sdb1)
and
 any process are all represented somewhere in the
file system as a file.
 It will become clear throughout this course that
everything on Linux is a file.
everything is a file
 The file utility determines the file type. Linux does
not use extensions to determine the file type. The
command line does not care whether a file ends in
.txt or .pdf.

 file <filename>
working with file contents
 Look at the contents of text files with head, tail,
cat, tac, more, less and strings.
filesystem hierarchy standard
 man hier
 the root directory /
 /home
ls -d /home/myname/.*
filesystem hierarchy standard
I/O redirection
 stdin, stdout, stderr

echo It is cold today! > winter.txt


cat winter.txt
 > sign will clear the file!
 >> append
 redirection of stdout to a file, and stderr to
/dev/null
find / > allfiles.txt 2> /dev/null
filters
 Commands created to be used with a pipe are
often called filters
 tee
 grep, grep –i, grep -v
 cat tennis.txt
Amelie Mauresmo, Fra
Kim Clijsters, BEL
Justine Henin, Bel
Serena Williams, usa
Venus Williams, USA

cat tennis.txt | grep Williams


Serena Williams, usa
Venus Williams, USA
filters
 cut -d" " -f1 tennis.txt
 cat tennis.txt | tr 'e' 'E‟
 cat tennis.txt | tr 'a-z' 'A-Z„

 tr -s filter
 tr to 'encrypt' texts with rot13
cat count.txt | tr 'a-z' 'nopqrstuvwxyzabcdefghijklm„

 Counting words, lines and characters is easy with


wc.
 sort
File Systems
 fdisk – partition manipulator
eg. sudo fdisk l

 mount – mount a file system


Usage: mount -t type device dir
eg. mount /dev/sda5 /media/target

 umount – unmount file systems


Usage: umount [OPTIONS] dir | device...
eg. umount /media/target

You might also like