shell - introduction & commands chapter iii / part ii
TRANSCRIPT
Shell - Introduction &
Commands
Chapter III / Part II
Command Line Interface
•Earlier - only command line interface (CLI)• Inheritance from Unix, just like other OS of
that time•CLI poses steep learning curve for some•Unintuitive• Needed to be terse for brevity and other reasons
•Necessary to do many tasks•Convenient for other complex tasks• Much more tedious by GUI
• For eg., delete all .avi files in current directory
• rm *.avi
Shells• Interprets and executes commands•4 - 5 different shells are installed by default•Each shell has slightly different
environment & features•Examples• bash - most common, powerful
• csh - C shell, less features than bash
• sh - Bourne shell, oldest
• zsh - new, quite powerful
• to find out the particular shell• type echo $SHELL
• should return something like /bin/bash
• cat /etc/shells
• to change shell use the command chsh
Shells (contd.)• Some useful key commands• Ctrl - U : delete the whole typed line
• Ctrl - C : abort a running command
• Ctrl - Z : suspend a running command
• stty -a gives a list of terminal settings
• Terminal emulators• GUI window lets you interact with a shell
• Just a container for the shell
• gnome-terminal is GNOME’s most used emulator
• KDE uses konsole
• No emulator needed if logging on to terminal directly
• Start a terminal (without X) by using CTR-Alt-F1 to Ctrl-Alt-F8• Ctrl-Alt-F7 is X (the GUI)
Common commands:Navigating directories
•Linux (as Windows) has hierarchical directory structure
•Each component is seperated by forward slash - /
• ‘/’ is the root directory •Examples• /usr/bin/
• /home/raheel
•To find out the current directory:• pwd - returns the current directory
•To change to another directory:• cd /path/to/dir
• . is the current directory• .. is the parent directory
Paths
•Paths define the location of a file or directory• seperated by slashes: /
•Could be absolute or relative•Absolute• starts with the root directory
• /home/raheel/paper.txt , /usr/local/bin
•Relative• in relation to the current directory
• pwd -> /home/raheel, cd hw -> /home/raheel/hw
• cd .. -> /home/raheel
Important directories
• /bin : essential Unix commands, like ls
• /usr/bin : some extra Unix commands
• /usr/sbin : for super user, system administration
• /boot : kernel and other files for booting
• /etc : system services - networking, mail, disk management
• /var : administrative files, such as logs
• /usr/local : locally added programs and files by sys. admin.
• /dev : device files - interface b/w hardware and filesystem
• /proc : running processes
Navigating directories (contd.)
•cd by itself - takes to home directory• cd ~ also
• Home directory also denoted by ‘~’
•cd . doesn’t do anything•cd .. takes you to parent directory•To make a directory:• mkdir dirname
• creates new directory under current one
• mkdir /path/to/dirname
• creates new directory under the exact path
• rmdir removes a directory
Listing files
•ls - the most popular command• lists the directory contents• lots of options available with arguments• argument - option given to a command
•ls• lists current directory’s contents
•ls /home• lists the contents of /home directory
•ls -a• list hidden files with the -a argument
•ls -l• ‘long’ information about contents. Very useful
What is a command?
•Small (big) program provided by UNIX/Linux•Can make your own commands too•Command is simply a file• Have to mark it executable
• Put it somewhere special (in command path)
•Path : directories where shell looks for a given command
•find out Path by echo $PATH• /usr/local/bin/:/usr/bin:/bin/•Sometime have to specify full (absolute
path) to a command• command not in path
• more than one name for same command
Command (contd.)•Add a directory to path
• export PATH=$PATH:/new/directory/path
•current directory usually not in path•use ./command-name
•Help for commands• man command-name
• man 2 command-name : for 2nd section
• apropos edit : displays all commands related to editing
•Put a command in background• ls & -> [1] 23142
•Bring it back• fg %1 or fg 23142
•Commands and shell provided features• type ls
• type cd
Typing shortcuts
•Word Completions• Don’t have to type full command or path
• Type Tab after typing a couple of letters
• cd /usr/inc - then hit Tab
• would complete it as cd /usr/include/
• completes the path
• Works on commands too
• Type mor and then hit Tab
• Will complete it as the command more
• If more than one match, the displays the options
• Type cd /usr/l and hit Tab
• Would display lib and local because they begin with l
Command line editing
•Can edit previously typed commands•Commands are stored once typed• No. of commands to be stored in history can be
set
•Can recall previous commands and use them
•Up arrow - previous command•Down arrow - next command•Left and right arrow to navigate•Ctrl-U to delete whole line if mistyped•Ctrl-A : beginning of line•Ctrl-E : end of line
Command history in detail!
•Can view previous commands in one display
• type history• displays history of commands, numbered
•history N• displays only N previous commands
•history -c• deletes all commands in history
•!!• execute previous command
•!N• execute the Nth command from top
Exapnding filenames (wildcards)
•Specify a group of files/directories with a single word
• if ls givespaper1.doc paper2.doc paper3.doc papa.doe prayer.doc thesis.doc
work.ppt
• Then ls *.doc will givepaper1.doc paper2.doc paper3.doc prayer.doc thesis.doc
•* says ‘match as many characters as possible in place of *’
•? says ‘match one character in place of ?’• ls paper?.doc gives
paper1.doc paper2.doc paper3.doc
More wildcards
•Can use * in between a word• ls p*.doc will givepaper1.doc paper2.doc paper3.doc
•Can specify characters instead of ? and *• ls paper[123].doc will give
paper1.doc paper2.doc paper3.doc
• ls paper[2-3].doc will givepaper2.doc paper3.doc
•Can combine wildcards• ls pap*.do? will givepaper1.doc paper2.doc paper3.doc papa.doe
Startup files•Customize environment before starting the shell
•Default startup file: /etc/profile• Interactive and non-interactive shell
sessions• Interactive: where user can enter commands for
that session
• login; same as bash --login; uses.bash_profile
• non-login: changing users, by bash, uses .bashrc
• Non-interactive: started by other processes, no user control
•Can contain commands and programs • comment: #this is a comment line• PS1 = \u
• HISTSIZE = 50
• path definition: PATH=/usr/local/bin:/bin:/usr/bin
• rename commands: alias rm=rm -i
Some file management commands
•touch filename - create an empty file•Copying files
• cp /file/to/copy /where/to/copy
• cp -r /dir/to/copy /where/to/copy
•Moving files• mv /original/file/location /new/file/location
•Deleting files • rm /file/to/remove
• rm -r /dir/to/remove
• rm -f /file/to/remove
Processes
•Core concept of Linux• Independent running programs, responsible
for different tasks•Can communicate between each other• ls | more
•To view processes• ps : view personal processes
• ps -a : view processes from all users
• ps -aux : view all processes including those with no terminal
• last command sorts by CPU usage
•more or less
Killing processes
• If can’t kill a process by Ctrl-c use kill command
•ps -aux shows all running commands•Note PID for the process•kill PID
•kill -9 PID : un-ignorable kill•kill -STOP PID•kill -CONT PID•need to be root to kill other’s processes
• Input to a command could be from anywhere• keyboard - default standard input (stdin)
• file
• another process’ output
•Output from a command could go anywhere• monitor - default standard output (stdout)
• file
• another process’ input
•Also, standard error (stderr) - all errors go here• by default - monitor display
Standard Input / Output
Output redirection•To redirect stdout from a command use ‘>’•ls -l /etc/ > listing.txt• redirects the output from ls command to the file
listing.txt
•cat file2• displays file2’s content on display
•cat file2 > file2_copy• copies the content to file2_copy (using redirection)
• If file2 doesn’t exist, above generates error• cat: file3: No such file or directory
• Redirect error using ‘2>’
• cat file2 2> file_error.txt
•To redirect both stdout and stderr• cat file2 > file2_copy 2>file_error.txt
• copies standard output to file2_copy and error to error.txt
• cat file2 > file2_copy 2>&1
• copies either error or standard output to file2_copy
Input Redirection
•Works similar to output redirection•Can read from file or another command•sort command• by itself gives sorts lines typed on the command
line
• sort < listing.txt
• sorts the lines stored in listing.txt
•Can combine output and input• sort < listing.txt > sorted_listing.txt
•Word count• wc < listing.txt
• counts lines, words, bytes in listing.txt
More stdin/stdout
•To redirect output to nothing: use /dev/null• /dev/null - special file, size always 0. doesn’t
retain anything
• cat listing.txt > /dev/null : gets rid of output
•To redirect to printer• sort < listing.txt > /dev/lp0
•To append to a file• cat file2 > file3 erases contents of file3
• cat file2 >> file3 appends to file3
• cat >> file3 appends whatever is typed to file3
Pipes
•Connect standard output of a command to standard input of another• channel output to input
•Can be done using redirection• To count number of files in /etc
• ls /etc > output.txt
• wc output.txt
• Two lines needed, a temporary file created
•Better to use pipes• ls /etc | wc - same result as above
More Pipes
•Most powerful feature of shell•Gives great flexibility to perform complex
tasks simply•Commands can be modular and simple
functioning• It is this combination of commands that give power
to Linux
•Print a list of current users on the computer• who | lpr
•View the output of a listing one screen at a time• ls /etc | less
•Mostly used with filters
Filters
•Small utility commands • Take input from stdin
• Do some operation
• Post output to stdout
•Example• cat
• grep - search text
• sort - sort according to some criteria
• head - display first few lines of text
• tail - display last few lines of text
•Very useful when combined with other commands using pipes
Filters in action
•Search files in /etc containing ‘pe’ in their names• ls /etc/ | grep pe
•Printing the above files• ls /etc/ | grep pe | lpr
•View the first few files in listing for /etc• ls /etc/ | head
•Total statistics of file1, file2, file3• cat file1 file2 file3 | wc
•View all files in /etc/ last modified in January• ls -l | grep "Aug"