Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shell - Introduction & Commands Chapter III / Part II.

Similar presentations


Presentation on theme: "Shell - Introduction & Commands Chapter III / Part II."— Presentation transcript:

1 Shell - Introduction & Commands Chapter III / Part II

2 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

3 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

4 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)

5 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

6 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

7 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

8 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

9 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

10 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 not in path more than one name for same command

11 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

12 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

13 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

14 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

15 Exapnding filenames (wildcards) Specify a group of files/directories with a single word if ls gives paper1.doc paper2.doc paper3.doc papa.doe prayer.doc thesis.doc work.ppt Then ls *.doc will give paper1.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

16 More wildcards Can use * in between a word ls p*.doc will give paper1.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 give paper2.doc paper3.doc Can combine wildcards ls pap*.do? will give paper1.doc paper2.doc paper3.doc papa.doe

17 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

18 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

19 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

20 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 -STOP PID kill -CONT PID need to be root to kill other’s processes

21 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

22 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

23 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

24 More stdin/stdout To redirect output to nothing: use /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

25 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

26 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

27 Filters Small utility commands Take input from stdin Do some operation Post output to stdout Example cat grep - search text 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

28 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"


Download ppt "Shell - Introduction & Commands Chapter III / Part II."

Similar presentations


Ads by Google