Linux Commands
Linux Commands
Linux Commands
. Multi user OS Ex: Unix, Linux 3. Network OS Ex: Windows NT/2000/2003 Linux: linux is a multi user/multi tasking operating system which is developed by linus trovalds, when he was a university student. HE made source code available in internet so that every body can access it with out any cost. Features: Multi User Capability Multi Tasking Security User level File level Encryption / decryption Virus Free Communication Scheduling tasks Terminals: Dump Terminals Intelligent Terminals Dial-Up Terminals Linux Kernel:
It is heart of Linux OS where each and every management is taken care by Kernal. Hardware Management Disk Management Process Management Memory Management Shell: IT is a user interface to contact linux. This is a place where user inputs the commands or executes shell scripts. Different types of shells: BORN Shell Bash shell CSh tcsh KSH Sh etc., Commands: Creating users: Syn: useradd [-g <group>] [ -d <path>] <login name> Ex: useradd -g DBA -d /home/madhu madhu Useradd madhu default group: madhu default path: /home/madhu To assign password for the user: Syn: Passwd <username> Ex: passwd madhu New Password: madhu123 Re eneter : madhu123
clear: to clear screen. echo: To display text on to the screen. Ex: echo welcome who: to display list of users curruntly logged in whoami: displays details regarding currunt user cat: 1. Create a file 2. Append to a file 3. displaying file Syn: 1. cat > filename appworld ^d 2. cat >> filename ^d 3. cat filename(s) rose lilly jasmine cat > hello Welcome to KPHB Colony ^d cat >> hello Hyderabad ^d cat hello OR cat
ls:To display list of files. ls l: Display full listing ls al: display long listing of all files(includeing hidden files) mkdir: To create directory Syn: mkdir <dir name(s)> Ex: mkdir max Mkdir jack lion king Pwd: Present Working Directory. Shows currunt working dir. Ex: pwd /home/satish
Root cd: to change directory Syn: cd <dir name> Ex: cd max Pwd /home/satish/max cd .. takes back to parent directory cd to come back to default directory. cp: 1. to copy files Syn: cp file1 file2 2. to copy files to directory Syn: cp file1 file2 file3 file4 directory. mv: 1. To rename a file/directory Ex: mv oldname newmame 2. To move files into a directory Syn: mv file1 file2 file3 file4 directory. rm: To remove files. Syn: rm filename [file2 file3 ]
rmdir: To remove directory. We can delete only empty directory. Syn: rmdir dirname(s) rm r: to remove directory even it is not empty. rm r dirname DAY 2: touch: To create empty files. Syn: touch <filename(s)> Ex: touch max jack king Syn:
ln: to link files. We can give a new name for exisiting file. The file can be accessed by both the names. Advantages: A single file will have two names. Memory occupied by both files is same. If file is deleted, it exisit with second name. Syn: ln <file1> <file2> Ln <file(s)> <dir> Ex: ln hello rose cal: To display calender Syn: cal [[<mm>] <yyyy>] Ex: cal displays currunt month cal 2007 cal 09 1977 date: to display system date Ex: date Thu Jul 23 10:11:12 IST 2009 Formats: %d day %m month %y year %H hours %M minuts %S sec. %n new line date '+ Today is %d' date '+ %d/%m/%y' date '+ %d/%m/%y%n%H:%M:%S' man: displays manual of any command. Ex: man date
bc: Base Calculator bc 10+20 30 18/2 9 10+58*78/86 62 scale=2 to specify decimal spaces. 10+58*78/86 62.60 ^d expr: Arithmetic Expression solver Syn: expr <arith opr> Ex: expr 10 + 20 30 +, -, \*, /, \( \) WILD CARD Characters: ? Single Char. * More Chars [ ] Any one among group. ls l f* displays all files which start with f ls l ??? ls l s?n rm *.dat cp * max ls l s[o,u]n ls l [a,e,i,o,u]* all files starting with a/e/i/o/u
A path is a address of a file. there are two types of paths. * absolute path * relative path Absolute Path: The full path of a file is called absolute path. i.e. a file is represented with its full address from the root directory. $ pwd /home/madhu $ cat /home/madhu/flowers/trees/rose/r1 $rm /home/madhu/flowers/creepy/lilly/* $cp /home/madhu/flowers/trees/jasmine/* /home/madhu/flowers/creepy/lotus $cp ~/flowers/trees/jasmine/* ~/flowers/creepy/lotus ~ default home directory Relative Path:
If a file is represented relative to currunt working directory, it is relative paths. $pwd /home/madhu $cp flowers/trees/jasmine/* flowers/creepy/lotus $ cd flowers $ pwd /home/madhu/flowers $cp trees/rose/* creepy/lotus/white $cp ~/* . $cd trees/rose $ pwd /home/madhu/flowers/trees/rose $ cp * .. [cpoies all files to parent dir] $cp * ../jasmine [copies all files to jasmine dir. Present in parent ] $cd ../..
DAY 3: VI Editor: To modify file contents and also used to create files. Syn: vi <filename> esc : Comes out of insert mode :w save the file :q quit :wq save and quit :q! Force quit There are two types of commands:
Command mode commands Xcommand mode commands Text insertion commands: i: to Insert text at currunt cursor possition a: to insert text after currunt cursor possition I: Inserts text at beginning of line A: Inserts text at end of line o (small): To insert new line after cursor position O : To insert new line before cursor position Text replace commands: r: To replace a single character R: To replace text Commands to delete text: x(small): To delete a character at current cursor position [Works like DEL key] X (Cap): To delete a character before cursor position [ works like BACK SPACE key] dd: TO delete current line ndd: Deletes n lines. Ex: 3dd delete 3 lines from cursor position. dw: To delete a word. Deletes from cursor position to end of word. d$: To delete text from cursor position to end of line Cursor Movement Commands: l (small L): Moves cursor right side h : move cursor left side j: move cursor to next line k: move cursor to prev. line
H: Moves to bigging of file L: Moves to end of file Yanking(coping) and pasting commands yy: to copy current line nyy: To copy n lines. Ex: 4yy p: to paste lines. np: pastes n times.
Xcommand Mode commands: :/<text> to serach for specified text n Serch of next occurrence N serach for previous occurrence :?<text> to serach for text in revrse direction Wecan use n & N to search text :[m,n] s/<text>/<repl.text>[/g] To search and replace text : s/Rose/Lotus Replaces first occurrence of Rose with Lotus in currunt line : s/Rose/Lotus/g Replaces all occurrence of Rose with Lotus in currunt line. Here g means globel. :1, 5 s/Rose/Lotus Replaces first occurrence of Rose with Lotus in all 1st to 5th lines. :1, 5 s/Rose/Lotus/g Replaces all occurrence of Rose with Lotus in all 1st to 5th lines. . currunt line $ last line
:1,$ s/Rose/Lotus/g replaces from first line to last line : .,$ s/Rose/Lotus/g replaces from currunt line to last line Miscellaneous Commands: ~ : To change case Ctrl + l To redraw the screen J: To join two lines. u (small): Undo U : Undo all operations on same line chmod: [Change mode]. To change File access permissions There two mode of chmod 1. Symbolic Mode 2. Absolute Mode Symbolic Mode: Syn: chmod <whome>+/-/=<permission> <filename> + adding - removing = assign Users Permissions Owner/user- u read - r Group - g write - w Others - o execute - x All a Default Permissions for a file: rwr- U G O r--
chmod o+w hello giving write permissions to others chmod g+r,g+w,g+x hello chmod g+rwx hello chmod g-wx hello chmod o+rwx hello chmod o=r hello chmod ugo+rwx hello OR chmod a+rwx hello Absolute Mode: Syn: chmod p1p2p3 <filename> p1 Permissions for Owner p2 Permissions for group members p3 Permissions for Others r-4 w-2 x-1 7 6 5 4 3 2 1 0 rwx (4+2+1) rwr-x r--wx -w--x ---
Ex: chmod 777 hello chmod 700 hello Note: If directory have execute permissions then only we can enter into it. Example program: $ cat > prg
date who cal 09 1977 ^d $ Chmod u+x prg $ ./prg Umask: displays umask value or sets umask value. A umask value represents default permissions to a directory. $umask 0002 Default permissions of a dir is total permissions minus umask value. Ie. 777-002 = 775 $umask 077 Default permissions of new dir is total permissions minus umask value. Ie. 777077 = 700 $ mkdir bond drwx------
FILTER COMMANDS: head: To displays specified no. of lines from top of the file. Syn: head n filename Ex: head -5 hello displays first 5 lines from the file hello Tail: To display specified no. of last lines. Syn: tail -/+n filename
Ex: tail -5 hello displays last 5 lines tail +5 hello displays 5th line to last line wc: Word Count Syn: wc [-options] filename Ex: wc hello 11 27 145 hello Options: -l : to display lines count -w: to display words count -c: to display chars count Ex: wc -l hello DAY 4 sort: To display contents of file in sorted order. Syn: sort [-options] filename $cat > emp madhu 31 kumar 24 aruna 23 jeeva 28 nandu 21 ramana 18 ^d $sort emp arun a 23 jeeva 28 kumar 24 madhu 31 nandu 21 ramana 18 manager clerk typist CLERK TYPIST peon 8000 5000 4000 5500 3500 950
Options: -r: to sort data in reverse order -km[,n]: Here m & n are numbers. Used to sort data on specified column. m to specify filed on which we want to sort data. Ex: sort -k3 emp n: In sorting, if there is equal data in two rows, it compares the next field and so on.To specify where sorting should be stopped we use -n option. Ex: sort -k3,4 emp -f : to ignore case wile sorting. Sort f emp Sort k3 f r emp Sort k3 f emp -n: to sort numbers Sort k4 n emp -t: to specify field seperator if other than TAB is used. sort k3 t ; emp grep: [Globel Regular Expression Printer] To search for specified text and display lines containing the text. Syn: grep [-options] text filename Ex: grep clerk emp Options: -v: Displays lines those do not contain specified text. -i: TO ignore case -c: Displays count of lines -n: Displays lines along wioth line numbers cut: To cut fields and columns
Syn: cut [options] filename Options: -f: To cut fields -c: to cut columns[characters] -d: to specify seperator if other than tab is used. Ex: cut -f1 emp emp cut -f1,3 emp emp cut -f1,3 -d ";" emp2 cut c1 cur c1,4
paste: to combine more than one file horizontally Ex: paste emp emp2 Tr: Translate 1. tr chars chars < filename To change one character to another 2. tr s char < filename to supress repeated character 3. tr d char < filename to delete a character Ex: tr a z < emp replaces a with z in the file emp tr abc xyz < emp replaces a with x, b with y, c with z in the file emp. tr a-z A-Z < emp Piping(|): used to combine more than one command. i.e. sends one commands output as input to another command. Ex: grep clerk emp | sort
who | sort who | grep satish | sort ls l | more ls l | sort | more ls l | grep Jul 25 tee: To get intermediate output into a file Ex: grep clerk emp | tee hello | sort Grep commands output will be sent into the file hello and also sent to sort command. Redirection Symbols: > Output Redirection Symbol >> Append ,, ,, < Input ,, ,, who > xyz date > xyz ls -l >> xyz cat < xyz 0 standerd input 1 - standerd output 2 standerd error sort aaa 1> hello 2>hi DAY 5 find: To search for files including sub directories. Syn: find <path/pattern> [options] Ex: find . find ./m* find ~/m*
Options: -mtime n: searches for the files which are last modified before n*24 hours. Ex: Display files older than 7 days. find ~/* -mtime 7 -type: to specify what you want to serach. Ex. To search files we use f to serach dirs we use d Ex: find ~/* -type f -exec: Used to execute a command based on the search result. Ex: find max/* -type f exec rm { } \; -user: To search files which are owned by a user. Ie. To search for files which are created by specified user. Ex: find ~/* -user root Sed: 1. To replace a text with another text in a file 2. To delete lines containing specified text Syn 1: sed s/text/rtext/g <filename> Ex: sed s/madhu/kumar/g emp Syn 2: sed /text/d <filename> Ex: sed /typist/d emp awk: pattern scanning and processing language Syn: awk {commands} filename Ex: awk { print $1} emp gets first filed in file emp and displays them with print command. awk { print $2 + $4 } emp awk '{ print "Employee name " $1 }' emp awk -F: { print $2 + $4 } emp2 [useinf -F we can specifiy field seperator]
awk { sum+=$4 }; END { print sum } emp adds all values in $4 field and displays total. Background Processing: If a process is running in the background where user can work with other porcesses in frontground, then it is called background processing. We can send a process to background using & symbol at the end of any command/script. ps: to show list of processes. Ex: ps Shows processes ruinning under currunt user with PID, TTY, CMD Options: -f: shows User Id (UId) and also parent process id (PPID) -e: Shows all processes running in linux server. Ex: ps f ps e ps ef kill: To kill a proces. Syn: kill [-9] <PID> Ex: kill 7922 Kill -9 6711 sure kill Sending a process to background: $cat > prog while true do echo "xyz" > /dev/null
done ^d Execute the above scrip as follows: $ sh prog & Communication Commands write: To write a message on another user terminal. Syn: write <username> <terminal> Ex: write hyma Message(s) ^d Write satish pts/4 Message(s) ^d Mesg: To enab;e/disable/view message status $ mesg is y $mesg n To disable messages $mesg y To enable messages. who T: Displays list of user along with message status. Message status will be either + or -. If it is +, mean user have messages enabled. mail: To send or view mails. Syn to Send mail: $ mail <username> Sub: .
Cc: ^d Syn: To view $ mail Scheduling Commands: at: Used to execute a command/shell script in future time. Syn: $ at hh:mm at> command at> ^d Ex: at 17:45 at>cp hello hi at> ^d atq: Displays list of jobs pending atrm: to remove jobs. Ex: atrm 2 here 2 is job id batch: to execute a batch of commands at system free times. Executes commands if system load average is less than 0.8. Syn: batch at> command ^d Ex: batch cp hello king ^d crontab: Executes command/shell script at regular intervals of time. Syn:
Crontab Min hours day month weekday command ^d Ex: Crontab 28 12 27 7 * cp /home/madhu/max/hello /home.madhu/max/hi ^d Every year on 27th july at 12:28 executes the cp command Example patterns: 30 10 1 * * command 30 * * * * command crontab -l: to view list of cron jobs crontab -e: To edit/add/remove jobs crontab r: to remove all jobs