LINUX By OpenGurukul.com Free/Open Source Software Laboratory
LINUX Module : History www.opengurukul.com 2
Unix : History UNIX Variants IBM AIX Oracle/Sun Solaris HP HP-UX www.opengurukul.com 3
Unix : History LINUX (Unix like operating system) Distributions Redhat Fedora (Free Edition), RHEL (Redhat Enterprise Linux) Novell SLED(Suse Linux Enterprise Desktop), SLES (Suse Linux Enterprise Server) Canonical Ubuntu (derived from Debian) Oracle Enterprise Linux (derived from RHEL) CentOS (derived from RHEL) www.opengurukul.com 4
LINUX Module : File Systems www.opengurukul.com 5
Linux : File Systems Computers store information in things called files. A file is like a sheet of paper with something writ- ten on it. Files are stored in things called directories (popu- larly known as folders on windows). www.opengurukul.com 6
Linux : File Systems : pwd pwd : print name of current/working directory Usage: $ pwd Example: $ pwd /home/sachin $ echo $HOME /home/sachin www.opengurukul.com 7 $
Linux : File Systems : cd cd : change directory Used to change current working directory Usage: $ cd <dirname> Example: $ cd /tmp $ pwd /tmp $ www.opengurukul.com 8
Linux : File Systems : cd to home directory cd ~ : cd to home directory cd : cd without arguments to go to home directory Example : Example : $ cd ~ ; pwd $ cd ; pwd /home/sachin /home/sachin $ $ www.opengurukul.com 9
Linux : File Systems : cd cd .. : cd to parent directory cd - : cd to previous working directory Example : Example: $ cd ~ ; pwd $ cd /z/y/x/w/v/u; pwd /home/sachin /z/y/x/w/v/u $ cd .. ; pwd $ cd /a/b/c/d/e/f; pwd /home /a/b/c/d/e/f $ $ cd -; pwd /z/y/x/w/v/u www.opengurukul.com 10 $
Linux : File Systems : mkdir mkdir : make directories Example : $ cd $HOME Options : $ mkdir lab -p : no error if it already $ mkdir lab exists, make parent mkdir: cannot create directory directories as needed `lab': File exists $ mkdir -p lab Usage : $ mkdir -p lab/scratch $mkdir <dirname> www.opengurukul.com 11
Linux : File Systems : cat cat – concatenate files and print on the standard output Usage: $ cat <list of files> Example: $ cat /etc/issue Fedora release 14 (Laughlin) Kernel r on an m (l) $ www.opengurukul.com 12
Linux : File Systems : head head – output the first part of files The head command without any argument displays first 10 lines of a file. Usage: $ head <filename> Example: To display first two lines $ head -2 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin $ www.opengurukul.com 13
Linux : File Systems : tail tail – output the last part of files The tail command without any argument displays last 10 lines of a file. Usage: $ tail <filename> Example: To display last two lines $ tail -2 /etc/passwd oracle:x:501:501::/usr/lib/oracle/xe:/bin/bash tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat6:/sbin/nologin $ www.opengurukul.com 14
Linux : File Systems : more more : paging through text one screenful at a time. The command also shows % of text already covered at bottom. Usage: $ more <filename> Commands: To quit – press q To go to next page – press space bar To go to prev page for files (not for pipes) – press b Example: $ more /etc/httpd/conf/httpd.conf www.opengurukul.com 15 $
Linux : File Systems : less less : similar to more. Less allows backward movement in the file as well as forward movement Usage: $ less <filename> Commands: To quit – press q To go to prev/next page – press page up/down Example: $ less /etc/httpd/conf/httpd.conf www.opengurukul.com 16 $
Linux : File Systems : touch touch – create a zero size file / change time stamp Creates a zero size file if the file doesn't exist. Change modification time if the file already exists. Usage: $ touch <filename> Example: $ cd ~/lab/scratch $ touch touch.txt $ cat touch.txt $ www.opengurukul.com 17
Linux : File Systems : echo echo – display text to standard output Usage: $ echo <text> Example: $ cd ~/lab/scratch $ echo “Hello World” Hello World $ www.opengurukul.com 18
Linux : File Systems : ls ls - list directory contents Options: -a show entries starting with dot (.) and dot dot (..) -l show long listing format -t sort by modification time -r reverse the order Example: $ cd /home $ ls sachin surikuma root ksurinde laxmi yukta kumasuri sunil saurav $ www.opengurukul.com 19
Linux : File Systems : ls -a ls -a : do not ignore entries starting with dot Example : $ cd ~/lab/scratch $ touch .system.txt $ ls echo.txt touch.txt $ ls -a .system.txt echo.txt touch.txt $ www.opengurukul.com 20
Linux : File Systems : ls -l ls -l : use a long listing format <file type> <permissions> <link count> <file owner> <group of owner> <file size> <date of modification> <time of modification> <file name> Example : $ cd /bin $ ls -l *sh* -rwxr-xr-x. 1 root root 877480 Jun 22 2010 bash -rwxr-xr-x. 1 root root 102408 Jun 21 2010 dash lrwxrwxrwx. 1 root root 4 Jan 27 2011 sh -> bash -rwxr-xr-x 1 root root 597768 Mar 22 2010 zsh www.opengurukul.com 21 $
Linux : File Systems : ls -t ls -t : sort by modification time Example : $ cd /bin $ ls -lt *sh* lrwxrwxrwx. 1 root root 4 Jan 27 2011 sh -> bash -rwxr-xr-x. 1 root root 877480 Jun 22 2010 bash -rwxr-xr-x. 1 root root 102408 Jun 21 2010 dash -rwxr-xr-x 1 root root 597768 Mar 22 2010 zsh $ www.opengurukul.com 22
Linux : File Systems : ls -r ls -r : reverse order while sorting Example : $ cd /bin $ ls -ltr *sh* -rwxr-xr-x 1 root root 597768 Mar 22 2010 zsh -rwxr-xr-x. 1 root root 102408 Jun 21 2010 dash -rwxr-xr-x. 1 root root 877480 Jun 22 2010 bash lrwxrwxrwx. 1 root root 4 Jan 27 2011 sh -> bash $ www.opengurukul.com 23
Linux : File Systems : ln ln : create hard/soft links between files Soft Link (Symbolic Link): The symbolic link contain path of the target link. The symbolic link can cross boundaries of file systems. Hard Link: Hard links are supported within a file system and these are used to refer to an existing content by some other name within the same file system. The inode number will be same for hard links. Most of the file systems do not support hard linking directories. www.opengurukul.com 24
Linux : File Systems : ln & ln -s Usage: Example : $ ln -s <old path> <new_name> Symbolic Link (ln -s): -s : create a symbolic link $ cd /bin without -s : creates a hard link $ ln -s bash sh $ NOTE : On AIX, /bin/sh is a hard linked Hard Link (ln): to /bin/ksh. $ cd /bin On Linux, /bin/sh is a soft link $ ln ksh sh to /bin/bash. $ www.opengurukul.com 25
Linux : File Systems : soft link Example : $ ls -l /bin | grep -e ^l | grep -e vi lrwxrwxrwx. 1 root root 2 Jan 27 2011 ex -> vi lrwxrwxrwx. 1 root root 2 Jan 27 2011 rvi -> vi lrwxrwxrwx. 1 root root 2 Jan 27 2011 rview -> vi lrwxrwxrwx. 1 root root 2 Jan 27 2011 view -> vi $ www.opengurukul.com 26
Linux : File Systems : cp cp: copy files and directories SYNOPSIS : cp SOURCE DEST cp SOURCE... DIRECTORY Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY Option : -R copy directories recursively (otherwise directories will be omitted) www.opengurukul.com 27
Linux : File Systems : cp cp: copy files and directories Example : $ cp ~/.bashrc ~/.bashrc.orig $ cp /etc/profile.d ~/ cp: omitting directory `/etc/profile.d' $ cp -R /etc/profile.d ~/ $ www.opengurukul.com 28
Linux : File Systems : mv mv: move (rename) files SYNOPSIS : mv [OPTION]... SOURCE DEST mv [OPTION]... SOURCE... DIRECTORY DESCRIPTION Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY Example: $ cp ~/.bashrc /tmp $ mv /tmp/.bashrc /tmp/.bashrc.new $ ls /tmp/.bashrc* /tmp/.bashrc.new www.opengurukul.com 29 $
Linux : File Systems : rm rm : remove files or directories SYNOPSIS: rm [OPTION]... FILE... Options: -i : interactive (prompt before every removal) -f : ignore nonexistent files, never prompt (force) -r : remove directories and their contents recursively www.opengurukul.com 30
Linux : File Systems : rm example Example (rm) : $ cd /tmp $ touch rm1.out $ rm rm1.out $ ls rm1.out ls: cannot access rm1.out: No such file or directory $ www.opengurukul.com 31
Linux : File Systems : rm -i Example (rm -i) : $ cd /tmp $ touch rm2.out $ rm -i rm2.out rm: remove regular file 'rm2.out'? Y $ ls rm2.out ls: cannot access rm2.out: No such file or directory $ www.opengurukul.com 32
Linux : File Systems : rm -f Example (rm -f) : $ rm non_existent_file.txt rm: cannot remove `non_existent_file.txt': No such file or directory $ rm -f non_existent_file.txt $ www.opengurukul.com 33
Linux : File Systems : rm -r Example (rm -r) : $ cd /tmp $ mkdir scratch $ rm scratch rm: cannot remove `scratch/': Is a directory $ rm -rf scratch $ www.opengurukul.com 34
LINUX MODULE : VI – Text Editor www.opengurukul.com 35
LINUX: VI Editor To invoke Type vi or 'vi filename'. To come out of vi editor Use ESC (get into command mode), followed by : (ex editor mode), followed by q (quit) and then press ENTER. $ vi Hi I am Open Gurukul :q $ www.opengurukul.com 36
LINUX: VI Editor : Mode The VI editor run in 3 different modes INSERT mode COMMAND mode ED/EX Editor mode www.opengurukul.com 37
LINUX: VI Editor : Command Mode Command mode Use ESC to go to COMMAND mode anytime. The typed character will not be visible in COMMAND mode. Under COMMAND mode, each character has special significance and the designated action takes place. www.opengurukul.com 38
LINUX: VI Editor : INSERT Mode INSERT mode The characters typed are visible when the editor is in INSERT mode. To get into INSERT mode from command mode i – insert shift+a – insert at end of line www.opengurukul.com 39
LINUX: VI Editor : EX Mode ED/EX Editor mode The ED/EX editor commands are typed after typing ':' in command mode. From INSERT mode, we can use ESC + ':' to get into ED/EX mode. www.opengurukul.com 40
LINUX: VI Editor : Commands INSERT i – insert shift+a – append at end of line DELETE x – delete a single character dw – delete a word dd – delete a line www.opengurukul.com 41
LINUX: VI Editor : Commands Undo / Redo u – undo the last commands action . (dot) – redo the last commands action Repeat Action <repeat_count>command 5dd – deletes 5 lines www.opengurukul.com 42
LINUX: VI Editor : Cursor Movement Cursor Movement Commands /| h – move left on the current line | j – move down one line | k – move up one line <-- -- -- h j k l -- -- --> l - move right on the current line | | !/ www.opengurukul.com 43
LINUX: VI Editor :Ex :Save & Quit :w write contents to a file :w /tmp/1.out write contents to file /tmp/1.out :q quit (will not work if there were changes after last write) :wq write and quit :q! quit (ignore recent modifications) www.opengurukul.com 44
LINUX: VI Editor : Copy & Paste yy – yank line into buffer <number_of_lines>yy – yank multiples lines into buffer (2yy – yank 2 lines into buffer) Go to line where the content of buffer has to be pasted. Type p to print contents of buffer www.opengurukul.com 45
LINUX: VI Editor: Search Pattern /pattern To goto a line that matches pattern. ?pattern To go to a line that matches pattern in the reverse direction. n Go to next such pattern match. N (shift+n) Go to next such pattern match in reverse direction. :set ic www.opengurukul.com 46 To ignore case while searching, set ic.
LINUX: VI Editor: Line Numbers Display Line Numbers :set nu # To display line numbers Do not Display Line Numbers :set nonu # To stop displaying line numbers To goto a particular line :1 # first line in the file :$ # last line in the file :line_num # Goto line number line_num www.opengurukul.com 47
LINUX: VI Editor: Search & Replace To replace old pattern with the new pattern in entire file. :1,$s/old/new/g replace old with new in entire file from first line to last line. :.,+50s/old/new/g replace old with new in file from current line (.) to next 50 lines www.opengurukul.com 48
LINUX: VI Editor: Editor Options To see options that are currently set. :set To see all the options :set all www.opengurukul.com 49
LINUX: VI Editor: Multiple Files To operate on multiple files, specify all of them while invoking vi editor. $ vi /tmp/1.txt /tmp/2.txt :files # to see files opened by vi editor :n # to go to next files :rew # to rewind to the first file $ www.opengurukul.com 50
LINUX Module : Environment www.opengurukul.com 51
Linux : Environment : printenv printenv : print all or part of environment Usage: printenv Example: $ printenv | grep -e HOME -e PWD -e SHELL -e USER -e HOSTNAME HOSTNAME=sachin-pc SHELL=/bin/bash OLDPWD=/tmp USER=sachin PWD=/home/sachin/lab/linux/scratch HOME=/home/sachin $ www.opengurukul.com 52
Linux : Environment : export export: exports an environment variable To set an environment variable so that it is visible to even child processes. Example : $ export MY_NAME=sachin1 $ echo $MY_NAME To set an environment variable $ MY_NAME=sachin1 (It will not be visible to child processes.) $ echo $MY_NAME www.opengurukul.com 53
LINUX : Environment : PS1 PS1 : stores value of primary prompt Example : $ export PS1='c: > ' # change the prompt c: > export PS1='$' # revert the prompt $ www.opengurukul.com 54
LINUX : Environment : PS2 PS2 Stores value of secondary prompt. Used when the command line is incomplete. Default secondary prompt is '>'. Example : $ echo 'hello >' $ www.opengurukul.com 55
Linux : Environment: HOME and PATH HOME : stores home directory of user. $ echo $HOME /home/sachin $ PATH : stores list of directories that should be searched for the executable if the absolute/relative path of an executable has not been specified. $ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin $ www.opengurukul.com 56
Linux : Environment: PWD & OLDPWD PWD : has value of current working directory. $ cd /tmp $ echo $PWD /tmp $ OLDPWD : has value of last working directory $ cd /var; cd /tmp $ echo $OLDPWD /var $ NOTE : The value of OLDPWD is used by command “cd -” to go back to last working www.opengurukul.com 57 directory
Linux : Environment: SHELL, HOSTNAME, USER SHELL : To know path of SHELL program. $ echo $SHELL /bin/bash $ HOSTNAME : To know host name $ echo $HOSTNAME sachin-pc $ USER : Name of User logged in. $ echo $USER sachin www.opengurukul.com 58 $
Linux: Environment: BASH Files /etc/profile : system wide initialization file It is executed for login shells. $HOME/.bash_profile : personal initialization file It is executed for login shells. $HOME/.bash_logout : individual login shell cleanup file It is executed when a login shell exits. $HOME/.bash_history : command history list file It stores commands executed by us. www.opengurukul.com 59
Linux: Environment: History history : display the command history list with line numbers The history command displays the command history list with line numbers. $ history 1037 cd /var; cd /tmp; 1038 echo $OLDPWD $ Execute a command from history : $ !1037 # to execute 1037th command. HISTSIZE : number of commands to be stored in history file $ echo $HISTSIZE 1000 $ www.opengurukul.com 60
Linux Module : FILE ACCESS www.opengurukul.com 61
Linux: File Access: whoami whoami : print effective user id $ whoami sachin $ groups : print the groups a user is in $ groups sachin $ www.opengurukul.com 62
Linux: File Access: id & chmod id : print user identity $ id uid=500(surikuma) gid=500(surikuma) groups=500(surikuma) $ chmod : change file mod bits www.opengurukul.com 63
Linux: File Access: file type <filetype> <rwx perm for owner> <rwx perm for group> <rwx perm for others> Regular file : '-' Directory : 'd' Pipes : 'p' Character device special files : 'c' Block device special files : 'b' For symbolic links : 'l' For sockets : 's' www.opengurukul.com 64
Linux: File Access: chmod User / Group / Other Read / Write / Execute r(read) u (user) w(write) g (group) x(execute) o (other) Octal Numbers for R / W / X 4 – read 2 – write 1 - execute (file) / search (directory) www.opengurukul.com 65
Linux: File Access: chmod Permissions Calculations Octal Number RWX 4+2+1 7 RW- 4+2 6 R-X 4+1 5 R-- 4 4 -WX 2+1 3 -W- 2 2 --X 1 1 www.opengurukul.com 66
Linux: File Access: chmod To give all the permissions to everybody on a file/directory $ chmod 777 path $ To give execute permission on a file $ chmod +x path $ To give execute permission on a file to user alone $ chmod u+x path www.opengurukul.com 67 $
Linux: File Access: umask umask : unix mask $ umask 022 $ Initial permission for new file : 666 - umask new directory : 777 - umask NOTE : On Linux, umask is 002 in case user name and group name are same. www.opengurukul.com 68
Linux: File Access: chown chown : change file owner & group The -R option is used to change the ownership recursively. $ chown -R <username>:<group name> <path> www.opengurukul.com 69
Linux MODULE : Filters (wc, grep, sed, awk, sort) www.opengurukul.com 70
Linux: Filters: wc wc : print newline, word, and byte counts for each file $ wc /etc/passwd 56 98 2916 /etc/passwd $ wc -l : print the newline counts $ wc -l /etc/passwd 56 /etc/passwd $ NOTE: The wc stands for wordcount. www.opengurukul.com 71
Linux: Filters : wc wc -w : print the word counts $ wc -w /etc/passwd 98 /etc/passwd $ wc -c : print the byte counts $ wc -c /etc/passwd 2916 /etc/passwd $ www.opengurukul.com 72
Linux: Filters: grep grep : print lines matching a pattern Syntax : grep [OPTIONS] PATTERN [FILE...] Example : $ grep root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin $ www.opengurukul.com 73
Linux: Filters: grep -e & -E grep -e PATTERN : use PATTERN for search $ grep -e sync -e shutdown /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown $ grep -E PATTERN : egrep - use extended PATTERN for search $ grep -E 'sync|halt' /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync halt:x:7:0:halt:/sbin:/sbin/halt www.opengurukul.com 74 $
Linux: Filters: grep -f grep -f file : patterns are kept in file $ cat /tmp/patterns.txt sync halt $ grep -f /tmp/patterns.txt /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync halt:x:7:0:halt:/sbin:/sbin/halt $ www.opengurukul.com 75
Linux: Filters: grep -v grep -v : Invert the sense of matching, to select non- matching lines $ cat /etc/passwd | grep -v nologin | grep -v bash sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt jetty:x:490:481::/usr/share/jetty:/bin/sh $ www.opengurukul.com 76
Linux: Filters: grep -n grep -n : Prefix each line of output with the line number within its input file $ grep -n -e halt -e sync -e shutdown /etc/passwd 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt $ www.opengurukul.com 77
Linux: Filters: grep -i grep -i : Ignore case distinctions in both the PATTERN and the input files $ grep ROOT /etc/passwd $ grep -i ROOT /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin $ www.opengurukul.com 78
Linux: Filters: sort sort : sort lines of text Example : files $ sort lamp.txt $ cat lamp.txt lang: php, perl, python os: linux os: linux webserver: apache rdbms: mysql rdbms: mysql webserver: apache lang: php, perl, python $ $ www.opengurukul.com 79
Linux: Filters: sort -n sort -n : compare $ sort sort.txt according to string 10 numerical value 2 $ $ cat sort.txt $ sort -n sort.txt 2 2 10 $ 10 $ www.opengurukul.com 80
Linux: Filters: sort -u sort -u : unique records $ sort sort_u.txt The uniq command also hello select unique adjacent lines. hello The uniq command is also dependent on sort command HELLO to sort the records. HELLO $ cat hello.txt $ HELLO hello $ sort -u sort_u.txt HELLO hello hello HELLO $ $ www.opengurukul.com 81
Linux: Filters: sort -r sort -r : reverse the result Example : of comparisons $ sort -r lamp.txt $ cat lamp.txt webserver: apache os: linux rdbms: mysql webserver: apache os: linux rdbms: mysql lang: php, perl, python lang: php, perl, python $ $ www.opengurukul.com 82
Linux: Filters: sed sed – stream editor used for search and replace operation on a file. Syntax : sed -e 's/old pattern/new word/gi' file /g is to replace globally. Multiple patterns on the same line will be replaced. /i is to ignore care while matching pattern www.opengurukul.com 83
Linux: Filters: sed -e Example: Example: $ sed -e 's/mysql/oracle/gi' $ sed -e 's/mysql/oracle/gi' lamp.txt -e 's/php/perl/gi' lamp.txt os: linux os: linux lang: php, perl, python lang: perl, perl, python rdbms: oracle rdbms: oracle webserver: apache webserver: apache $ $ www.opengurukul.com 84
Linux: Filters: awk awk - pattern scanning and AWK Variables : processing language $0 – current record awk is mainly used for $1 – first field of current selecting fields from the record output. $2 – second field of current Syntax : record awk -Ffs ' /pattern/ {action}' ... Option : $NF – last field of C.R. -F is used to specify field NF – number of fields in C. separator (default is white R. space) NR – number of records read www.opengurukul.com 85 so far
Linux: Filters: awk example Example : To display first and last entry for root user. $ awk -F: ' /^root/ { printf("%s %sn",$1, $NF); } ' /etc/passwd root /bin/bash $ www.opengurukul.com 86
Linux MODULE : PROCESS www.opengurukul.com 87
Linux: Process: ps ps : report a snapshot of the current processes It displays PID(process id), TTY(terminal), and CMD(command) of existing processes. Example : $ ps PID TTY TIME CMD 17286 pts/0 00:00:00 bash $ www.opengurukul.com 88
Linux: Process: ps -e ps -e : to see all the processes Example : $ ps -e | head -3 PID TTY TIME CMD 1? 00:00:01 init 2? 00:00:00 kthreadd $ NOTE : www.opengurukul.com 89 -e is equivalent to -A.
Linux: Process: ps -f ps -f : does full-format listing It can be combined with any other option. It displays uid (user id), pid (process id), ppid (parent process id), c (nice value), stime (start time), tty, time executed and cmd. Example: $ ps -ef | head -2 UID PID PPID C STIME TTY TIME CMD root 1 0 0 09:12 ? 00:00:01 /sbin/init $ www.opengurukul.com 90
Linux: Process: jobs jobs : List active jobs If a programs takes more time during execution, it can be put into background by using & at the end in the command line. The process which is either running in the background or is in the stopped state is shown in the jobs. Example: $ sleep 60 & [1] 12721 $ jobs [1]+ Running sleep 60 & $ www.opengurukul.com 91
Linux: process: fg & bg fg : run job in the foreground To bring the process from background to foreground (terminal), use fg command. Example: $ fg %job_number bg : run job in the background To run a suspended job in the background use bg command. Example: $ bg %job_number www.opengurukul.com 92
Linux: process: ^Z & kill CONTROL+Z : to suspend a kill : send a signal to job process To stop a foreground process, Example: use CONTROL+Z. $ kill %job_number Example: $ sleep 60 NOTE: ^Z It will send SIGTERM(15) to job [1]+ Stopped sleep 60 number. $ jobs [1]+ Stopped sleep 60 $ www.opengurukul.com 93
Linux: Process: jobs example $ sleep 60 & $ jobs [1] 12721 [1]+ Stopped sleep 60 $ jobs $ bg %1 [1]+ Running sleep 60 & [1]+ sleep 60 & $ fg %1 $ jobs sleep 60 [1]+ Running sleep 60 & ^Z $ kill %1 [1]+ Stopped sleep 60 [1]+ Terminated sleep 60 $ $ jobs $ www.opengurukul.com 94
Linux: process: kill kill: signal a process To send a signal to a process, kill command is used. Example : $ kill -<signal_number> <PID> The signal number 9 is for SIGKILL. $ kill -9 $$ # will send SIGKILL(9) to your shell program $ kill <pid> # will send SIGTERM (15) to the pid. $ kill -l # to see list of signals NOTE : Signal SIGKILL cannot be masked. Used to kill the process. www.opengurukul.com 95
Linux: process: kill -l $ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN ....64) SIGRTMAX $ www.opengurukul.com 96
Linux : processes: nohup nohup : run a command immune to hangups, with output to a non-tty Syntax: nohup command NOTE: Sends the output to 'nohup.out' or '$HOME/nohup.out'. www.opengurukul.com 97
Linux MODULE : DISK www.opengurukul.com 98
Linux: disk: fdisk fdisk: Create & Edit Partition Table. Example:To print partition table $ fdisk /dev/sda Command (m for help): p Disk /dev/sda: 120.0 GB, 120034123776 bytes 255 heads, 63 sectors/track, 14593 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x3db20996 Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200781 83 Linux /dev/sda2 26 14593 117017460 8e Linux LVM Command (m for help): q www.opengurukul.com 99
Linux: disk : mkfs mkfs : build a file system To create file system on partition (logical volume) Example: $ mkfs /dev/hda2 $ /etc/fstab : file system table The file system table that maps partitions to be mounted and the directory on which it has to be mounted. Example: $ cat /etc/fstab # partition mount-directory fs-type mount-options dump fsck-order /dev/sda1 / ext4 defaults 1 1 www.opengurukul.com 100 $
Linux: disk : mount & unmount mount : mount a file system Mount file systems on partitions (logical volumes) to directories. Example: $ mount /dev/cdrom /mnt/cdimage $ umount : unmount a file system Unmount a file system from directory Example: $ umount <mounted dir name> # dirname on which fs has been mounted. $ umount /dev/cdrom # or umount /mnt/cdimage www.opengurukul.com 101
Linux: disk : du du : estimate file space usage To check disk used @ directory level. Options : -s summary (do not display for each directory) -h print sizes in human readable format (e.g., 1K 234M 2G) www.opengurukul.com 102
Linux: disk : du example $ du -sh /etc/rc.d 596K /etc/rc.d $ www.opengurukul.com 103
Linux: disk : df df : report file system disk space usage To check free space on a partition (logical volume) Options : -h in human readable format Usage : $ df # see all mounted file systems or $ df <mounted directory name> www.opengurukul.com 104
Linux: disk : df example $ df -h Filesystem Size Used Available Use% Mounted on /dev/mapper/VolGroup00-rootlv 77G 23G 51G 31% / /dev/sda1 190M 47M 134M 26% /boot /dev/mapper/VolGroup00-datalv 29G 22G 6.1G 78% /data $ $ df -h /home Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-rootlv 77G 23G 51G 31% / $ www.opengurukul.com 105
LINUX MODULE : NETWORK www.opengurukul.com 106
Linux: Network: ifconfig ifconfig : configures a network interface Used to configure a network interface. Also used to retrieve configuration of network interface like ip address of the Ethernet network interface 'eth0'. Option: -a show all interfaces Example: To know ip address of Ethernet network interface eth0. $ /sbin/ifconfig -a eth0 Link encap:Ethernet HWaddr 00:21:70:B1:3F:01 inet addr:192.168.1.35 Bcast:192.168.3.255 Mask:255.255.252.0 www.opengurukul.com 107 $
Linux: Network: ssh ssh : remote login program The ssh is a remote login program like telnet to operate on remote machine. Syntax: $ ssh -l login_name host_name Example: $ ssh -l matsya1 192.168.1.2 $ www.opengurukul.com 108
Linux: Network: ftp ftp: Internet file transfer program To get/put files from/to internet. Example : $ ftp <remote_hostname> Username : Password : ftp> cd <remote_directory_name> ftp> lcd <local_directory_name> ftp> pwd <remote directory name> ftp> ! pwd <local directory name> www.opengurukul.com 109
Linux: Network: ftp : example ftp> get file # to get single file from remote host to current box ftp> put file # to put single file from current box to remote box ftp> mget file* # to get files from remote host to current box ftp> mput file* # to put files from current box to remote box ftp> prompt # to disable prompt ftp> bi # binary mode ftp> ha # hash ftp> bye www.opengurukul.com 110
LINUX MODULE : COMMANDS www.opengurukul.com 111
Linux: Commands : locate locate : find files by name The locate reads one or more databases prepared by updatedb and writes file names matching at least one of the PATTERNs to standard output, one per line. Example : $ locate php.conf /etc/httpd/conf.d/php.conf $ NOTE: The database for locate is prepared by updatedb. www.opengurukul.com 112
Linux: Commands : updatedb.conf updatedb.conf : Do not index following file system types, names, paths. $ cat /etc/updatedb.conf PRUNE_BIND_MOUNTS = "yes" PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs" PRUNENAMES = ".git .hg .svn" PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/cache/ccache /var/spool/cups /var/spool/squid /var/tmp" $ www.opengurukul.com 113
Linux: Commands : find find – search for files in a directory hierarchy find <STARTING DIRECTORY> <OPTIONS> <EXPRESSION> Options : –name : search for files matching a pattern –type : search for files of a particular type ('f' is for regular file and 'd' is for directory) –exec : execute an action if the file is found Example : $ find /etc -name 'httpd.conf' -type f -exec ls -l {} ; -rw-r--r--. 1 root root 34417 Jul 9 2010 /etc/httpd/conf/httpd.conf $ www.opengurukul.com 114
Linux: Commands: uname uname -s : os name uname -n : node name $ uname -s $ uname -n Linux sachin-pc $ $ uname : os, host, kernel & arch information $ uname -a Linux sachin-pc 2.6.27.9-73.fc9.i686 #1 SMP Tue Dec 16 15:25:05 EST 2008 i686 i686 i386 GNU/Linux $ www.opengurukul.com 115
Linux: Commands: su su - run a shell with substitute user and group IDs This command is used to switch user. To become a super user, su can be used without arguments or (su root) www.opengurukul.com 116
Linux: Commands: su : Example $ id uid=500(surikuma) gid=500(surikuma) groups=500(surikuma),501(dba) $ su # without any argument, ask for root password Password: # id uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),501 (dba) # su surikuma # root can switch to any user $ id uid=500(surikuma) gid=500(surikuma) groups=500(surikuma),501(dba) $ www.opengurukul.com 117
Linux: Commands: date date - print or set the system date and time Syntax : Get Date $ date Sat Sep 19 22:43:14 IST 2009 $ Syntax : Set Date $ date [MMDDhhmm[[CC]YY][.ss] www.opengurukul.com 118
Linux: Commands: cal cal : displays calendar The cal displays calendar for current month without any args Options: -1 : displays calendar for current month -3 : displays calendar for previous/current/next month -y : displays calendar for current year www.opengurukul.com 119
Linux: Commands: cal example Example : $ cal October 2011 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 www.opengurukul.com 120
UNIX MODULE : Fedora & OpenSuse : .rpm packages : rpm and yum www.opengurukul.com 121
Linux: rpm : -e & -i rpm -i : install package $ rpm -i <package_name.rpm> $ rpm -e : erase/delete package $ rpm -e <package_name> $ www.opengurukul.com 122
Linux: rpm: package contents rpm -q -a : list all packages installed on systems $ rpm -qa | grep -e '^httpd' httpd-tools-2.2.16-1.fc14.i686 httpd-2.2.16-1.fc14.i686 httpd-manual-2.2.16-1.fc14.noarch $ www.opengurukul.com 123
Linux: rpm -q -list -p rpm -q –list : list files included in an installed rpm package $ rpm -q –list <package_name> $ rpm -q --list php /etc/httpd/conf.d/php.conf /usr/lib/httpd/modules/libphp5.so /var/lib/php/session /var/www/icons/php.gif $ rpm -q –list -p : list files included in an rpm package $ rpm -q –list -p <package_location> $ rpm -q --list -p /var/cache/yum/updates-newkey/packages/*.rpm www.opengurukul.com 124 $
Linux: rpm: -q -f rpm -q -f <filename> : Find the Package that Contains a file. $ rpm -qf /etc/issue fedora-release-14-1.noarch $ www.opengurukul.com 125
Linux: yum yum : Install/update packages directly from internet You must be a super user to install package directly from internet. You can create/update repositories information in /etc/yum/repos.d/ www.opengurukul.com 126
UNIX MODULE : Debin / Ubuntu LINUX : .deb packages : dpkg and apt-get www.opengurukul.com 127
Linux: dpkg : -i & -r dpkg -i : install package $ dpkg -i <package_name.deb> $ dpkg -r : remove package $ dpkg -r <package_name> $ www.opengurukul.com 128
Linux: dpkg : package contents dpkg -l : list all packages installed on systems $ dpkg -l | grep -e 'apache' $ The second filed has the package name. www.opengurukul.com 129
Linux: dpkg -c & -L dpkg -L : list files included in an installed package $ dpkg -L <package_name> $ dpkg -L php /etc/httpd/conf.d/php.conf /usr/lib/httpd/modules/libphp5.so /var/lib/php/session /var/www/icons/php.gif $ dpkg -c <.deb pkg name>: list files included in a deb package $ dpkg -c <deb_package_location> $ www.opengurukul.com 130
Linux: dpkg : -S dpkg -S <filename> : Find the Package that Contains a file. $ dpkg -S /bin/ls coreutils: /bin/ls $ www.opengurukul.com 131
Linux: apt-get apt-get : Install/update packages directly from internet You must be a super user to install package directly from internet. You can create/update repositories information in /etc/apt/sources.list www.opengurukul.com 132
Linux : apt-get : list, update, install, remove yum list: list packages apt-get install: install package $ yum list <package_name>* $ apt-get install <package_name> $ $ To list packages matching bluefish To install bluefish editor $ yum list *bluefish* $ apt-get install bluefish apt-get update: update package apt-get remove: remove package $ apt-get update <package_name> $ apt-get remove <package_name> $ apt-get update bluefish $ To remove bluefish editor $ apt-get erase bluefish www.opengurukul.com 133
UNIX MODULE : LINUX www.opengurukul.com 134
Linux : Distribution Info /etc/*release - distribution name & version $ ls /etc/*release /etc/fedora-release /etc/redhat-release /etc/system-release $ cat /etc/*release Fedora release 14 (Laughlin) Fedora release 14 (Laughlin) Fedora release 14 (Laughlin) $ www.opengurukul.com 135
Linux : LSB Distribution Info lsb_release – Distribution Information using LSB $ lsb_release -a LSB Version: :core-4.0-ia32:core-4.0-noarch Distributor ID: Fedora Description:Fedora release 14 (Laughlin) Release: 14 Codename: Laughlin $ www.opengurukul.com 136
Linux: Services Service : status, start, stop, restart $ service <service_name> status | start | stop | restart Example: $ service httpd status # status of apache web server # service httpd start # start apache web server # service httpd restart # restart apache web server # service httpd stop # stop apache web server www.opengurukul.com 137
Linux: gzip & gunzip gunzip/gzip : GNU unzip/zip To zip a file using GZIP. $ gzip x.tar (creates x.tar.gz) $ To unzip a file using GUNZIP. $ gunzip x.tar.gz (creates uncompressed x.tar file) $ www.opengurukul.com 138
Linux: bzip2 & bunzip2 bzip2/bunzip2 - block-sorting file compressor The block-sorting file compressor - better than GNU zip/unzip. bzip2: To zip a file $ bzip2 x.tar (creates x.tar.bz2) $ bunzip2 : To unzip a file $ bunzip2 x.tar.bz2 (creates uncompressed x.tar file) www.opengurukul.com 139 $
Linux: tar tar : tape archive commands The tape archive is used to combine multiple files and directories into one file. Options : -v verbose mode -f target file -c create tar -t list toc of tar -x extract files from tar www.opengurukul.com 140
Linux: tar : -c tar -c : create a tar file Example : Syntax : $ cd /tmp tar -c -v -f <tar file> <list of $ tar -cvf team.tar team files> $ tar -cvf /tmp/team.tar team Setup : team/ $ cd /tmp team/india/ $ mkdir -p team/india team/india/sachin $ mkdir -p team/australia team/australia/ $ touch team/india/sachin team/australia/ricky $ touch team/australia/ricky $ www.opengurukul.com 141
Linux: tar : -t tar -t : list table of contents Syntax : tar -t -v -f <tar_file> Example : $ tar -t -v -f /tmp/team.tar drwxrwxr-x sachin/sachin 0 2011-10-03 23:47 team/ drwxrwxr-x sachin/sachin 0 2011-10-03 23:47 team/india/ -rw-rw-r-- sachin/sachin 0 2011-10-03 23:47 team/india/sachin drwxrwxr-x sachin/sachin 0 2011-10-03 23:47 team/australia/ -rw-rw-r-- sachin/sachin 0 2011-10-03 23:47 team/australia/ricky www.opengurukul.com 142 $
Linux: tar : -x tar -x : extract files from a tar Example : file $ cd /tmp To extract all the files $ mkdir -p new; cd new $ tar -xvf <tar file> $ tar -xvf /tmp/team.tar $ team/ To extract a particular file/dir team/india/ $ tar -xvf <tar file> <file path> team/india/sachin $ team/australia/ team/australia/ricky $ www.opengurukul.com 143
Linux: install s/w from sources Steps : Steps : Download .tar.bz2 file or .tar.gz. $ bunzip2 software.tar.bz2 Unzip and Extract $ tar -xvf software.tar Configure $ cd software Create Makefile $ ./configure Run Make $ make clean Install using Make $ make all $ make install www.opengurukul.com 144
Linux: Support Please register yourself @ www.opengurukul.com In case you need any support in future. www.opengurukul.com 145

OpenGurukul : Operating System : Linux

  • 1.
    LINUX By OpenGurukul.com Free/Open Source Software Laboratory
  • 2.
    LINUX Module : History www.opengurukul.com 2
  • 3.
    Unix : History UNIXVariants IBM AIX Oracle/Sun Solaris HP HP-UX www.opengurukul.com 3
  • 4.
    Unix : History LINUX(Unix like operating system) Distributions Redhat Fedora (Free Edition), RHEL (Redhat Enterprise Linux) Novell SLED(Suse Linux Enterprise Desktop), SLES (Suse Linux Enterprise Server) Canonical Ubuntu (derived from Debian) Oracle Enterprise Linux (derived from RHEL) CentOS (derived from RHEL) www.opengurukul.com 4
  • 5.
    LINUX Module : FileSystems www.opengurukul.com 5
  • 6.
    Linux : FileSystems Computers store information in things called files. A file is like a sheet of paper with something writ- ten on it. Files are stored in things called directories (popu- larly known as folders on windows). www.opengurukul.com 6
  • 7.
    Linux : FileSystems : pwd pwd : print name of current/working directory Usage: $ pwd Example: $ pwd /home/sachin $ echo $HOME /home/sachin www.opengurukul.com 7 $
  • 8.
    Linux : FileSystems : cd cd : change directory Used to change current working directory Usage: $ cd <dirname> Example: $ cd /tmp $ pwd /tmp $ www.opengurukul.com 8
  • 9.
    Linux : FileSystems : cd to home directory cd ~ : cd to home directory cd : cd without arguments to go to home directory Example : Example : $ cd ~ ; pwd $ cd ; pwd /home/sachin /home/sachin $ $ www.opengurukul.com 9
  • 10.
    Linux : FileSystems : cd cd .. : cd to parent directory cd - : cd to previous working directory Example : Example: $ cd ~ ; pwd $ cd /z/y/x/w/v/u; pwd /home/sachin /z/y/x/w/v/u $ cd .. ; pwd $ cd /a/b/c/d/e/f; pwd /home /a/b/c/d/e/f $ $ cd -; pwd /z/y/x/w/v/u www.opengurukul.com 10 $
  • 11.
    Linux : FileSystems : mkdir mkdir : make directories Example : $ cd $HOME Options : $ mkdir lab -p : no error if it already $ mkdir lab exists, make parent mkdir: cannot create directory directories as needed `lab': File exists $ mkdir -p lab Usage : $ mkdir -p lab/scratch $mkdir <dirname> www.opengurukul.com 11
  • 12.
    Linux : FileSystems : cat cat – concatenate files and print on the standard output Usage: $ cat <list of files> Example: $ cat /etc/issue Fedora release 14 (Laughlin) Kernel r on an m (l) $ www.opengurukul.com 12
  • 13.
    Linux : FileSystems : head head – output the first part of files The head command without any argument displays first 10 lines of a file. Usage: $ head <filename> Example: To display first two lines $ head -2 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin $ www.opengurukul.com 13
  • 14.
    Linux : FileSystems : tail tail – output the last part of files The tail command without any argument displays last 10 lines of a file. Usage: $ tail <filename> Example: To display last two lines $ tail -2 /etc/passwd oracle:x:501:501::/usr/lib/oracle/xe:/bin/bash tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat6:/sbin/nologin $ www.opengurukul.com 14
  • 15.
    Linux : FileSystems : more more : paging through text one screenful at a time. The command also shows % of text already covered at bottom. Usage: $ more <filename> Commands: To quit – press q To go to next page – press space bar To go to prev page for files (not for pipes) – press b Example: $ more /etc/httpd/conf/httpd.conf www.opengurukul.com 15 $
  • 16.
    Linux : FileSystems : less less : similar to more. Less allows backward movement in the file as well as forward movement Usage: $ less <filename> Commands: To quit – press q To go to prev/next page – press page up/down Example: $ less /etc/httpd/conf/httpd.conf www.opengurukul.com 16 $
  • 17.
    Linux : FileSystems : touch touch – create a zero size file / change time stamp Creates a zero size file if the file doesn't exist. Change modification time if the file already exists. Usage: $ touch <filename> Example: $ cd ~/lab/scratch $ touch touch.txt $ cat touch.txt $ www.opengurukul.com 17
  • 18.
    Linux : FileSystems : echo echo – display text to standard output Usage: $ echo <text> Example: $ cd ~/lab/scratch $ echo “Hello World” Hello World $ www.opengurukul.com 18
  • 19.
    Linux : FileSystems : ls ls - list directory contents Options: -a show entries starting with dot (.) and dot dot (..) -l show long listing format -t sort by modification time -r reverse the order Example: $ cd /home $ ls sachin surikuma root ksurinde laxmi yukta kumasuri sunil saurav $ www.opengurukul.com 19
  • 20.
    Linux : FileSystems : ls -a ls -a : do not ignore entries starting with dot Example : $ cd ~/lab/scratch $ touch .system.txt $ ls echo.txt touch.txt $ ls -a .system.txt echo.txt touch.txt $ www.opengurukul.com 20
  • 21.
    Linux : FileSystems : ls -l ls -l : use a long listing format <file type> <permissions> <link count> <file owner> <group of owner> <file size> <date of modification> <time of modification> <file name> Example : $ cd /bin $ ls -l *sh* -rwxr-xr-x. 1 root root 877480 Jun 22 2010 bash -rwxr-xr-x. 1 root root 102408 Jun 21 2010 dash lrwxrwxrwx. 1 root root 4 Jan 27 2011 sh -> bash -rwxr-xr-x 1 root root 597768 Mar 22 2010 zsh www.opengurukul.com 21 $
  • 22.
    Linux : FileSystems : ls -t ls -t : sort by modification time Example : $ cd /bin $ ls -lt *sh* lrwxrwxrwx. 1 root root 4 Jan 27 2011 sh -> bash -rwxr-xr-x. 1 root root 877480 Jun 22 2010 bash -rwxr-xr-x. 1 root root 102408 Jun 21 2010 dash -rwxr-xr-x 1 root root 597768 Mar 22 2010 zsh $ www.opengurukul.com 22
  • 23.
    Linux : FileSystems : ls -r ls -r : reverse order while sorting Example : $ cd /bin $ ls -ltr *sh* -rwxr-xr-x 1 root root 597768 Mar 22 2010 zsh -rwxr-xr-x. 1 root root 102408 Jun 21 2010 dash -rwxr-xr-x. 1 root root 877480 Jun 22 2010 bash lrwxrwxrwx. 1 root root 4 Jan 27 2011 sh -> bash $ www.opengurukul.com 23
  • 24.
    Linux : FileSystems : ln ln : create hard/soft links between files Soft Link (Symbolic Link): The symbolic link contain path of the target link. The symbolic link can cross boundaries of file systems. Hard Link: Hard links are supported within a file system and these are used to refer to an existing content by some other name within the same file system. The inode number will be same for hard links. Most of the file systems do not support hard linking directories. www.opengurukul.com 24
  • 25.
    Linux : FileSystems : ln & ln -s Usage: Example : $ ln -s <old path> <new_name> Symbolic Link (ln -s): -s : create a symbolic link $ cd /bin without -s : creates a hard link $ ln -s bash sh $ NOTE : On AIX, /bin/sh is a hard linked Hard Link (ln): to /bin/ksh. $ cd /bin On Linux, /bin/sh is a soft link $ ln ksh sh to /bin/bash. $ www.opengurukul.com 25
  • 26.
    Linux : FileSystems : soft link Example : $ ls -l /bin | grep -e ^l | grep -e vi lrwxrwxrwx. 1 root root 2 Jan 27 2011 ex -> vi lrwxrwxrwx. 1 root root 2 Jan 27 2011 rvi -> vi lrwxrwxrwx. 1 root root 2 Jan 27 2011 rview -> vi lrwxrwxrwx. 1 root root 2 Jan 27 2011 view -> vi $ www.opengurukul.com 26
  • 27.
    Linux : FileSystems : cp cp: copy files and directories SYNOPSIS : cp SOURCE DEST cp SOURCE... DIRECTORY Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY Option : -R copy directories recursively (otherwise directories will be omitted) www.opengurukul.com 27
  • 28.
    Linux : FileSystems : cp cp: copy files and directories Example : $ cp ~/.bashrc ~/.bashrc.orig $ cp /etc/profile.d ~/ cp: omitting directory `/etc/profile.d' $ cp -R /etc/profile.d ~/ $ www.opengurukul.com 28
  • 29.
    Linux : FileSystems : mv mv: move (rename) files SYNOPSIS : mv [OPTION]... SOURCE DEST mv [OPTION]... SOURCE... DIRECTORY DESCRIPTION Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY Example: $ cp ~/.bashrc /tmp $ mv /tmp/.bashrc /tmp/.bashrc.new $ ls /tmp/.bashrc* /tmp/.bashrc.new www.opengurukul.com 29 $
  • 30.
    Linux : FileSystems : rm rm : remove files or directories SYNOPSIS: rm [OPTION]... FILE... Options: -i : interactive (prompt before every removal) -f : ignore nonexistent files, never prompt (force) -r : remove directories and their contents recursively www.opengurukul.com 30
  • 31.
    Linux : FileSystems : rm example Example (rm) : $ cd /tmp $ touch rm1.out $ rm rm1.out $ ls rm1.out ls: cannot access rm1.out: No such file or directory $ www.opengurukul.com 31
  • 32.
    Linux : FileSystems : rm -i Example (rm -i) : $ cd /tmp $ touch rm2.out $ rm -i rm2.out rm: remove regular file 'rm2.out'? Y $ ls rm2.out ls: cannot access rm2.out: No such file or directory $ www.opengurukul.com 32
  • 33.
    Linux : FileSystems : rm -f Example (rm -f) : $ rm non_existent_file.txt rm: cannot remove `non_existent_file.txt': No such file or directory $ rm -f non_existent_file.txt $ www.opengurukul.com 33
  • 34.
    Linux : FileSystems : rm -r Example (rm -r) : $ cd /tmp $ mkdir scratch $ rm scratch rm: cannot remove `scratch/': Is a directory $ rm -rf scratch $ www.opengurukul.com 34
  • 35.
    LINUX MODULE : VI– Text Editor www.opengurukul.com 35
  • 36.
    LINUX: VI Editor Toinvoke Type vi or 'vi filename'. To come out of vi editor Use ESC (get into command mode), followed by : (ex editor mode), followed by q (quit) and then press ENTER. $ vi Hi I am Open Gurukul :q $ www.opengurukul.com 36
  • 37.
    LINUX: VI Editor: Mode The VI editor run in 3 different modes INSERT mode COMMAND mode ED/EX Editor mode www.opengurukul.com 37
  • 38.
    LINUX: VI Editor: Command Mode Command mode Use ESC to go to COMMAND mode anytime. The typed character will not be visible in COMMAND mode. Under COMMAND mode, each character has special significance and the designated action takes place. www.opengurukul.com 38
  • 39.
    LINUX: VI Editor: INSERT Mode INSERT mode The characters typed are visible when the editor is in INSERT mode. To get into INSERT mode from command mode i – insert shift+a – insert at end of line www.opengurukul.com 39
  • 40.
    LINUX: VI Editor: EX Mode ED/EX Editor mode The ED/EX editor commands are typed after typing ':' in command mode. From INSERT mode, we can use ESC + ':' to get into ED/EX mode. www.opengurukul.com 40
  • 41.
    LINUX: VI Editor: Commands INSERT i – insert shift+a – append at end of line DELETE x – delete a single character dw – delete a word dd – delete a line www.opengurukul.com 41
  • 42.
    LINUX: VI Editor: Commands Undo / Redo u – undo the last commands action . (dot) – redo the last commands action Repeat Action <repeat_count>command 5dd – deletes 5 lines www.opengurukul.com 42
  • 43.
    LINUX: VI Editor: Cursor Movement Cursor Movement Commands /| h – move left on the current line | j – move down one line | k – move up one line <-- -- -- h j k l -- -- --> l - move right on the current line | | !/ www.opengurukul.com 43
  • 44.
    LINUX: VI Editor:Ex :Save & Quit :w write contents to a file :w /tmp/1.out write contents to file /tmp/1.out :q quit (will not work if there were changes after last write) :wq write and quit :q! quit (ignore recent modifications) www.opengurukul.com 44
  • 45.
    LINUX: VI Editor: Copy & Paste yy – yank line into buffer <number_of_lines>yy – yank multiples lines into buffer (2yy – yank 2 lines into buffer) Go to line where the content of buffer has to be pasted. Type p to print contents of buffer www.opengurukul.com 45
  • 46.
    LINUX: VI Editor:Search Pattern /pattern To goto a line that matches pattern. ?pattern To go to a line that matches pattern in the reverse direction. n Go to next such pattern match. N (shift+n) Go to next such pattern match in reverse direction. :set ic www.opengurukul.com 46 To ignore case while searching, set ic.
  • 47.
    LINUX: VI Editor:Line Numbers Display Line Numbers :set nu # To display line numbers Do not Display Line Numbers :set nonu # To stop displaying line numbers To goto a particular line :1 # first line in the file :$ # last line in the file :line_num # Goto line number line_num www.opengurukul.com 47
  • 48.
    LINUX: VI Editor:Search & Replace To replace old pattern with the new pattern in entire file. :1,$s/old/new/g replace old with new in entire file from first line to last line. :.,+50s/old/new/g replace old with new in file from current line (.) to next 50 lines www.opengurukul.com 48
  • 49.
    LINUX: VI Editor:Editor Options To see options that are currently set. :set To see all the options :set all www.opengurukul.com 49
  • 50.
    LINUX: VI Editor:Multiple Files To operate on multiple files, specify all of them while invoking vi editor. $ vi /tmp/1.txt /tmp/2.txt :files # to see files opened by vi editor :n # to go to next files :rew # to rewind to the first file $ www.opengurukul.com 50
  • 51.
    LINUX Module : Environment www.opengurukul.com 51
  • 52.
    Linux : Environment: printenv printenv : print all or part of environment Usage: printenv Example: $ printenv | grep -e HOME -e PWD -e SHELL -e USER -e HOSTNAME HOSTNAME=sachin-pc SHELL=/bin/bash OLDPWD=/tmp USER=sachin PWD=/home/sachin/lab/linux/scratch HOME=/home/sachin $ www.opengurukul.com 52
  • 53.
    Linux : Environment: export export: exports an environment variable To set an environment variable so that it is visible to even child processes. Example : $ export MY_NAME=sachin1 $ echo $MY_NAME To set an environment variable $ MY_NAME=sachin1 (It will not be visible to child processes.) $ echo $MY_NAME www.opengurukul.com 53
  • 54.
    LINUX : Environment: PS1 PS1 : stores value of primary prompt Example : $ export PS1='c: > ' # change the prompt c: > export PS1='$' # revert the prompt $ www.opengurukul.com 54
  • 55.
    LINUX : Environment: PS2 PS2 Stores value of secondary prompt. Used when the command line is incomplete. Default secondary prompt is '>'. Example : $ echo 'hello >' $ www.opengurukul.com 55
  • 56.
    Linux : Environment:HOME and PATH HOME : stores home directory of user. $ echo $HOME /home/sachin $ PATH : stores list of directories that should be searched for the executable if the absolute/relative path of an executable has not been specified. $ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin $ www.opengurukul.com 56
  • 57.
    Linux : Environment:PWD & OLDPWD PWD : has value of current working directory. $ cd /tmp $ echo $PWD /tmp $ OLDPWD : has value of last working directory $ cd /var; cd /tmp $ echo $OLDPWD /var $ NOTE : The value of OLDPWD is used by command “cd -” to go back to last working www.opengurukul.com 57 directory
  • 58.
    Linux : Environment:SHELL, HOSTNAME, USER SHELL : To know path of SHELL program. $ echo $SHELL /bin/bash $ HOSTNAME : To know host name $ echo $HOSTNAME sachin-pc $ USER : Name of User logged in. $ echo $USER sachin www.opengurukul.com 58 $
  • 59.
    Linux: Environment: BASHFiles /etc/profile : system wide initialization file It is executed for login shells. $HOME/.bash_profile : personal initialization file It is executed for login shells. $HOME/.bash_logout : individual login shell cleanup file It is executed when a login shell exits. $HOME/.bash_history : command history list file It stores commands executed by us. www.opengurukul.com 59
  • 60.
    Linux: Environment: History history: display the command history list with line numbers The history command displays the command history list with line numbers. $ history 1037 cd /var; cd /tmp; 1038 echo $OLDPWD $ Execute a command from history : $ !1037 # to execute 1037th command. HISTSIZE : number of commands to be stored in history file $ echo $HISTSIZE 1000 $ www.opengurukul.com 60
  • 61.
    Linux Module : FILEACCESS www.opengurukul.com 61
  • 62.
    Linux: File Access:whoami whoami : print effective user id $ whoami sachin $ groups : print the groups a user is in $ groups sachin $ www.opengurukul.com 62
  • 63.
    Linux: File Access:id & chmod id : print user identity $ id uid=500(surikuma) gid=500(surikuma) groups=500(surikuma) $ chmod : change file mod bits www.opengurukul.com 63
  • 64.
    Linux: File Access:file type <filetype> <rwx perm for owner> <rwx perm for group> <rwx perm for others> Regular file : '-' Directory : 'd' Pipes : 'p' Character device special files : 'c' Block device special files : 'b' For symbolic links : 'l' For sockets : 's' www.opengurukul.com 64
  • 65.
    Linux: File Access:chmod User / Group / Other Read / Write / Execute r(read) u (user) w(write) g (group) x(execute) o (other) Octal Numbers for R / W / X 4 – read 2 – write 1 - execute (file) / search (directory) www.opengurukul.com 65
  • 66.
    Linux: File Access:chmod Permissions Calculations Octal Number RWX 4+2+1 7 RW- 4+2 6 R-X 4+1 5 R-- 4 4 -WX 2+1 3 -W- 2 2 --X 1 1 www.opengurukul.com 66
  • 67.
    Linux: File Access:chmod To give all the permissions to everybody on a file/directory $ chmod 777 path $ To give execute permission on a file $ chmod +x path $ To give execute permission on a file to user alone $ chmod u+x path www.opengurukul.com 67 $
  • 68.
    Linux: File Access:umask umask : unix mask $ umask 022 $ Initial permission for new file : 666 - umask new directory : 777 - umask NOTE : On Linux, umask is 002 in case user name and group name are same. www.opengurukul.com 68
  • 69.
    Linux: File Access:chown chown : change file owner & group The -R option is used to change the ownership recursively. $ chown -R <username>:<group name> <path> www.opengurukul.com 69
  • 70.
    Linux MODULE : Filters (wc, grep, sed, awk, sort) www.opengurukul.com 70
  • 71.
    Linux: Filters: wc wc: print newline, word, and byte counts for each file $ wc /etc/passwd 56 98 2916 /etc/passwd $ wc -l : print the newline counts $ wc -l /etc/passwd 56 /etc/passwd $ NOTE: The wc stands for wordcount. www.opengurukul.com 71
  • 72.
    Linux: Filters :wc wc -w : print the word counts $ wc -w /etc/passwd 98 /etc/passwd $ wc -c : print the byte counts $ wc -c /etc/passwd 2916 /etc/passwd $ www.opengurukul.com 72
  • 73.
    Linux: Filters: grep grep: print lines matching a pattern Syntax : grep [OPTIONS] PATTERN [FILE...] Example : $ grep root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin $ www.opengurukul.com 73
  • 74.
    Linux: Filters: grep-e & -E grep -e PATTERN : use PATTERN for search $ grep -e sync -e shutdown /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown $ grep -E PATTERN : egrep - use extended PATTERN for search $ grep -E 'sync|halt' /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync halt:x:7:0:halt:/sbin:/sbin/halt www.opengurukul.com 74 $
  • 75.
    Linux: Filters: grep-f grep -f file : patterns are kept in file $ cat /tmp/patterns.txt sync halt $ grep -f /tmp/patterns.txt /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync halt:x:7:0:halt:/sbin:/sbin/halt $ www.opengurukul.com 75
  • 76.
    Linux: Filters: grep-v grep -v : Invert the sense of matching, to select non- matching lines $ cat /etc/passwd | grep -v nologin | grep -v bash sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt jetty:x:490:481::/usr/share/jetty:/bin/sh $ www.opengurukul.com 76
  • 77.
    Linux: Filters: grep-n grep -n : Prefix each line of output with the line number within its input file $ grep -n -e halt -e sync -e shutdown /etc/passwd 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt $ www.opengurukul.com 77
  • 78.
    Linux: Filters: grep-i grep -i : Ignore case distinctions in both the PATTERN and the input files $ grep ROOT /etc/passwd $ grep -i ROOT /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin $ www.opengurukul.com 78
  • 79.
    Linux: Filters: sort sort: sort lines of text Example : files $ sort lamp.txt $ cat lamp.txt lang: php, perl, python os: linux os: linux webserver: apache rdbms: mysql rdbms: mysql webserver: apache lang: php, perl, python $ $ www.opengurukul.com 79
  • 80.
    Linux: Filters: sort-n sort -n : compare $ sort sort.txt according to string 10 numerical value 2 $ $ cat sort.txt $ sort -n sort.txt 2 2 10 $ 10 $ www.opengurukul.com 80
  • 81.
    Linux: Filters: sort-u sort -u : unique records $ sort sort_u.txt The uniq command also hello select unique adjacent lines. hello The uniq command is also dependent on sort command HELLO to sort the records. HELLO $ cat hello.txt $ HELLO hello $ sort -u sort_u.txt HELLO hello hello HELLO $ $ www.opengurukul.com 81
  • 82.
    Linux: Filters: sort-r sort -r : reverse the result Example : of comparisons $ sort -r lamp.txt $ cat lamp.txt webserver: apache os: linux rdbms: mysql webserver: apache os: linux rdbms: mysql lang: php, perl, python lang: php, perl, python $ $ www.opengurukul.com 82
  • 83.
    Linux: Filters: sed sed– stream editor used for search and replace operation on a file. Syntax : sed -e 's/old pattern/new word/gi' file /g is to replace globally. Multiple patterns on the same line will be replaced. /i is to ignore care while matching pattern www.opengurukul.com 83
  • 84.
    Linux: Filters: sed-e Example: Example: $ sed -e 's/mysql/oracle/gi' $ sed -e 's/mysql/oracle/gi' lamp.txt -e 's/php/perl/gi' lamp.txt os: linux os: linux lang: php, perl, python lang: perl, perl, python rdbms: oracle rdbms: oracle webserver: apache webserver: apache $ $ www.opengurukul.com 84
  • 85.
    Linux: Filters: awk awk- pattern scanning and AWK Variables : processing language $0 – current record awk is mainly used for $1 – first field of current selecting fields from the record output. $2 – second field of current Syntax : record awk -Ffs ' /pattern/ {action}' ... Option : $NF – last field of C.R. -F is used to specify field NF – number of fields in C. separator (default is white R. space) NR – number of records read www.opengurukul.com 85 so far
  • 86.
    Linux: Filters: awkexample Example : To display first and last entry for root user. $ awk -F: ' /^root/ { printf("%s %sn",$1, $NF); } ' /etc/passwd root /bin/bash $ www.opengurukul.com 86
  • 87.
    Linux MODULE : PROCESS www.opengurukul.com 87
  • 88.
    Linux: Process: ps ps: report a snapshot of the current processes It displays PID(process id), TTY(terminal), and CMD(command) of existing processes. Example : $ ps PID TTY TIME CMD 17286 pts/0 00:00:00 bash $ www.opengurukul.com 88
  • 89.
    Linux: Process: ps-e ps -e : to see all the processes Example : $ ps -e | head -3 PID TTY TIME CMD 1? 00:00:01 init 2? 00:00:00 kthreadd $ NOTE : www.opengurukul.com 89 -e is equivalent to -A.
  • 90.
    Linux: Process: ps-f ps -f : does full-format listing It can be combined with any other option. It displays uid (user id), pid (process id), ppid (parent process id), c (nice value), stime (start time), tty, time executed and cmd. Example: $ ps -ef | head -2 UID PID PPID C STIME TTY TIME CMD root 1 0 0 09:12 ? 00:00:01 /sbin/init $ www.opengurukul.com 90
  • 91.
    Linux: Process: jobs jobs: List active jobs If a programs takes more time during execution, it can be put into background by using & at the end in the command line. The process which is either running in the background or is in the stopped state is shown in the jobs. Example: $ sleep 60 & [1] 12721 $ jobs [1]+ Running sleep 60 & $ www.opengurukul.com 91
  • 92.
    Linux: process: fg& bg fg : run job in the foreground To bring the process from background to foreground (terminal), use fg command. Example: $ fg %job_number bg : run job in the background To run a suspended job in the background use bg command. Example: $ bg %job_number www.opengurukul.com 92
  • 93.
    Linux: process: ^Z& kill CONTROL+Z : to suspend a kill : send a signal to job process To stop a foreground process, Example: use CONTROL+Z. $ kill %job_number Example: $ sleep 60 NOTE: ^Z It will send SIGTERM(15) to job [1]+ Stopped sleep 60 number. $ jobs [1]+ Stopped sleep 60 $ www.opengurukul.com 93
  • 94.
    Linux: Process: jobsexample $ sleep 60 & $ jobs [1] 12721 [1]+ Stopped sleep 60 $ jobs $ bg %1 [1]+ Running sleep 60 & [1]+ sleep 60 & $ fg %1 $ jobs sleep 60 [1]+ Running sleep 60 & ^Z $ kill %1 [1]+ Stopped sleep 60 [1]+ Terminated sleep 60 $ $ jobs $ www.opengurukul.com 94
  • 95.
    Linux: process: kill kill:signal a process To send a signal to a process, kill command is used. Example : $ kill -<signal_number> <PID> The signal number 9 is for SIGKILL. $ kill -9 $$ # will send SIGKILL(9) to your shell program $ kill <pid> # will send SIGTERM (15) to the pid. $ kill -l # to see list of signals NOTE : Signal SIGKILL cannot be masked. Used to kill the process. www.opengurukul.com 95
  • 96.
    Linux: process: kill-l $ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN ....64) SIGRTMAX $ www.opengurukul.com 96
  • 97.
    Linux : processes:nohup nohup : run a command immune to hangups, with output to a non-tty Syntax: nohup command NOTE: Sends the output to 'nohup.out' or '$HOME/nohup.out'. www.opengurukul.com 97
  • 98.
    Linux MODULE : DISK www.opengurukul.com 98
  • 99.
    Linux: disk: fdisk fdisk:Create & Edit Partition Table. Example:To print partition table $ fdisk /dev/sda Command (m for help): p Disk /dev/sda: 120.0 GB, 120034123776 bytes 255 heads, 63 sectors/track, 14593 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x3db20996 Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200781 83 Linux /dev/sda2 26 14593 117017460 8e Linux LVM Command (m for help): q www.opengurukul.com 99
  • 100.
    Linux: disk :mkfs mkfs : build a file system To create file system on partition (logical volume) Example: $ mkfs /dev/hda2 $ /etc/fstab : file system table The file system table that maps partitions to be mounted and the directory on which it has to be mounted. Example: $ cat /etc/fstab # partition mount-directory fs-type mount-options dump fsck-order /dev/sda1 / ext4 defaults 1 1 www.opengurukul.com 100 $
  • 101.
    Linux: disk :mount & unmount mount : mount a file system Mount file systems on partitions (logical volumes) to directories. Example: $ mount /dev/cdrom /mnt/cdimage $ umount : unmount a file system Unmount a file system from directory Example: $ umount <mounted dir name> # dirname on which fs has been mounted. $ umount /dev/cdrom # or umount /mnt/cdimage www.opengurukul.com 101
  • 102.
    Linux: disk :du du : estimate file space usage To check disk used @ directory level. Options : -s summary (do not display for each directory) -h print sizes in human readable format (e.g., 1K 234M 2G) www.opengurukul.com 102
  • 103.
    Linux: disk :du example $ du -sh /etc/rc.d 596K /etc/rc.d $ www.opengurukul.com 103
  • 104.
    Linux: disk :df df : report file system disk space usage To check free space on a partition (logical volume) Options : -h in human readable format Usage : $ df # see all mounted file systems or $ df <mounted directory name> www.opengurukul.com 104
  • 105.
    Linux: disk :df example $ df -h Filesystem Size Used Available Use% Mounted on /dev/mapper/VolGroup00-rootlv 77G 23G 51G 31% / /dev/sda1 190M 47M 134M 26% /boot /dev/mapper/VolGroup00-datalv 29G 22G 6.1G 78% /data $ $ df -h /home Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-rootlv 77G 23G 51G 31% / $ www.opengurukul.com 105
  • 106.
    LINUX MODULE : NETWORK www.opengurukul.com 106
  • 107.
    Linux: Network: ifconfig ifconfig: configures a network interface Used to configure a network interface. Also used to retrieve configuration of network interface like ip address of the Ethernet network interface 'eth0'. Option: -a show all interfaces Example: To know ip address of Ethernet network interface eth0. $ /sbin/ifconfig -a eth0 Link encap:Ethernet HWaddr 00:21:70:B1:3F:01 inet addr:192.168.1.35 Bcast:192.168.3.255 Mask:255.255.252.0 www.opengurukul.com 107 $
  • 108.
    Linux: Network: ssh ssh: remote login program The ssh is a remote login program like telnet to operate on remote machine. Syntax: $ ssh -l login_name host_name Example: $ ssh -l matsya1 192.168.1.2 $ www.opengurukul.com 108
  • 109.
    Linux: Network: ftp ftp:Internet file transfer program To get/put files from/to internet. Example : $ ftp <remote_hostname> Username : Password : ftp> cd <remote_directory_name> ftp> lcd <local_directory_name> ftp> pwd <remote directory name> ftp> ! pwd <local directory name> www.opengurukul.com 109
  • 110.
    Linux: Network: ftp: example ftp> get file # to get single file from remote host to current box ftp> put file # to put single file from current box to remote box ftp> mget file* # to get files from remote host to current box ftp> mput file* # to put files from current box to remote box ftp> prompt # to disable prompt ftp> bi # binary mode ftp> ha # hash ftp> bye www.opengurukul.com 110
  • 111.
    LINUX MODULE : COMMANDS www.opengurukul.com 111
  • 112.
    Linux: Commands :locate locate : find files by name The locate reads one or more databases prepared by updatedb and writes file names matching at least one of the PATTERNs to standard output, one per line. Example : $ locate php.conf /etc/httpd/conf.d/php.conf $ NOTE: The database for locate is prepared by updatedb. www.opengurukul.com 112
  • 113.
    Linux: Commands :updatedb.conf updatedb.conf : Do not index following file system types, names, paths. $ cat /etc/updatedb.conf PRUNE_BIND_MOUNTS = "yes" PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs" PRUNENAMES = ".git .hg .svn" PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/cache/ccache /var/spool/cups /var/spool/squid /var/tmp" $ www.opengurukul.com 113
  • 114.
    Linux: Commands :find find – search for files in a directory hierarchy find <STARTING DIRECTORY> <OPTIONS> <EXPRESSION> Options : –name : search for files matching a pattern –type : search for files of a particular type ('f' is for regular file and 'd' is for directory) –exec : execute an action if the file is found Example : $ find /etc -name 'httpd.conf' -type f -exec ls -l {} ; -rw-r--r--. 1 root root 34417 Jul 9 2010 /etc/httpd/conf/httpd.conf $ www.opengurukul.com 114
  • 115.
    Linux: Commands: uname uname-s : os name uname -n : node name $ uname -s $ uname -n Linux sachin-pc $ $ uname : os, host, kernel & arch information $ uname -a Linux sachin-pc 2.6.27.9-73.fc9.i686 #1 SMP Tue Dec 16 15:25:05 EST 2008 i686 i686 i386 GNU/Linux $ www.opengurukul.com 115
  • 116.
    Linux: Commands: su su- run a shell with substitute user and group IDs This command is used to switch user. To become a super user, su can be used without arguments or (su root) www.opengurukul.com 116
  • 117.
    Linux: Commands: su: Example $ id uid=500(surikuma) gid=500(surikuma) groups=500(surikuma),501(dba) $ su # without any argument, ask for root password Password: # id uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),501 (dba) # su surikuma # root can switch to any user $ id uid=500(surikuma) gid=500(surikuma) groups=500(surikuma),501(dba) $ www.opengurukul.com 117
  • 118.
    Linux: Commands: date date- print or set the system date and time Syntax : Get Date $ date Sat Sep 19 22:43:14 IST 2009 $ Syntax : Set Date $ date [MMDDhhmm[[CC]YY][.ss] www.opengurukul.com 118
  • 119.
    Linux: Commands: cal cal: displays calendar The cal displays calendar for current month without any args Options: -1 : displays calendar for current month -3 : displays calendar for previous/current/next month -y : displays calendar for current year www.opengurukul.com 119
  • 120.
    Linux: Commands: calexample Example : $ cal October 2011 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 www.opengurukul.com 120
  • 121.
    UNIX MODULE : Fedora& OpenSuse : .rpm packages : rpm and yum www.opengurukul.com 121
  • 122.
    Linux: rpm :-e & -i rpm -i : install package $ rpm -i <package_name.rpm> $ rpm -e : erase/delete package $ rpm -e <package_name> $ www.opengurukul.com 122
  • 123.
    Linux: rpm: packagecontents rpm -q -a : list all packages installed on systems $ rpm -qa | grep -e '^httpd' httpd-tools-2.2.16-1.fc14.i686 httpd-2.2.16-1.fc14.i686 httpd-manual-2.2.16-1.fc14.noarch $ www.opengurukul.com 123
  • 124.
    Linux: rpm -q-list -p rpm -q –list : list files included in an installed rpm package $ rpm -q –list <package_name> $ rpm -q --list php /etc/httpd/conf.d/php.conf /usr/lib/httpd/modules/libphp5.so /var/lib/php/session /var/www/icons/php.gif $ rpm -q –list -p : list files included in an rpm package $ rpm -q –list -p <package_location> $ rpm -q --list -p /var/cache/yum/updates-newkey/packages/*.rpm www.opengurukul.com 124 $
  • 125.
    Linux: rpm: -q-f rpm -q -f <filename> : Find the Package that Contains a file. $ rpm -qf /etc/issue fedora-release-14-1.noarch $ www.opengurukul.com 125
  • 126.
    Linux: yum yum :Install/update packages directly from internet You must be a super user to install package directly from internet. You can create/update repositories information in /etc/yum/repos.d/ www.opengurukul.com 126
  • 127.
    UNIX MODULE : Debin/ Ubuntu LINUX : .deb packages : dpkg and apt-get www.opengurukul.com 127
  • 128.
    Linux: dpkg :-i & -r dpkg -i : install package $ dpkg -i <package_name.deb> $ dpkg -r : remove package $ dpkg -r <package_name> $ www.opengurukul.com 128
  • 129.
    Linux: dpkg :package contents dpkg -l : list all packages installed on systems $ dpkg -l | grep -e 'apache' $ The second filed has the package name. www.opengurukul.com 129
  • 130.
    Linux: dpkg -c& -L dpkg -L : list files included in an installed package $ dpkg -L <package_name> $ dpkg -L php /etc/httpd/conf.d/php.conf /usr/lib/httpd/modules/libphp5.so /var/lib/php/session /var/www/icons/php.gif $ dpkg -c <.deb pkg name>: list files included in a deb package $ dpkg -c <deb_package_location> $ www.opengurukul.com 130
  • 131.
    Linux: dpkg :-S dpkg -S <filename> : Find the Package that Contains a file. $ dpkg -S /bin/ls coreutils: /bin/ls $ www.opengurukul.com 131
  • 132.
    Linux: apt-get apt-get :Install/update packages directly from internet You must be a super user to install package directly from internet. You can create/update repositories information in /etc/apt/sources.list www.opengurukul.com 132
  • 133.
    Linux : apt-get: list, update, install, remove yum list: list packages apt-get install: install package $ yum list <package_name>* $ apt-get install <package_name> $ $ To list packages matching bluefish To install bluefish editor $ yum list *bluefish* $ apt-get install bluefish apt-get update: update package apt-get remove: remove package $ apt-get update <package_name> $ apt-get remove <package_name> $ apt-get update bluefish $ To remove bluefish editor $ apt-get erase bluefish www.opengurukul.com 133
  • 134.
    UNIX MODULE : LINUX www.opengurukul.com 134
  • 135.
    Linux : DistributionInfo /etc/*release - distribution name & version $ ls /etc/*release /etc/fedora-release /etc/redhat-release /etc/system-release $ cat /etc/*release Fedora release 14 (Laughlin) Fedora release 14 (Laughlin) Fedora release 14 (Laughlin) $ www.opengurukul.com 135
  • 136.
    Linux : LSBDistribution Info lsb_release – Distribution Information using LSB $ lsb_release -a LSB Version: :core-4.0-ia32:core-4.0-noarch Distributor ID: Fedora Description:Fedora release 14 (Laughlin) Release: 14 Codename: Laughlin $ www.opengurukul.com 136
  • 137.
    Linux: Services Service :status, start, stop, restart $ service <service_name> status | start | stop | restart Example: $ service httpd status # status of apache web server # service httpd start # start apache web server # service httpd restart # restart apache web server # service httpd stop # stop apache web server www.opengurukul.com 137
  • 138.
    Linux: gzip &gunzip gunzip/gzip : GNU unzip/zip To zip a file using GZIP. $ gzip x.tar (creates x.tar.gz) $ To unzip a file using GUNZIP. $ gunzip x.tar.gz (creates uncompressed x.tar file) $ www.opengurukul.com 138
  • 139.
    Linux: bzip2 &bunzip2 bzip2/bunzip2 - block-sorting file compressor The block-sorting file compressor - better than GNU zip/unzip. bzip2: To zip a file $ bzip2 x.tar (creates x.tar.bz2) $ bunzip2 : To unzip a file $ bunzip2 x.tar.bz2 (creates uncompressed x.tar file) www.opengurukul.com 139 $
  • 140.
    Linux: tar tar :tape archive commands The tape archive is used to combine multiple files and directories into one file. Options : -v verbose mode -f target file -c create tar -t list toc of tar -x extract files from tar www.opengurukul.com 140
  • 141.
    Linux: tar :-c tar -c : create a tar file Example : Syntax : $ cd /tmp tar -c -v -f <tar file> <list of $ tar -cvf team.tar team files> $ tar -cvf /tmp/team.tar team Setup : team/ $ cd /tmp team/india/ $ mkdir -p team/india team/india/sachin $ mkdir -p team/australia team/australia/ $ touch team/india/sachin team/australia/ricky $ touch team/australia/ricky $ www.opengurukul.com 141
  • 142.
    Linux: tar :-t tar -t : list table of contents Syntax : tar -t -v -f <tar_file> Example : $ tar -t -v -f /tmp/team.tar drwxrwxr-x sachin/sachin 0 2011-10-03 23:47 team/ drwxrwxr-x sachin/sachin 0 2011-10-03 23:47 team/india/ -rw-rw-r-- sachin/sachin 0 2011-10-03 23:47 team/india/sachin drwxrwxr-x sachin/sachin 0 2011-10-03 23:47 team/australia/ -rw-rw-r-- sachin/sachin 0 2011-10-03 23:47 team/australia/ricky www.opengurukul.com 142 $
  • 143.
    Linux: tar :-x tar -x : extract files from a tar Example : file $ cd /tmp To extract all the files $ mkdir -p new; cd new $ tar -xvf <tar file> $ tar -xvf /tmp/team.tar $ team/ To extract a particular file/dir team/india/ $ tar -xvf <tar file> <file path> team/india/sachin $ team/australia/ team/australia/ricky $ www.opengurukul.com 143
  • 144.
    Linux: install s/wfrom sources Steps : Steps : Download .tar.bz2 file or .tar.gz. $ bunzip2 software.tar.bz2 Unzip and Extract $ tar -xvf software.tar Configure $ cd software Create Makefile $ ./configure Run Make $ make clean Install using Make $ make all $ make install www.opengurukul.com 144
  • 145.
    Linux: Support Please register yourself @ www.opengurukul.com In case you need any support in future. www.opengurukul.com 145