50 Most Frequently Used UNIX - Linux Commands (With Examples)
50 Most Frequently Used UNIX - Linux Commands (With Examples)
311
Like
455
This article provides practical examples for 50 most frequently used commands in Linux / UNIX. This is not a comprehensive list by any means, but this should give you a jumpstart on some of the common Linux commands. Bookmark this article for your future reference. Did I miss any frequently used Linux commands? Leave a comment and let me know.
$t a rc v fa r c h i v e _ n a m e . t a rd i r n a m e /
$t a rx v fa r c h i v e _ n a m e . t a r
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 1/35
3/20/2014
$t a rt v fa r c h i v e _ n a m e . t a r
More tar examples: The Ultimate Tar Command Tutorial with 10 Practical Examples
$g r e pi" t h e "d e m o _ f i l e
Print the matched line, along with the 3 lines after it.
$g r e pA3i" e x a m p l e "d e m o _ t e x t
http://www.thegeekstuff.com/2010/11/50-linux-commands/
2/35
3/20/2014
$g r e pr" r a m e s h "*
More grep examples: Get a Grip on the Grep! 15 Practical Grep Command Examples
#f i n di n a m e" M y C P r o g r a m . c "
$f i n di n a m e" M y C P r o g r a m . c "e x e cm d 5 s u m{ }\ ;
#f i n d~e m p t y
More find examples: Mommy, I found it! 15 Practical Linux Find Command Examples
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 3/35
3/20/2014
s s hlj s m i t hr e m o t e h o s t . e x a m p l e . c o m
s s hvlj s m i t hr e m o t e h o s t . e x a m p l e . c o m
$s s hV O p e n S S H _ 3 . 9 p 1 ,O p e n S S L0 . 9 . 7 aF e b1 92 0 0 3
$ s e d' s / . $ / / 'f i l e n a m e
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 4/35
3/20/2014
$s e dn' 1 ! G ; h ; $ p 't h e g e e k s t u f f . t x t
Print all lines from /etc/passwd that has the same uid and gid
$a w k' { p r i n t$ 2 , $ 5 ; } 'e m p l o y e e . t x t
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 5/35
3/20/2014
More awk examples: 8 Powerful Awk Built-in Variables FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
$v i m+ 1 4 3f i l e n a m e . t x t
$v i m+ / s e a r c h t e r mf i l e n a m e . t x t
$v i mR/ e t c / p a s s w d
#d i f fwn a m e _ l i s t . t x tn a m e _ l i s t _ n e w . t x t
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 6/35
3/20/2014
More diff examples: Top 4 File Difference Tools on UNIX / Linux Diff, Colordiff, Wdiff, Vimdiff
$s o r tn a m e s . t x t
$s o r trn a m e s . t x t
$s o r tt :k3 n/ e t c / p a s s w d|m o r e
3/20/2014
$e x p o r t|g r e pO R A C L E d e c l a r exO R A C L E _ B A S E = " / u 0 1 / a p p / o r a c l e " d e c l a r exO R A C L E _ H O M E = " / u 0 1 / a p p / o r a c l e / p r o d u c t / 1 0 . 2 . 0 " d e c l a r exO R A C L E _ S I D = " m e d " d e c l a r exO R A C L E _ T E R M = " x t e r m "
$e x p o r tO R A C L E _ H O M E = / u 0 1 / a p p / o r a c l e / p r o d u c t / 1 0 . 2 . 0
#l s* . j p g|x a r g sn 1ic p{ }/ e x t e r n a l h a r d d r i v e / d i r e c t o r y
3/20/2014
#c a tu r l l i s t . t x t|x a r g sw g e t c
$l sl h r w r -1r a m e s ht e a m d e v8 . 9 MJ u n1 21 5 : 2 7a r c h l i n u x . t x t . g z
Order Files Based on Last Modified Time (In Reverse Order) Using ls ltr
$l sl t r
$l sF
3/20/2014
$g z i pt e s t . t x t
$g z i pdt e s t . t x t . g z
$g z i pl* . g z c o m p r e s s e d 2 3 7 0 9
u n c o m p r e s s e d r a t i ou n c o m p r e s s e d _ n a m e 9 7 9 7 5 7 5 . 8 %a s p p a t c h r p m s . t x t
http://www.thegeekstuff.com/2010/11/50-linux-commands/
10/35
3/20/2014
$b z i p 2t e s t . t x t
b z i p 2dt e s t . t x t . b z 2
More bzip2 examples: BZ is Eazy! bzip2, bzgrep, bzcmp, bzdiff, bzcat, bzless, bzmore examples
$u n z i pt e s t . z i p
$u n z i plj a s p e r . z i p A r c h i v e : j a s p e r . z i p L e n g t h D a t e T i m e - http://www.thegeekstuff.com/2010/11/50-linux-commands/
N a m e 11/35
3/20/2014
4 0 9 9 5 3 2 1 6 9 1 5 9 6 4 1 0 5 4 2
1 1 3 0 9 82 3 : 5 0 0 8 2 5 9 82 1 : 0 7 0 8 2 5 9 82 1 : 0 7 0 8 2 5 9 82 1 : 0 7
M E T A I N F / M A N I F E S T . M F c l a s s e s _ c l a s s e s _ n a m e s c l a s s e s _ n c o m p
#s h u t d o w nhn o w
#s h u t d o w nh+ 1 0
#s h u t d o w nrn o w
#s h u t d o w nF rn o w
http://www.thegeekstuff.com/2010/11/50-linux-commands/
12/35
3/20/2014
$f t pI P / h o s t n a m e f t p >m g e t* . h t m l
To view the file names located on the remote server before downloading, mls ftp command as shown below.
f t p >m l s* . h t m l/ f t p t e s t / f e a t u r e s . h t m l / f t p t e s t / i n d e x . h t m l / f t p t e s t / o t h e r t o o l s . h t m l / f t p t e s t / s a m p l e r e p o r t . h t m l / f t p t e s t / u s a g e . h t m l
More ftp examples: FTP and SFTP Beginners Guide with 10 Examples
#c r o n t a buj o h nl
http://www.thegeekstuff.com/2010/11/50-linux-commands/
13/35
3/20/2014
* / 1 0****/ h o m e / r a m e s h / c h e c k d i s k s p a c e
#s e r v i c es s hs t a t u s
s e r v i c es t a t u s a l l
Restart a service.
#s e r v i c es s hr e s t a r t
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 14/35
3/20/2014
$p se f|m o r e
To view current running processes in a tree structure. H option stands for process hierarchy.
$p se f H|m o r e
3/20/2014
$f r e e t o t a l M e m : 3 5 6 6 4 0 8 / +b u f f e r s / c a c h e : S w a p : 4 0 0 0 1 7 6 u s e d 1 5 8 0 2 2 0 4 7 3 2 7 2 0 f r e e 1 9 8 6 1 8 8 3 0 9 3 1 3 6 4 0 0 0 1 7 6 s h a r e d 0 b u f f e r s 2 0 3 9 8 8
c a c h 9 0 2 9
If you want to quickly check how many GB of RAM your system has use the -g option. -b option displays in bytes, -k in kilo bytes, -m in mega bytes.
$f r e eg t o t a l M e m : 3 / +b u f f e r s / c a c h e : S w a p : 3 u s e d 1 0 0 f r e e 1 2 3 s h a r e d 0 b u f f e r s 0
c a c h
If you want to see a total memory ( including the swap), use the -t switch, which will display a total line as shown below.
r a m e s h @ r a m e s h l a p t o p : ~ $f r e et t o t a l u s e d M e m : 3 5 6 6 4 0 8 1 5 9 2 1 4 8 / +b u f f e r s / c a c h e : 4 7 5 3 3 2 S w a p : 4 0 0 0 1 7 6 0 T o t a l : 7 5 6 6 5 8 4 1 5 9 2 1 4 8
f r e e 1 9 7 4 2 6 0 3 0 9 1 0 7 6 4 0 0 0 1 7 6 5 9 7 4 4 3 6
s h a r e d 0
b u f f e r s 2 0 4 2 6 0
c a c h 9 1 2 5
http://www.thegeekstuff.com/2010/11/50-linux-commands/
16/35
3/20/2014
C u r r e n tS o r tF i e l d : P f o rw i n d o w1 : D e f S e l e c ts o r tf i e l dv i af i e l dl e t t e r ,t y p ea n yo t h e rk e yt or e t u r n a :P I D d :U I D e :U S E R . . . . . . . . =P r o c e s sI d =U s e rI d =U s e rN a m e v :n D R T y :W C H A N z :F l a g s
=D i r t yP a g e sc =S l e e p i n gi nF =T a s kF l a g s
To displays only the processes that belong to a particular user use -u option. The following will show only the top processes that belongs to oracle user.
$t o puo r a c l e
More top examples: Can You Top This? 15 Practical Linux Top Command Examples
3/20/2014
Displays the file system disk space usage. By default df -k displays output in bytes.
$d fk F i l e s y s t e m / d e v / s d a 1 / d e v / s d a 2
1 K b l o c k s U s e dA v a i l a b l eU s e %M o u n t e do n 2 9 5 3 0 4 0 0 3 2 3 3 1 0 4 2 4 7 9 7 2 3 2 1 2 %/ 1 2 0 3 6 7 9 9 2 5 0 1 7 1 5 9 6 6 4 0 8 2 0 6 0 4 4 %/ h o m e
df -h displays output in human readable form. i.e size will be displayed in GBs.
r a m e s h @ r a m e s h l a p t o p : ~ $d fh F i l e s y s t e m S i z e U s e dA v a i lU s e %M o u n t e do n / d e v / s d a 1 2 9 G 3 . 1 G 2 4 G 1 2 %/ / d e v / s d a 2 1 1 5 G 4 8 G 6 2 G 4 4 %/ h o m e
r a m e s h @ r a m e s h l a p t o p : ~ $d fT F i l e s y s t e m T y p e 1 K b l o c k s U s e dA v a i l a b l eU s e %M o u n t e do n / d e v / s d a 1 e x t 4 2 9 5 3 0 4 0 0 3 2 3 3 1 2 0 2 4 7 9 7 2 1 6 1 2 %/ / d e v / s d a 2 e x t 4 1 2 0 3 6 7 9 9 2 5 0 1 7 1 5 9 6 6 4 0 8 2 0 6 0 4 4 %/ h o m e
3/20/2014
Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running Linux process as shown below. You can also use killall, pkill, xkill to terminate a unix process.
$p se f|g r e pv i m r a m e s h 7 2 4 3 7 2 2 2 92 2 : 4 3p t s / 2 $k i l l97 2 4 3
0 0 : 0 0 : 0 0v i m
More kill examples: 4 Ways to Kill a Process kill, killall, pkill, xkill
$r mif i l e n a m e . t x t
It is very useful while giving shell metacharacters in the file name argument. Print the filename and get confirmation before removing the file.
$r mif i l e *
http://www.thegeekstuff.com/2010/11/50-linux-commands/
19/35
3/20/2014
Following example recursively removes all files and directories under the example directory. This also removes the example directory itself.
$r mre x a m p l e
$c ppf i l e 1f i l e 2
Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.
$c pif i l e 1f i l e 2
$m vif i l e 1f i l e 2
http://www.thegeekstuff.com/2010/11/50-linux-commands/
20/35
3/20/2014
Note: mv -f is just the opposite, which will overwrite file2 without prompting. mv -v will print what is happening during file rename, which is useful while specifying shell metacharacters in the file name argument.
$m vvf i l e 1f i l e 2
$c a tf i l e 1f i l e 2
While displaying the file, following cat -n command will prepend the line number to each line of the output.
$c a tn/ e t c / l o g r o t a t e . c o n f 1 / v a r / l o g / b t m p{ 2 m i s s i n g o k 3 m o n t h l y 4 c r e a t e0 6 6 0r o o tu t m p 5 r o t a t e1 6 }
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 21/35
3/20/2014
#m k d i r/ u 0 1 #m o u n t/ d e v / s d b 1/ u 0 1
You can also add this to the fstab for automatic mounting. i.e Anytime system is restarted, the filesystem will be mounted.
/ d e v / s d b 1/ u 0 1e x t 2d e f a u l t s02
$c h m o du g + r w xf i l e . t x t
Revoke all access for the group (i.e read, write and execute ) on a
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 22/35
3/20/2014
specific file.
$c h m o dg r w xf i l e . t x t
Apply the file permissions recursively to all the files in the subdirectories.
$c h m o dRu g + r w xf i l e . t x t
$c h o w no r a c l e : d b ad b o r a . s h
$c h o w nRo r a c l e : d b a/ h o m e / o r a c l e
http://www.thegeekstuff.com/2010/11/50-linux-commands/
23/35
3/20/2014
$p a s s w d
Super user can use passwd command to reset others password. This will not prompt for current password of the user.
#p a s s w dU S E R N A M E
Remove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password.
#p a s s w ddU S E R N A M E
$m k d i r~ / t e m p
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 24/35
3/20/2014
Create nested directories using one mkdir command. If any of these directories exist already, it will not display any error. If any of these directories doesnt exist, it will create them.
$m k d i rpd i r 1 / d i r 2 / d i r 3 / d i r 4 /
$i f c o n f i ga
Start or stop a specific interface using up and down command as shown below.
$i f c o n f i ge t h 0u p $i f c o n f i ge t h 0d o w n
3/20/2014
$u n a m ea L i n u xj o h n l a p t o p2 . 6 . 3 2 2 4 g e n e r i c# 4 1 U b u n t uS M PT h uA u g1 90 1 : 1 2 : 5 2
$w h e r e i sl s l s :/ b i n / l s/ u s r / s h a r e / m a n / m a n 1 / l s . 1 . g z/ u s r / s h a r e / m a n / m a n 1 p / l s . 1 p . g z
When you want to search an executable from a path other than the whereis default path, you can use -B option and give path as argument to it. This searches for the executable lsmk in the /tmp directory, and displays it, if it is available.
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 26/35
3/20/2014
$w h e r e i suB/ t m pfl s m k l s m k :/ t m p / l s m k
$w h a t i sl s l s
( 1 ) -l i s td i r e c t o r yc o n t e n t s
$w h a t i si f c o n f i g i f c o n f i g( 8 )
-c o n f i g u r ean e t w o r ki n t e r f a c e
$l o c a t ec r o n t a b / e t c / a n a c r o n t a b / e t c / c r o n t a b / u s r / b i n / c r o n t a b
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 27/35
3/20/2014
/ u s r / s h a r e / d o c / c r o n / e x a m p l e s / c r o n t a b 2 e n g l i s h . p l . g z / u s r / s h a r e / m a n / m a n 1 / c r o n t a b . 1 . g z / u s r / s h a r e / m a n / m a n 5 / a n a c r o n t a b . 5 . g z / u s r / s h a r e / m a n / m a n 5 / c r o n t a b . 5 . g z / u s r / s h a r e / v i m / v i m 7 2 / s y n t a x / c r o n t a b . v i m
$m a nc r o n t a b
When a man page for a command is located under more than one section, you can view the man page for that command from a specific section as shown below.
$m a nS E C T I O N N U M B E Rc o m m a n d n a m e
Following 8 sections are available in the man page. 1. General commands 2. System calls 3. C library functions 4. Special files (usually devices, those found in /dev) and drivers 5. File formats and conventions
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 28/35
3/20/2014
6. Games and screensavers 7. Miscellaneous 8. System administration commands and daemons For example, when you do whatis crontab, youll notice that crontab has two man pages (section 1 and section 5). To view section 5 of crontab man page, do the following.
$w h a t i sc r o n t a b c r o n t a b( 1 ) c r o n t a b( 5 ) $m a n5c r o n t a b
-m a i n t a i nc r o n t a bf i l e sf o ri n d i v i d u a lu s e r s( V 3 ) -t a b l e sf o rd r i v i n gc r o n
$t a i lf i l e n a m e . t x t
$t a i lnNf i l e n a m e . t x t
http://www.thegeekstuff.com/2010/11/50-linux-commands/
29/35
3/20/2014
View the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C.
$t a i lfl o g f i l e
More tail examples: 3 Methods To View tail -f output of Multiple Log Files in One Terminal
$l e s sh u g e l o g f i l e . l o g
One you open a file using less command, following two keys are very helpful.
C T R L + Ff o r w a r do n ew i n d o w C T R L + Bb a c k w a r do n ew i n d o w
More less examples: Unix Less Command: 10 Tips for Effective Navigation
http://www.thegeekstuff.com/2010/11/50-linux-commands/ 30/35
3/20/2014
$s u-U S E R N A M E
Execute a single command from a different account name. In the following example, john can execute the ls command as raj username. Once the command is executed, it will come back to johns account.
Login to a specified user account, and execute the specified shell instead of the default shell.
$s us' S H E L L N A M E 'U S E R N A M E
3/20/2014
database running on the remote server. To connect to a remote mysql database. This will prompt for a password.
$m y s q lur o o tph1 9 2 . 1 6 8 . 1 . 2
$m y s q lur o o tp
If you want to specify the mysql root password in the command line itself, enter it immediately after -p (without any space).
$y u mi n s t a l lh t t p d
$y u mu p d a t eh t t p d
http://www.thegeekstuff.com/2010/11/50-linux-commands/
32/35
3/20/2014
$y u mr e m o v eh t t p d
#r p mi v hh t t p d 2 . 2 . 3 2 2 . 0 . 1 . e l 5 . i 3 8 6 . r p m
#r p mu v hh t t p d 2 . 2 . 3 2 2 . 0 . 1 . e l 5 . i 3 8 6 . r p m
#r p me vh t t p d
More rpm examples: RPM Command: 15 Examples to Install, Uninstall, Upgrade, Query RPM Packages
3/20/2014
$p i n gc5g m a i l . c o m
#d a t es" 0 1 / 3 1 / 2 0 1 02 3 : 5 9 : 5 3 "
Once youve changed the system date, you should syncronize the hardware clock with the system date as shown below.
#h w c l o c k s y s t o h c #h w c l o c ks y s t o h c u t c
3/20/2014
$w g e th t t p : / / p r d o w n l o a d s . s o u r c e f o r g e . n e t / s o u r c e f o r g e / n a g i o s / n a g i o s 3 . 2
$w g e tOt a g l i s t . z i ph t t p : / / w w w . v i m . o r g / s c r i p t s / d o w n l o a d _ s c r i p t . p h p ? s r
http://www.thegeekstuff.com/2010/11/50-linux-commands/
35/35