Jump to content

Linux 101 hacks: Difference between revisions

From TetraWiki
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[category:Linux-General‏‎]]


=='''Linux 101 Hacks'''==
=='''Linux 101 Hacks'''==
Line 8: Line 8:




'''Table of Contents'''
=='''Introduction'''==




'''Introduction7'''
''”There are only 10 types of people in the world — those who understand [http://en.wikipedia.org/wiki/Binary_numeral_system ][http://en.wikipedia.org/wiki/Binary_numeral_system binary], those who don’t, and those who understand [http://en.wikipedia.org/wiki/Gray_code ][http://en.wikipedia.org/wiki/Gray_code gray code]”''




'''Foreword8'''
'''''— Geek'''''




'''Version8'''
There are total of 101 hacks in this book that will help you build a strong foundation in Linux. All the hacks in this book are explained with appropriate Linux command examples that are easy to follow.




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page9 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page9 Chapter 1: Powerful CD Command Hacks]9'''
This book contains 12 chapters. Hacks mentioned in 6 chapters are based on the articles that I’ve already posted on my blog. Hacks mentioned in rest of the 6 chapters are brand new.




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page9 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page9 Hack 1. Use CDPATH to define the base directory for cd command]9'''
I’m Ramesh Natarajan, author of [http://www.thegeekstuff.com/ ][http://www.thegeekstuff.com/ The Geek Stuff][http://www.thegeekstuff.com/ ]blog and this eBook.


[[Image:]]


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page10 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page10 Hack 2. Use cd alias to navigate up the directory effectively]10'''


I have done intensive programming on several languages and C is my favorite. I have done lot of work on the infrastructure side including Linux system administration, DBA, Networking, Hardware and Storage (EMC).


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page13 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page13 Hack 3. Perform mkdir and cd using a single command]13'''


I have also developed [http://www.passworddragon.com/ ][http://www.passworddragon.com/ Password Dragon][http://www.passworddragon.com/  ]— free, easy and secure password manager that runs on Windows, Linux and Mac.


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page14 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page14 Hack 4. Use “cd -” to toggle between the last two directories]14'''


If you have any feedback about this eBook, please use this [http://www.thegeekstuff.com/contact/ ][http://www.thegeekstuff.com/contact/ contact form][http://www.thegeekstuff.com/contact/  ]to get in touch with me.


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page14 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page14 Hack 5. Use dirs, pushd and popd to manipulate directory stack]14'''


''Copyright © 2009 – Ramesh Natarajan''


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page17 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page17 Hack 6. Use “shopt -s cdspell” to automatically correct mistyped]


=='''Foreword'''==


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page17 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page17 directory names on cd]17'''


Another collection of hacks? Yes! If you have just completed your first admin course or looking for better ways to get the job done the "Linux 101 Hacks" eBook is a good point to start. These useful tips are concise, well written and easy to read.


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page18 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page18 Chapter 2: Date Manipulation]18'''


Well done - I will recommend this eBook to my students.


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page18 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page18 Hack 7. Set System Date and Time]18'''


'''--Prof. Dr. Fritz Mehner, FH Südwestfalen, Germany'''


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page19 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page19 Hack 8. Set Hardware Date and Time]19'''


(Author of several [http://www.vim.org/account/profile.php?user_id=169 ][http://www.vim.org/account/profile.php?user_id=169 Vim plugins], including [http://www.vim.org/scripts/script.php?script_id=365 ][http://www.vim.org/scripts/script.php?script_id=365 bash-support vim plugin])


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page19 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page19 Hack 9. Display Current Date and Time in a Specific Format]19'''


=='''Version'''==


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page21 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page21 Hack 10. Display Past Date and Time]21'''




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page22 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page22 Hack 11. Display Future Date and Time]22'''
{| style="border-spacing:0;"
| style="border-top:1pt solid #000001;border-bottom:none;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Version'''
| style="border-top:1pt solid #000001;border-bottom:none;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Date'''
| style="border-top:1pt solid #000001;border-bottom:none;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Revisions'''


|-
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page24 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page24 Chapter 3: SSH Client Commands]24'''
|-
| style="border-top:none;border-bottom:none;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1.0
| style="border-top:none;border-bottom:none;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12-Feb-2009
| style="border-top:none;border-bottom:none;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| First Edition


|-
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page24 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page24 Hack 12. Identify SSH Client Version]24'''
|-
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
Download the [http://www.thegeekstuff.com/linux-101-hacks-free-ebook ][http://www.thegeekstuff.com/linux-101-hacks-free-ebook latest version of the book here].


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page24 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page24 Hack 13. Login to Remote Host using SSH]24'''
='''Chapter 1: Powerful CD Command Hacks'''=




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page26 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page26 Hack 14. Debug SSH Client Session]26'''
cd is one of the most frequently used commands during a UNIX session. The 6 cd command hacks mentioned in this chapter will boost your productivity instantly and make it easier to navigate the directory structure from command line.




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page27 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page27 Hack 15. Toggle SSH Session using SSH Escape Character]27'''
=='''Hack 1. Use CDPATH to define the base directory for cd command'''==




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page29 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page29 Hack 16. SSH Session Statistics using SSH Escape Character]29'''
If you are frequently performing cd to subdirectories of a specific parent directory, you can set the CDPATH to the parent directory and perform cd to the subdirectories without giving the parent directory path as explained below.




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page31 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page31 Chapter 4: Essential Linux Commands]31'''
<nowiki>[ramesh@dev-db ~]# </nowiki>'''pwd''' /home/ramesh


<nowiki>[ramesh@dev-db </nowiki>~]# '''cd mail'''


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page31 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page31 Hack 17. Grep Command]31'''2'''Linux 101 Hacks'''www.thegeekstuff.com


-bash: cd: mail: No such file or directory


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page33 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page33 Hack 18. Find Command]33'''


<nowiki>[</nowiki>'''Note:''' This is looking for mail directory under current directory]


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page35 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page35 Hack 19. Suppress Standard Output and Error Message]35'''


<nowiki>[ramesh@dev-db ~]# </nowiki>'''export CDPATH=/etc'''<nowiki> [ramesh@dev-db ~]# </nowiki>'''cd mail'''


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page35 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page35 Hack 20. Join Command]35'''
/etc/mail




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page36 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page36 Hack 21. Change the Case]36'''
<nowiki>[</nowiki>'''Note:''' This is looking for mail under /etc and not under current directory]




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page37 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page37 Hack 22. Xargs Command]37'''
<nowiki>[ramesh@dev-db /etc/mail]# </nowiki>'''pwd''' /etc/mail




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page38 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page38 Hack 23. Sort Command]38'''
To make this change permanent, add export CDPATH=/etc to your ~/.bash_profile




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page41 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page41 Hack 24. Uniq Command]41'''
Similar to the PATH variable, you can add more than one directory entry in the CDPATH variable, separating them with : , as shown below.




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page42 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page42 Hack 25. Cut Command]42'''
export CDPATH=.:~:/etc:/var




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page43 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page43 Hack 26. Stat Command]43'''
This hack can be very helpful under the following situations:




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page44 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page44 Hack 27. Diff Command]44'''
# Oracle DBAs frequently working under $ORACLE_HOME, can set the CDPATH variable to the oracle home


# Unix sysadmins frequently working under /etc, can set the CDPATH variable to /etc


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page45 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page45 Hack 28. Display total connect time of users]45'''
# Developers frequently working under project directory /home/projects, can set the CDPATH variable to /home/projects


# End-users frequently accessing the subdirectories under their home directory, can set the CDPATH variable to ~ (home directory)


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page47 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page47 Chapter 5: PS1, PS2, PS3, PS4 and PROMPT_COMMAND]47'''
=='''Hack 2. Use cd alias to navigate up the directory effectively'''==




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page47 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page47 Hack 29. PS1 - Default Interaction Prompt]47'''
When you are navigating up a very long directory structure, you may be using cd ..\..\ with multiple ..\’s depending on how many directories you want to go up as shown below.




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page48 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page48 Hack 30. PS2 - Continuation Interactive Prompt]48'''
# mkdir -p /tmp/very/long/directory/structure/that/is/too/deep
# cd /tmp/very/long/directory/structure/that/is/too/deep


# pwd /tmp/very/long/directory/structure/that/is/too/deep


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page49 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page49 Hack 31. PS3 - Prompt used by “select” inside shell script]49'''
# cd ../../../../  




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page50 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page50 Hack 32. PS4 - Used by “set -x” to prefix tracing output]50'''


<nowiki># pwd /tmp/very/long/directory/structure</nowiki>


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page52 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page52 Hack 33. PROMPT_COMMAND]52'''


Instead of executing cd ../../../.. to navigate four levels up, use one of the following three alias methods:


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page53 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page53 Chapter 6: Colorful and Functional Shell Prompt Using PS1 . 53]


'''Method 1: Navigate up the directory using “..n”'''


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page53 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page53 Hack 34. Display username, hostname and basename of directory in]


In the example below, ..4 is used to go up 4 directory level, ..3 to go up 3 directory level, ..2 to go up 2 directory level. Add the following alias to your ~/.bash_profile and re-login.




{| style="border-spacing:0;"
alias ..="cd .." alias ..2="cd ../.."
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page53 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page53 the prompt .......................................................................]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''53'''</div>


|-
alias ..3="cd ../../.." alias ..4="cd ../../../.." alias ..5="cd ../../../../.."
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page53 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page53 Hack 35. Display current time in the prompt .............................]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''53'''</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page54 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page54 Hack 36. Display output of any command in the prompt ...............]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''54'''</div>


|-
<nowiki># cd /tmp/very/long/directory/structure/that/is/too/deep</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page55 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page55 Hack 37. Change foreground color of the prompt........................]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''55'''</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page56 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page56 Hack 38. Change background color of the prompt .......................]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''56'''</div>


|-
<nowiki># </nowiki>..4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page57 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page57 Hack 39. Display multiple colors in the prompt ..........................]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''57'''</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page58 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page58 Hack 40. Change the prompt color using tput ............................]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''58'''</div>


|-
<nowiki>[</nowiki>'''Note:''' use ..4 to go up 4 directory level]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page59 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page59 Hack 41. Create your own prompt using the available codes for PS1]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page59 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page59 variable............................................................................]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''59'''</div>


|-
<nowiki># pwd /tmp/very/long/directory/structure/</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page61 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page61 Hack 42. Use bash shell function inside PS1 variable ...................]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''61'''</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page61 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page61 Hack 43. Use shell script inside PS1 variable .............................]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''61'''</div>


|-
'''Method 2: Navigate up the directory using only dots'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| [file:///tmp/lubq0ytm.tmp/HYPERLINK#page63 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page63 Chapter 7: Archive and Compression..............................]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''63'''</div>


|}
3'''Linux 101 Hacks'''www.thegeekstuff.com


In the example below, ….. (five dots) is used to go up 4 directory level. Typing 5 dots to go up 4 directory structure is really easy to remember, as when you type the first two dots, you are thinking “going up one directory”, after that every additional dot, is to go one level up. So, use …. (four dots) to go up 3 directory level and .. (two dots) to go up 1 directory level. Add the following alias to your ~/.bash_profile and re-login for the ….. (five dots) to work properly.


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page63 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page63 Hack 44. Zip command basics]63'''


alias ..="cd .."


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page65 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page65 Hack 45. Advanced compression using zip command.]65'''


alias ...="cd ../.." alias ....="cd ../../.."


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page66 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page66 Hack 46. Password Protection of Zip files]66'''
alias .....="cd ../../../.." alias ......="cd ../../../../.."




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page66 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page66 Hack 47. Validate a zip archive]66'''
# cd /tmp/very/long/directory/structure/that/is/too/deep


# .....


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page67 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page67 Hack 48. Tar Command Basics]67'''
<nowiki>[</nowiki>'''Note:''' use ..... (five dots) to go up 4 directory level]




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page68 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page68 Hack 49. Combine gzip, bzip2 with tar]68'''
<nowiki># pwd /tmp/very/long/directory/structure/</nowiki>




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page70 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page70 Chapter 8: Command Line History]70'''
'''Method 3: Navigate up the directory using cd followed by consecutive dots'''




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page70 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page70 Hack 50. Display TIMESTAMP in history using HISTTIMEFORMAT]70'''
In the example below, cd….. (cd followed by five dots) is used to go up 4 directory level. Making it 5 dots to go up 4 directory structure is really easy to remember, as when you type the first two dots, you are thinking “going up one directory”, after that every additional dot, is to go one level up. So, use cd…. (cd followed by four dots) to go up 3 directory level and cd… (cd followed by three dots) to go up 2 directory level. Add the following alias to your ~/.bash_profile and re-login for the above cd….. (five dots) to work properly.




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page70 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page70 Hack 51. Search the history using Control+R]70'''
alias cd..="cd .." alias cd...="cd ../.."




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page72 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page72 Hack 52. Repeat previous command quickly using 4 different methods]
alias cd....="cd ../../.." alias cd.....="cd ../../../.."




'''72'''
alias cd......="cd ../../../../.."




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page72 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page72 Hack 53. Execute a specific command from history]72'''
# cd /tmp/very/long/directory/structure/that/is/too/deep


# cd.....


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page73 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page73 Hack 54. Execute previous command that starts with a specific word]
<nowiki>[</nowiki>'''Note:''' use cd..... to go up 4 directory level]




'''73'''
<nowiki># pwd /tmp/very/long/directory/structure</nowiki>




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page73 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page73 Hack 55. Control the total number of lines in the history using]
'''Method 5: Navigate up the directory using cd followed by number'''




'''HISTSIZE73'''
In the example below, cd4 (cd followed by number 4) is used to go up 4 directory level.




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page73 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page73 Hack 56. Change the history file name using HISTFILE]73'''
alias cd1="cd .." alias cd2="cd ../.." alias cd3="cd ../../.."


alias cd4="cd ../../../.." alias cd5="cd ../../../../.."


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page74 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page74 Hack 57. Eliminate the continuous repeated entry from history using]


=='''Hack 3. Perform mkdir and cd using a single command'''==


'''HISTCONTROL74'''


Sometimes when you create a new directory, you may cd to the new directory immediately to perform some work as shown below.


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page75 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page75 Hack 58. Erase duplicates across the whole history using]


# mkdir -p /tmp/subdir1/subdir2/subdir3


'''HISTCONTROL75'''
# cd /tmp/subdir1/subdir2/subdir3


# pwd


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page76 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page76 Hack 59. Force history not to remember a particular command using]
/tmp/subdir1/subdir2/subdir3




'''HISTCONTROL76'''
Wouldn’t it be nice to combine both mkdir and cd in a single command? Add the following to the .bash_profile and re-login.




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page76 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page76 Hack 60. Clear all the previous history using option -c]76'''
$ vi .bash_profile




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page77 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page77 Hack 61. Substitute words from history commands]77'''
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page77 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page77 Hack 62. Substitute a specific argument for a specific command]77'''
Now, perform both mkdir and cd at the same time using a single command as shown below:




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page78 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page78 Hack 63. Disable the usage of history using HISTSIZE]78'''
<nowiki># </nowiki>mkdircd /tmp/subdir1/subdir2/subdir3




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page78 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page78 Hack 64. Ignore specific commands from the history using HISTIGNORE]
<nowiki>[</nowiki>'''Note:''' This creates the directory and cd to it automatically]


<nowiki># pwd /tmp/subdir1/subdir2/subdir3</nowiki>


'''78'''


=='''Hack 4. Use “cd -” to toggle between the last two directories'''==


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page80 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page80 Chapter 9: System Administration Tasks]80'''


You can toggle between the last two current directories using cd - as shown below.


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page80 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page80 Hack 65. Partition using fdisk]80'''


# cd /tmp/very/long/directory/structure/that/is/too/deep


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page82 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page82 Hack 66. Format a partition using mke2fsk]82'''
# cd /tmp/subdir1/subdir2/subdir3


# cd -


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page84 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page84 Hack 67. Mount the partition]84'''4'''Linux 101 Hacks'''www.thegeekstuff.com
# pwd /tmp/very/long/directory/structure/that/is/too/deep


# '''cd - '''


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page84 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page84 Hack 68. Fine tune the partition using tune2fs]84'''
# pwd


/tmp/subdir1/subdir2/subdir3


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page86 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page86 Hack 69. Create a swap file system.]86'''


<nowiki># </nowiki>'''cd -'''


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page87 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page87 Hack 70. Create a new user]87'''


<nowiki># </nowiki>pwd


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page88 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page88 Hack 71. Create a new group and assign to an user]88'''


/tmp/very/long/directory/structure/that/is/too/deep


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page89 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page89 Hack 72. Setup SSH passwordless login in OpenSSH]89'''


=='''Hack 5. Use dirs, pushd and popd to manipulate directory stack'''==


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page91 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page91 Hack 73. Use ssh-copy-id along with ssh-agent]91'''


You can use directory stack to push directories into it and later pop directory from the stack. Following three commands are used in this example.


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page92 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page92 Hack 74. Crontab]92'''


o dirs: Display the directory stack


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page94 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page94 Hack 75. Safe Reboot Of Linux Using Magic SysRq Key]94'''


o pushd: Push directory into the stack


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page97 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page97 Chapter 10: Apachectl and Httpd Examples]97'''


o popd: Pop directory from the stack and cd to it


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page97 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page97 Hack 76. Pass different httpd.conf filename to apachectl]97'''


Dirs will always print the current directory followed by the content of the stack. Even when the directory stack is empty, dirs command will still print only the current directory as shown below.


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page98 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page98 Hack 77. Use a temporary DocumentRoot without modifying]


<nowiki># </nowiki>popd


'''httpd.conf98'''


-bash: popd: directory stack empty


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page99 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page99 Hack 78. Increase the Log Level temporarily]99'''


<nowiki># </nowiki>dirs


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page100 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page100 Hack 79. Display the modules inside Apache]100'''


~


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page101 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page101 Hack 80. Show all accepted directives inside httpd.conf]101'''


<nowiki># pwd /home/ramesh</nowiki>


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page101 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page101 Hack 81. Validate the httpd.conf after making changes]101'''


How to use pushd and popd? Let us first create some temporary directories and push them to the directory stack as shown below.


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page102 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page102 Hack 82. Display the httpd build parameters]102'''


# mkdir /tmp/dir1


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page103 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page103 Hack 83. Load a specific module only on demand]103'''
# mkdir /tmp/dir2


# mkdir /tmp/dir3


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page105 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page105 Chapter 11: Bash Scripting]105'''
# mkdir /tmp/dir4


# cd /tmp/dir1


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page105 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page105 Hack 84. Execution Sequence of .bash_* files]105'''
# pushd .  


# cd /tmp/dir2


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page106 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page106 Hack 85. How to generate random number in bash shell]106'''
# pushd .  


# cd /tmp/dir3


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page107 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page107 Hack 86. Debug a shell script]107'''
# pushd .  


# cd /tmp/dir4


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page108 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page108 Hack 87. Quoting]108'''
# pushd .  




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page110 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page110 Hack 88. Read data file fields inside a shell script]110'''




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page112 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page112 Chapter 12: System Monitoring and Performance]112'''
<nowiki># </nowiki>dirs




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page112 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page112 Hack 89. Free command]112'''
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page113 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page113 Hack 90. Top Command]113'''
<nowiki>[</nowiki>'''Note:''' The first directory (/tmp/dir4) of the dir command output is always the current directory and not the content from the stack.]




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page116 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page116 Hack 91. Ps Command]116'''
At this stage, the directory stack contains the following directories:




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page118 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page118 Hack 92. Df Command]118'''
/tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page119 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page119 Hack 93. Kill Command]119'''
The last directory that was pushed to the stack will be at the top. When you perform popd, it will cd to the top directory entry in the stack and remove it from the stack. As shown above, the last directory that was pushed into the stack is /tmp/dir4. So, when we do a popd, it will cd to the /tmp/dir4 and remove it from the directory stack as shown below.




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page121 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page121 Hack 94. Du Command]121'''5'''Linux 101 Hacks'''www.thegeekstuff.com
<nowiki># </nowiki>popd




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page121 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page121 Hack 95. lsof commands.]121'''
<nowiki># pwd /tmp/dir4</nowiki>


<nowiki>[</nowiki>'''Note''': After the above popd, directory Stack Contains: /tmp/dir3


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page124 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page124 Hack 96. Sar Command]124'''
/tmp/dir2 /tmp/dir1]




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page126 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page126 Hack 97. vmstat Command]126'''
<nowiki># </nowiki>popd




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page128 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page128 Hack 98. Netstat Command]128'''
<nowiki># pwd /tmp/dir3</nowiki>




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page130 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page130 Hack 99. Sysctl Command]130'''
<nowiki>[</nowiki>'''Note''': After the above popd, directory Stack Contains: /tmp/dir2


/tmp/dir1]


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page132 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page132 Hack 100. Nice Command]132'''


<nowiki># </nowiki>popd


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page134 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page134 Hack 101. Renice Command]134'''


<nowiki># pwd /tmp/dir2</nowiki>


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page136 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page136 12 Amazing and Essential Linux Books]136'''


<nowiki>[</nowiki>'''Note''': After the above popd, directory Stack Contains: /tmp/dir1]


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page139 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page139 Extended Reading]139'''


<nowiki># </nowiki>popd


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page140 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page140 Your Feedback and Support]140'''


<nowiki># pwd /tmp/dir1</nowiki>


[file:///tmp/lubq0ytm.tmp/HYPERLINK#page140 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page140 Subscribe to TGS]140'''
<nowiki>[</nowiki>'''Note''': After the above popd, directory Stack is empty!]




[file:///tmp/lubq0ytm.tmp/HYPERLINK#page140 ][file:///tmp/lubq0ytm.tmp/HYPERLINK#page140 Contact TGS]140'''6'''Linux 101 Hacks'''www.thegeekstuff.com
<nowiki># </nowiki>popd




'''Introduction'''
-bash: popd: directory stack empty




''”There are only 10 types of people in the world — those who understand [http://en.wikipedia.org/wiki/Binary_numeral_system ][http://en.wikipedia.org/wiki/Binary_numeral_system binary], those who don’t, and those who understand [http://en.wikipedia.org/wiki/Gray_code ][http://en.wikipedia.org/wiki/Gray_code gray code]”''
=='''Hack 6. Use “shopt -s cdspell” to automatically correct mistyped directory names on cd'''==




'''''— Geek'''''
Use shopt -s cdspell to correct the typos in the cd command automatically as shown below. If you are not good at typing and make lot of mistakes, this will be very helpful.




There are total of 101 hacks in this book that will help you build a strong foundation in Linux. All the hacks in this book are explained with appropriate Linux command examples that are easy to follow.
<nowiki># </nowiki>cd /etc/mall




This book contains 12 chapters. Hacks mentioned in 6 chapters are based on the articles that I’ve already posted on my blog. Hacks mentioned in rest of the 6 chapters are brand new.
-bash: cd: /etc/mall: No such file or directory




I’m Ramesh Natarajan, author of [http://www.thegeekstuff.com/ ][http://www.thegeekstuff.com/ The Geek Stuff][http://www.thegeekstuff.com/  ]blog and this eBook.
# '''shopt -s cdspell '''


[[Image:]]
# cd /etc/mall


# pwd


I have done intensive programming on several languages and C is my favorite. I have done lot of work on the infrastructure side including Linux system administration, DBA, Networking, Hardware and Storage (EMC).
/etc/mail




I have also developed [http://www.passworddragon.com/ ][http://www.passworddragon.com/ Password Dragon][http://www.passworddragon.com/  ]— free, easy and secure password manager that runs on Windows, Linux and Mac.
<nowiki>[</nowiki>'''Note''': By mistake, when I typed mall instead of mail, cd corrected it automatically]




If you have any feedback about this eBook, please use this [http://www.thegeekstuff.com/contact/ ][http://www.thegeekstuff.com/contact/ contact form][http://www.thegeekstuff.com/contact/  ]to get in touch with me.
='''Chapter 2: Date Manipulation'''=




''Copyright © 2009 – Ramesh Natarajan''7'''Linux 101 Hacks'''www.thegeekstuff.com
=='''Hack 7. Set System Date and Time'''==




'''Foreword'''
To change the system date use:




Another collection of hacks? Yes! If you have just completed your first admin course or looking for better ways to get the job done the "Linux 101 Hacks" eBook is a good point to start. These useful tips are concise, well written and easy to read.
<nowiki># </nowiki>date {mmddhhmiyyyy.ss}




Well done - I will recommend this eBook to my students.
o mm – Month o dd – Date




'''--Prof. Dr. Fritz Mehner, FH Südwestfalen, Germany'''
o hh – 24 hour format o mi – Minutes




(Author of several [http://www.vim.org/account/profile.php?user_id=169 ][http://www.vim.org/account/profile.php?user_id=169 Vim plugins], including [http://www.vim.org/scripts/script.php?script_id=365 ][http://www.vim.org/scripts/script.php?script_id=365 bash-support vim plugin])
o yyyy – Year o ss – seconds




'''Version'''
For example, to set system date to Jan 31<sup>st</sup> 2008, 10:19 p.m, 53 seconds




<nowiki># </nowiki>date 013122192009.53


{| style="border-spacing:0;"
| style="border-top:1pt solid #000001;border-bottom:none;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Version'''
| style="border-top:1pt solid #000001;border-bottom:none;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Date'''
| style="border-top:1pt solid #000001;border-bottom:none;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Revisions'''


|-
You can also change system date using set argument as shown below.
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border-top:none;border-bottom:none;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1.0
| style="border-top:none;border-bottom:none;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12-Feb-2009
| style="border-top:none;border-bottom:none;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| First Edition


|-
# date 013122192009.53
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
# date +%Y%m%d -s "20090131"  
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border-top:none;border-bottom:1pt solid #000001;border-left:none;border-right:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
# date -s "01/31/2009 22:19:53"
Download the [http://www.thegeekstuff.com/linux-101-hacks-free-ebook ][http://www.thegeekstuff.com/linux-101-hacks-free-ebook latest version of the book here].8'''Linux 101 Hacks'''www.thegeekstuff.com


=='''Chapter 1: Powerful CD Command Hacks'''==
# date -s "31 JAN 2009 22:19:53"


# date set="31 JAN 2009 22:19:53"


cd is one of the most frequently used commands during a UNIX session. The 6 cd command hacks mentioned in this chapter will boost your productivity instantly and make it easier to navigate the directory structure from command line.
To set the time only:




'''Hack 1. Use CDPATH to define the base directory for cd command'''
<nowiki># </nowiki>date +%T -s "22:19:53"




If you are frequently performing cd to subdirectories of a specific parent directory, you can set the CDPATH to the parent directory and perform cd to the subdirectories without giving the parent directory path as explained below.
<nowiki># </nowiki>date +%T%p -s "10:19:53PM"




<nowiki>[ramesh@dev-db ~]# </nowiki>'''pwd''' /home/ramesh
=='''Hack 8. Set Hardware Date and Time'''==


<nowiki>[ramesh@dev-db </nowiki>~]# '''cd mail'''


Before setting the hardware date and time, make sure the OS date and time is set appropriately as shown in the hack#7.


-bash: cd: mail: No such file or directory


Set the hardware date and time based on the system date as shown below:


<nowiki>[</nowiki>'''Note:''' This is looking for mail directory under current directory]


# hwclock –systohc


<nowiki>[ramesh@dev-db ~]# </nowiki>'''export CDPATH=/etc'''<nowiki> [ramesh@dev-db ~]# </nowiki>'''cd mail'''
# hwclock --systohc –utc


/etc/mail
Use hwclock without any parameter, to view the current hardware date and time:




<nowiki>[</nowiki>'''Note:''' This is looking for mail under /etc and not under current directory]
<nowiki># </nowiki>hwclock




<nowiki>[ramesh@dev-db /etc/mail]# </nowiki>'''pwd''' /etc/mail
Check the clock file to verify whether the system is set for UTC:




To make this change permanent, add export CDPATH=/etc to your ~/.bash_profile9'''Linux 101 Hacks'''www.thegeekstuff.com
<nowiki># </nowiki>cat /etc/sysconfig/clock




Similar to the PATH variable, you can add more than one directory entry in the CDPATH variable, separating them with : , as shown below.
ZONE="America/Los_Angeles"




export CDPATH=.:~:/etc:/var
UTC=false




This hack can be very helpful under the following situations:
ARC=false




# Oracle DBAs frequently working under $ORACLE_HOME, can set the CDPATH variable to the oracle home
=='''Hack 9. Display Current Date and Time in a Specific'''==


# Unix sysadmins frequently working under /etc, can set the CDPATH variable to /etc


# Developers frequently working under project directory /home/projects, can set the CDPATH variable to /home/projects
'''Format'''


# End-users frequently accessing the subdirectories under their home directory, can set the CDPATH variable to ~ (home directory)


'''Hack 2. Use cd alias to navigate up the directory effectively'''
Following are different ways of displaying the current date and time in various formats:


$ date


When you are navigating up a very long directory structure, you may be using cd ..\..\ with multiple ..\’s depending on how many directories you want to go up as shown below.


Thu Jan 1 08:19:23 PST 2009


# mkdir -p /tmp/very/long/directory/structure/that/is/too/deep
# cd /tmp/very/long/directory/structure/that/is/too/deep


# pwd /tmp/very/long/directory/structure/that/is/too/deep
$ date --date="now"


# cd ../../../../


10'''Linux 101 Hacks'''www.thegeekstuff.com
Thu Jan 1 08:20:05 PST 2009




<nowiki># pwd /tmp/very/long/directory/structure</nowiki>
$ date --date="today"




Instead of executing cd ../../../.. to navigate four levels up, use one of the following three alias methods:
Thu Jan 1 08:20:12 PST 2009




'''Method 1: Navigate up the directory using “..n”'''
$ date --date='1970-01-01 00:00:01 UTC +5 hours' +%s 18001




In the example below, ..4 is used to go up 4 directory level, ..3 to go up 3 directory level, ..2 to go up 2 directory level. Add the following alias to your ~/.bash_profile and re-login.
$ date '+Current Date: %m/%d/%y%nCurrent Time:%H:%M:%S' Current Date: 01/01/09




alias ..="cd .." alias ..2="cd ../.."
Current Time:08:21:41


alias ..3="cd ../../.." alias ..4="cd ../../../.." alias ..5="cd ../../../../.."


$ date +"%d-%m-%Y" 01-01-2009


<nowiki># cd /tmp/very/long/directory/structure/that/is/too/deep</nowiki>


$ date +"%d/%m/%Y" 01/01/2009


<nowiki># </nowiki>..4


$ date +"%A,%B %d %Y" Thursday,January 01 2009


<nowiki>[</nowiki>'''Note:''' use ..4 to go up 4 directory level]


Following are the different format options you can pass to the date command:


<nowiki># pwd /tmp/very/long/directory/structure/</nowiki>




'''Method 2: Navigate up the directory using only dots'''
{| style="border-spacing:0;"
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %D
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| date (mm/dd/yy)


In the example below, ….. (five dots) is used to go up 4 directory level. Typing 5 dots to go up 4 directory structure is really easy to remember, as when you type the first two dots, you are thinking “going up one directory”, after that every additional dot, is to go one level up. So, use …. (four dots) to go up 3 directory level and .. (two dots) to go up 1 directory level. Add the following alias to your ~/.bash_profile and re-login for the ….. (five dots) to work properly.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %d
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| day of month (01..31)


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %m
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| month (01..12)


alias ..="cd .."11'''Linux 101 Hacks'''www.thegeekstuff.com
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %y last two digits of year (00..99)


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %a
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| locale’s abbreviated weekday name (Sun..Sat)


alias ...="cd ../.." alias ....="cd ../../.."
|}
# %A locale’s full weekday name, variable length (Sunday..Saturday)


alias .....="cd ../../../.." alias ......="cd ../../../../.."
o %b locale’s abbreviated month name (Jan..Dec)




# cd /tmp/very/long/directory/structure/that/is/too/deep
# %B locale’s full month name, variable length (January..December)


# .....


<nowiki>[</nowiki>'''Note:''' use ..... (five dots) to go up 4 directory level]
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %H
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| hour (00..23)


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %I
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| hour (01..12)


<nowiki># pwd /tmp/very/long/directory/structure/</nowiki>
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %Y
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| year (1970…)


|}


'''Method 3: Navigate up the directory using cd followed by consecutive dots'''
=='''Hack 10. Display Past Date and Time'''==




In the example below, cd….. (cd followed by five dots) is used to go up 4 directory level. Making it 5 dots to go up 4 directory structure is really easy to remember, as when you type the first two dots, you are thinking “going up one directory”, after that every additional dot, is to go one level up. So, use cd…. (cd followed by four dots) to go up 3 directory level and cd… (cd followed by three dots) to go up 2 directory level. Add the following alias to your ~/.bash_profile and re-login for the above cd….. (five dots) to work properly.
Following are various ways to display a past date and time:




alias cd..="cd .." alias cd...="cd ../.."
$ date --date='3 seconds ago' Thu Jan 1 08:27:00 PST 2009




alias cd....="cd ../../.." alias cd.....="cd ../../../.."
$ date --date="1 day ago" Wed Dec 31 08:27:13 PST 2008


$ date --date="1 days ago" Wed Dec 31 08:27:18 PST 2008


alias cd......="cd ../../../../.."
$ date --date="1 month ago" Mon Dec 1 08:27:23 PST 2008




# cd /tmp/very/long/directory/structure/that/is/too/deep
$ date --date="1 year ago" Tue Jan 1 08:27:28 PST 2008


# cd.....


<nowiki>[</nowiki>'''Note:''' use cd..... to go up 4 directory level]
$ date --date="yesterday" Wed Dec 31 08:27:34 PST 2008




<nowiki># pwd /tmp/very/long/directory/structure</nowiki>12'''Linux 101 Hacks'''www.thegeekstuff.com
$ date --date="10 months 2 day ago" Thu Feb 28 08:27:41 PST 2008




'''Method 5: Navigate up the directory using cd followed by number'''
=='''Hack 11. Display Future Date and Time'''==




In the example below, cd4 (cd followed by number 4) is used to go up 4 directory level.
Following examples shows how to display a future date and time.




alias cd1="cd .." alias cd2="cd ../.." alias cd3="cd ../../.."
$ date


alias cd4="cd ../../../.." alias cd5="cd ../../../../.."


Thu Jan 1 08:30:07 PST 2009


'''Hack 3. Perform mkdir and cd using a single command'''


$ date --date='3 seconds' Thu Jan 1 08:30:12 PST 2009


Sometimes when you create a new directory, you may cd to the new directory immediately to perform some work as shown below.


$ date --date='4 hours'


# mkdir -p /tmp/subdir1/subdir2/subdir3


# cd /tmp/subdir1/subdir2/subdir3
Thu Jan 1 12:30:17 PST 2009


# pwd


/tmp/subdir1/subdir2/subdir3
$ date --date='tomorrow' Fri Jan 2 08:30:25 PST 2009




Wouldn’t it be nice to combine both mkdir and cd in a single command? Add the following to the .bash_profile and re-login.
$ date --date="1 day"




$ vi .bash_profile
Fri Jan 2 08:30:31 PST 2009




function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
$ date --date="1 days"




Now, perform both mkdir and cd at the same time using a single command as shown below:13'''Linux 101 Hacks'''www.thegeekstuff.com
Fri Jan 2 08:30:38 PST 2009




<nowiki># </nowiki>mkdircd /tmp/subdir1/subdir2/subdir3
$ date --date="2 days"




<nowiki>[</nowiki>'''Note:''' This creates the directory and cd to it automatically]
Sat Jan 3 08:30:43 PST 2009


<nowiki># pwd /tmp/subdir1/subdir2/subdir3</nowiki>


$ date --date='1 month'


'''Hack 4. Use “cd -” to toggle between the last two directories'''


Sun Feb 1 08:30:48 PST 2009


You can toggle between the last two current directories using cd - as shown below.


$ date --date='1 week'


# cd /tmp/very/long/directory/structure/that/is/too/deep


# cd /tmp/subdir1/subdir2/subdir3
Thu Jan 8 08:30:53 PST 2009


# cd -


# pwd /tmp/very/long/directory/structure/that/is/too/deep
$ date --date="2 months" Sun Mar 1 08:30:58 PST 2009


# '''cd - '''


# pwd
$ date --date="2 years"


/tmp/subdir1/subdir2/subdir3


Sat Jan 1 08:31:03 PST 2011


<nowiki># </nowiki>'''cd -'''


$ date --date="next day" Fri Jan 2 08:31:10 PST 2009


<nowiki># </nowiki>pwd


$ date --date="-1 days ago" Fri Jan 2 08:31:15 PST 2009


/tmp/very/long/directory/structure/that/is/too/deep


$ date --date="this Wednesday" Wed Jan 7 00:00:00 PST 2009


'''Hack 5. Use dirs, pushd and popd to manipulate directory stack'''
='''Chapter 3: SSH Client Commands'''=




You can use directory stack to push directories into it and later pop directory from the stack. Following three commands are used in this example.14'''Linux 101 Hacks'''www.thegeekstuff.com
=='''Hack 12. Identify SSH Client Version'''==




o dirs: Display the directory stack
Sometimes it may be necessary to identify the SSH client that you are currently running and it’s corresponding version number. Use ssh –V to identify the version number. Please note that Linux comes with OpenSSH.




o pushd: Push directory into the stack
The following example indicates that this particular system is using OpenSSH:




o popd: Pop directory from the stack and cd to it
$ ssh -V




Dirs will always print the current directory followed by the content of the stack. Even when the directory stack is empty, dirs command will still print only the current directory as shown below.
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003




<nowiki># </nowiki>popd
The following example indicates that this particular system is using SSH2:




-bash: popd: directory stack empty
$ ssh -V




<nowiki># </nowiki>dirs
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu




~
=='''Hack 13. Login to Remote Host using SSH'''==




<nowiki># pwd /home/ramesh</nowiki>
The First time when you login to a remotehost from a localhost, it will display the host key not found message and you can give “yes” to continue. The host key of the remote host will be added under .ssh2/hostkeys directory of your home directory, as shown below.




How to use pushd and popd? Let us first create some temporary directories and push them to the directory stack as shown below.
localhost$ '''ssh -l jsmith remotehost.example.com'''




# mkdir /tmp/dir1
Host key not found from database. Key fingerprint:


# mkdir /tmp/dir2


# mkdir /tmp/dir3
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum


# mkdir /tmp/dir4
You can get a public key’s fingerprint by running % ssh-keygen -F publickey.pub


# cd /tmp/dir1


# pushd .  
on the keyfile.


# cd /tmp/dir2


# pushd .
Are you sure you want to continue connecting (yes/no)? '''Yes'''


# cd /tmp/dir3


# pushd .  
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:06:50 -0700


# cd /tmp/dir4


# pushd .  
jsmith@remotehost.example.com password:


15'''Linux 101 Hacks'''www.thegeekstuff.com


remotehost.example.com$


<nowiki># </nowiki>dirs


The Second time when you login to the remote host from the localhost, it will prompt only for the password as the remote host key is already added to the known hosts list of the ssh client.


/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1


localhost$ '''ssh -l jsmith remotehost.example.com''' jsmith@remotehost.example.com password:


<nowiki>[</nowiki>'''Note:''' The first directory (/tmp/dir4) of the dir command output is always the current directory and not the content from the stack.]


remotehost.example.com$


At this stage, the directory stack contains the following directories:


For some reason, if the host key of the remote host is changed after you logged in for the first time, you may get a warning message as shown below. This could be because of various reasons such as:


/tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1


o Sysadmin upgraded/reinstalled the SSH server on the remote host


The last directory that was pushed to the stack will be at the top. When you perform popd, it will cd to the top directory entry in the stack and remove it from the stack. As shown above, the last directory that was pushed into the stack is /tmp/dir4. So, when we do a popd, it will cd to the /tmp/dir4 and remove it from the directory stack as shown below.


o Someone is doing malicious activity etc.,


<nowiki># </nowiki>popd


The best possible action to take before saying “yes” to the message below, is to call your sysadmin and identify why you got the host key changed message and verify whether it is the correct host key or not.


<nowiki># pwd /tmp/dir4</nowiki>


<nowiki>[</nowiki>'''Note''': After the above popd, directory Stack Contains: /tmp/dir3
localhost$ '''ssh -l jsmith remotehost.example.com'''


/tmp/dir2 /tmp/dir1]




<nowiki># </nowiki>popd
{| style="border-spacing:0;"
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| @
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| WARNING: HOST IDENTIFICATION
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| HAS CHANGED!
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">@</div>


<nowiki># pwd /tmp/dir3</nowiki>
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| IT IS POSSIBLE THAT SOMEONE IS
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| DOING
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SOMETHING NASTY!
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


<nowiki>[</nowiki>'''Note''': After the above popd, directory Stack Contains: /tmp/dir2
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Someone
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| could be eavesdropping
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| on you right now (man-in-the-


/tmp/dir1]
|}




<nowiki># </nowiki>popd16'''Linux 101 Hacks'''www.thegeekstuff.com




<nowiki># pwd /tmp/dir2</nowiki>
middle attack)!




<nowiki>[</nowiki>'''Note''': After the above popd, directory Stack Contains: /tmp/dir1]
It is also possible that the host key has just been changed. Please contact your system administrator.




<nowiki># </nowiki>popd
Add correct host key to “/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pu b”




<nowiki># pwd /tmp/dir1</nowiki>
to get rid of this message. Received server key’s fingerprint:


<nowiki>[</nowiki>'''Note''': After the above popd, directory Stack is empty!]


xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum


<nowiki># </nowiki>popd


You can get a public key’s fingerprint by running % ssh-keygen -F publickey.pub


-bash: popd: directory stack empty


on the keyfile.


'''Hack 6. Use “shopt -s cdspell” to automatically correct mistyped directory names on cd'''


Agent forwarding is disabled to avoid attacks by corrupted servers.


Use shopt -s cdspell to correct the typos in the cd command automatically as shown below. If you are not good at typing and make lot of mistakes, this will be very helpful.


Are you sure you want to continue connecting (yes/no)? '''yes'''


<nowiki># </nowiki>cd /etc/mall


Do you want to change the host key on disk (yes/no)? '''yes'''


-bash: cd: /etc/mall: No such file or directory


Agent forwarding re-enabled. Host key saved to


# '''shopt -s cdspell '''


# cd /etc/mall
/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:17:31 -0700


# pwd


/etc/mail
jsmith @remotehost.example.com’s password:




<nowiki>[</nowiki>'''Note''': By mistake, when I typed mall instead of mail, cd corrected it automatically]17'''Linux 101 Hacks'''www.thegeekstuff.com
remotehost$




'''Chapter 2: Date Manipulation'''
'''Hack 14. Debug SSH Client Session'''




'''Hack 7. Set System Date and Time'''
Sometimes it is necessary to view debug messages to troubleshoot any SSH connection issues. pass -v (lowercase v) option to the ssh as shown below to view the ssh debug messages.




To change the system date use:
'''Example without SSH client debug message:'''




<nowiki># </nowiki>date {mmddhhmiyyyy.ss}
localhost$ '''ssh -l jsmith remotehost.example.com'''




o mm – Month o dd – Date
warning: Connecting to remotehost.example.com failed: No address associated to the name


'''Example with SSH client debug message:'''


o hh – 24 hour format o mi – Minutes


locaclhost$ '''ssh -v -l jsmith remotehost.example.com'''


o yyyy – Year o ss – seconds


debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext: Metaconfig parsing stopped at line 3.


For example, to set system date to Jan 31<sup>st</sup> 2008, 10:19 p.m, 53 seconds


debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose: Setting variable ‘VerboseMode’ to ‘FALSE’.


<nowiki># </nowiki>date 013122192009.53


debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.


You can also change system date using set argument as shown below.


debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for ‘/home/jsmith/.ssh2/ssh2_config’)


# date 013122192009.53


# date +%Y%m%d -s "20090131"
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)


# date -s "01/31/2009 22:19:53"
warning: Connecting to remotehost.example.com failed: No address associated to the name


# date -s "31 JAN 2009 22:19:53"


# date set="31 JAN 2009 22:19:53"
=='''Hack 15. Toggle SSH Session using SSH Escape Character'''==


To set the time only:


When you’ve logged on to the remotehost using ssh from the localhost, you may want to come back to the localhost to perform some activity and go back to remote host again. In this case, you don’t need to disconnect the ssh session to the remote host. Instead, follow the steps below.


<nowiki># </nowiki>date +%T -s "22:19:53"18'''Linux 101 Hacks'''www.thegeekstuff.com


# Login to remotehost from localhost: localhost$ ssh -l jsmith remotehost


<nowiki># </nowiki>date +%T%p -s "10:19:53PM"




'''Hack 8. Set Hardware Date and Time'''


# Now you are connected to the remotehost: remotehost$


Before setting the hardware date and time, make sure the OS date and time is set appropriately as shown in the hack#7.
# To come back to the localhost temporarily, type the escape character ~ and Control-Z.  


<nowiki>When you type ~ you will not see that immediately on the screen until you press <Control-Z> and press enter. So, on the remotehost in a new line enter the following key strokes for the below to work: ~<Control-Z></nowiki>


Set the hardware date and time based on the system date as shown below:


remotehost$ '''~^Z'''


# hwclock –systohc


# hwclock --systohc –utc
<nowiki>[1]+ </nowiki>Stopped ssh -l jsmith remotehost


Use hwclock without any parameter, to view the current hardware date and time:


localhost$


<nowiki># </nowiki>hwclock


4. Now you are back to the localhost and the ssh remotehost client session runs as a typical UNIX background job, which you can check as shown below:


Check the clock file to verify whether the system is set for UTC:


localhost$ '''jobs'''


<nowiki># </nowiki>cat /etc/sysconfig/clock


<nowiki>[1]+ </nowiki>Stopped ssh -l jsmith remotehost


ZONE="America/Los_Angeles"


5. You can go back to the remote host ssh without entering the password again by bringing the background ssh remotehost session job to foreground on the localhost.


UTC=false


localhost$ '''fg %1'''


ARC=false


ssh -l jsmith remotehost


'''Hack 9. Display Current Date and Time in a Specific'''


remotehost$


'''Format'''


=='''Hack 16. SSH Session Statistics using SSH Escape Character'''==


Following are different ways of displaying the current date and time in various formats:19'''Linux 101 Hacks'''www.thegeekstuff.com


To get some useful statistics about the current ssh session, do the following. This works only on SSH2 client.


$ date


# Login to remotehost from localhost. localhost$ ssh -l jsmith remotehost


Thu Jan 1 08:19:23 PST 2009
# On the remotehost, type ssh escape character ~ followed by s as shown below. This will display lot of useful statistics about the current SSH connection.


remotehost$ <nowiki>[</nowiki>'''Note:''' The ~s is not visible on the command line when you type.]


$ date --date="now"
remote host: remotehost local host: localhost


remote version: SSH-1.99-OpenSSH_3.9p1 local version: SSH-2.0-3.2.9.1 SSH Secure


Thu Jan 1 08:20:05 PST 2009


Shell (non-commercial) compressed bytes in: 1506


$ date --date="today"


uncompressed bytes in: 1622 compressed bytes out: 4997 uncompressed bytes out: 5118 packets in: 15


Thu Jan 1 08:20:12 PST 2009


packets out: 24 rekeys: 0 Algorithms:


$ date --date='1970-01-01 00:00:01 UTC +5 hours' +%s 18001
Chosen key exchange algorithm: diffie-hellman-group1-sha1


Chosen host key algorithm: ssh-dss


$ date '+Current Date: %m/%d/%y%nCurrent Time:%H:%M:%S' Current Date: 01/01/09


Common host key algorithms: ssh-dss,ssh-rsa Algorithms client to server:


Current Time:08:21:41
Cipher: aes128-cbc MAC: hmac-sha1 Compression: zlib




$ date +"%d-%m-%Y" 01-01-2009
Algorithms server to client:




$ date +"%d/%m/%Y" 01/01/2009
Cipher: aes128-cbc




$ date +"%A,%B %d %Y" Thursday,January 01 2009
MAC: hmac-sha1




Following are the different format options you can pass to the date command:
Compression: zlib




localhost$


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %D
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| date (mm/dd/yy)


|-
'''Additional SSH Info'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %d
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| day of month (01..31)


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %m
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| month (01..12)


|-
On a side note, to setup SSH key based authentication, refer [http://www.thegeekstuff.com/2008/06/perform-ssh-and-scp-without-entering-password-on-openssh/ ][http://www.thegeekstuff.com/2008/06/perform-ssh-and-scp-without-entering-password-on-openssh/ openSSH][http://www.thegeekstuff.com/2008/06/perform-ssh-and-scp-without-entering-password-on-openssh/ ]and [http://www.thegeekstuff.com/2008/06/comprehensive-guide-for-ssh2-key-based-authentication-setup/ ][http://www.thegeekstuff.com/2008/06/comprehensive-guide-for-ssh2-key-based-authentication-setup/ SSH2 ]tutorials.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %y last two digits of year (00..99)


|-
='''Chapter 4: Essential Linux Commands'''=
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %a
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| locale’s abbreviated weekday name (Sun..Sat)


|}
# %A locale’s full weekday name, variable length (Sunday..Saturday)


o %b locale’s abbreviated month name (Jan..Dec)20'''Linux 101 Hacks'''www.thegeekstuff.com
=='''Hack 17. Grep Command'''==




# %B locale’s full month name, variable length (January..December)
grep command is used to search files for a specific text. This is incredibly powerful command with lot of options.




{| style="border-spacing:0;"
Syntax: grep <nowiki>[options] </nowiki>pattern <nowiki>[files]</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %H
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| hour (00..23)


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %I
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| hour (01..12)


|-
'''How can I find all lines matching a specific keyword on a file?'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %Y
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| year (1970…)


|}
'''Hack 10. Display Past Date and Time'''


In this example, grep looks for the text John inside /etc/passwd file and displays all the matching lines.


Following are various ways to display a past date and time:


<nowiki># </nowiki>grep John /etc/passwd


$ date --date='3 seconds ago' Thu Jan 1 08:27:00 PST 2009


jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash


$ date --date="1 day ago" Wed Dec 31 08:27:13 PST 2008


$ date --date="1 days ago" Wed Dec 31 08:27:18 PST 2008
Option -v, will display all the lines except the match. In the example below, it displays all the records from /etc/password that doesn't match John.


$ date --date="1 month ago" Mon Dec 1 08:27:23 PST 2008


'''Note: '''There are several lines in the /etc/password that doesn’t contain the''' '''word John. Only the first line of the output is shown below.


$ date --date="1 year ago" Tue Jan 1 08:27:28 PST 2008


<nowiki># </nowiki>grep -v John /etc/passwd


$ date --date="yesterday" Wed Dec 31 08:27:34 PST 2008


jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash


$ date --date="10 months 2 day ago" Thu Feb 28 08:27:41 PST 200821'''Linux 101 Hacks'''www.thegeekstuff.com


'''How many lines matched the text pattern in a particular file?'''


'''Hack 11. Display Future Date and Time'''


In the example below, it displays the total number of lines that contains the text John in /etc/passwd file.


Following examples shows how to display a future date and time.
<nowiki># grep -c John /etc/passwd 2</nowiki>




$ date
You can also get the total number of lines that did not match the specific pattern by passing option -cv.




Thu Jan 1 08:30:07 PST 2009
<nowiki># grep -cv John /etc/passwd 39</nowiki>




$ date --date='3 seconds' Thu Jan 1 08:30:12 PST 2009
'''How to search a text by ignoring the case?'''




$ date --date='4 hours'
Pass the option -i (ignore case), which will ignore the case while searching.




Thu Jan 1 12:30:17 PST 2009
<nowiki># </nowiki>grep -i john /etc/passwd




$ date --date='tomorrow' Fri Jan 2 08:30:25 PST 2009
jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash




$ date --date="1 day"
'''How do I search all subdirectories for a text matching a specific pattern?'''




Fri Jan 2 08:30:31 PST 2009
Use option -r (recursive) for this purpose. In the example below, it will search for the text "John" by ignoring the case inside all the subdirectories under /home/users.




$ date --date="1 days"
This will display the output in the format of "filename: line that matching the pattern". You can also pass the option -l, which will display only the name of the file that matches the pattern.




Fri Jan 2 08:30:38 PST 2009
<nowiki># </nowiki>grep -ri john /home/users




$ date --date="2 days"
/home/users/subdir1/letter.txt:John, Thanks for your contribution.


/home/users/name_list.txt:John Smith


Sat Jan 3 08:30:43 PST 2009
/home/users/name_list.txt:John Doe




$ date --date='1 month'
<nowiki># </nowiki>grep -ril john /root




Sun Feb 1 08:30:48 PST 2009
/home/users/subdir1/letter.txt /home/users/name_list.txt




$ date --date='1 week'
=='''Hack 18. Find Command'''==




Thu Jan 8 08:30:53 PST 2009
find is frequently used command to find files in the UNIX filesystem based on numerous conditions. Let us review some practice examples of find command.




$ date --date="2 months" Sun Mar 1 08:30:58 PST 2009
Syntax: find <nowiki>[pathnames] </nowiki><nowiki>[conditions]</nowiki>




$ date --date="2 years"
'''How to find files containing a specific word in its name?'''




Sat Jan 1 08:31:03 PST 2011
The following command looks for all the files under /etc directory with mail in the filename.




$ date --date="next day" Fri Jan 2 08:31:10 PST 200922'''Linux 101 Hacks'''www.thegeekstuff.com
<nowiki># </nowiki>find /etc -name "*mail*"




$ date --date="-1 days ago" Fri Jan 2 08:31:15 PST 2009
'''How to find all the files greater than certain size?'''




$ date --date="this Wednesday" Wed Jan 7 00:00:00 PST 200923'''Linux 101 Hacks'''www.thegeekstuff.com
The following command will list all the files in the system greater than 100MB.




'''Chapter 3: SSH Client Commands'''
<nowiki># </nowiki>find / -type f -size +100M




'''Hack 12. Identify SSH Client Version'''
'''How to find files that are not modified in the last x number of days?'''




Sometimes it may be necessary to identify the SSH client that you are currently running and it’s corresponding version number. Use ssh –V to identify the version number. Please note that Linux comes with OpenSSH.
The following command will list all the files that were modified more than 60 days ago under the current directory.




The following example indicates that this particular system is using OpenSSH:
<nowiki># </nowiki>find . -mtime +60




$ ssh -V
'''How to find files that are modified in the last x number of days?'''




OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
The following command will list all the files that were modified in the last two days under the current directory.




The following example indicates that this particular system is using SSH2:
<nowiki># </nowiki>find . –mtime -2




$ ssh -V
'''How to delete all the archive files with extension *.tar.gz and greater than 100MB?'''




ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
Please be careful while executing the following command as you don’t want to delete the files by mistake. The best practice is to execute the same command with ls –l to make sure you know which files will get deleted when you execute the command with rm.




'''Hack 13. Login to Remote Host using SSH'''
# find / -type f -name <nowiki>*.tar.gz </nowiki>-size +100M -exec ls -l {} \;


# find / -type f -name <nowiki>*.tar.gz </nowiki>-size +100M -exec rm -f {} \;


The First time when you login to a remotehost from a localhost, it will display the host key not found message and you can give “yes” to continue. The host key of the remote host will be added under .ssh2/hostkeys directory of your home directory, as shown below.
'''How to archive all the files that are not modified in the last x number of days?'''




localhost$ '''ssh -l jsmith remotehost.example.com'''
The following command finds all the files not modified in the last 60 days under /home/jsmith directory and creates an archive files under /tmp in the format of ddmmyyyy_archive.tar.
 


<nowiki># find /home/jsmith -type f -mtime +60 | xargs tar -cvf /tmp/`date '+%d%m%Y'_archive.tar`</nowiki>


Host key not found from database. Key fingerprint:


On a side note, you can perform lot of file related activities (including finding files) using [http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ ][http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ midnight commander GUI], a powerful text based file manager for Unix.


xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum


You can get a public key’s fingerprint by running % ssh-keygen -F publickey.pub24'''Linux 101 Hacks'''www.thegeekstuff.com
=='''Hack 19. Suppress Standard Output and Error'''==




on the keyfile.
'''Message'''




Are you sure you want to continue connecting (yes/no)? '''Yes'''
Sometime while debugging a shell script, you may not want to see either the standard output or standard error message. Use /dev/null as shown below for suppressing the output.




Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:06:50 -0700
'''Suppress standard output using > /dev/null'''




jsmith@remotehost.example.com password:
This will be very helpful when you are debugging shell scripts, where you don’t want to display the echo statement and interested in only looking at the error messages.




remotehost.example.com$
# cat file.txt > /dev/null


# ./shell-script.sh > /dev/null


The Second time when you login to the remote host from the localhost, it will prompt only for the password as the remote host key is already added to the known hosts list of the ssh client.
'''Suppress standard error using 2> /dev/null'''




localhost$ '''ssh -l jsmith remotehost.example.com''' jsmith@remotehost.example.com password:
This is also helpful when you are interested in viewing only the standard output and don’t want to view the error messages.




remotehost.example.com$
# cat invalid-file-name.txt 2> /dev/null


# ./shell-script.sh 2> /dev/null


For some reason, if the host key of the remote host is changed after you logged in for the first time, you may get a warning message as shown below. This could be because of various reasons such as:
=='''Hack 20. Join Command'''==




o Sysadmin upgraded/reinstalled the SSH server on the remote host
Join command combines lines from two files based on a common field.




o Someone is doing malicious activity etc.,
In the example below, we have two files – employee.txt and salary.txt. Both have employee-id as common field. So, we can use join command to combine


the data from these two files using employee-id as shown below.


The best possible action to take before saying “yes” to the message below, is to call your sysadmin and identify why you got the host key changed message and verify whether it is the correct host key or not.


$ cat employee.txt


localhost$ '''ssh -l jsmith remotehost.example.com'''


100 Jason Smith




{| style="border-spacing:0;"
200 John Doe
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| @
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| WARNING: HOST IDENTIFICATION
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| HAS CHANGED!
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">@</div>


|-
300 Sanjay Gupta
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| IT IS POSSIBLE THAT SOMEONE IS
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| DOING
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SOMETHING NASTY!
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
400 Ashok Sharma
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Someone
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| could be eavesdropping
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| on you right now (man-in-the-


|}
25'''Linux 101 Hacks'''www.thegeekstuff.com


$ cat bonus.txt


middle attack)!


100 $5,000


It is also possible that the host key has just been changed. Please contact your system administrator.


200 $500


Add correct host key to “/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pu b”


300 $3,000


to get rid of this message. Received server key’s fingerprint:


400 $1,250


xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum


$ '''join employee.txt bonus.txt'''


You can get a public key’s fingerprint by running % ssh-keygen -F publickey.pub


100 Jason Smith $5,000


on the keyfile.


200 John Doe $500


Agent forwarding is disabled to avoid attacks by corrupted servers.


300 Sanjay Gupta $3,000


Are you sure you want to continue connecting (yes/no)? '''yes'''


400 Ashok Sharma $1,250


Do you want to change the host key on disk (yes/no)? '''yes'''


=='''Hack 21. Change the Case'''==


Agent forwarding re-enabled. Host key saved to


'''Convert a file to all upper-case'''


/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:17:31 -0700


$ cat employee.txt


jsmith @remotehost.example.com’s password:


100 Jason Smith


remotehost$


200 John Doe


'''Hack 14. Debug SSH Client Session'''


300 Sanjay Gupta


Sometimes it is necessary to view debug messages to troubleshoot any SSH connection issues. pass -v (lowercase v) option to the ssh as shown below to view the ssh debug messages.


400 Ashok Sharma


'''Example without SSH client debug message:'''


$ '''tr a-z A-Z <nowiki>< </nowiki>employee.txt'''


localhost$ '''ssh -l jsmith remotehost.example.com'''


100 JASON SMITH


warning: Connecting to remotehost.example.com failed: No address associated to the name26'''Linux 101 Hacks'''www.thegeekstuff.com


200 JOHN DOE


'''Example with SSH client debug message:'''


300 SANJAY GUPTA


locaclhost$ '''ssh -v -l jsmith remotehost.example.com'''


400 ASHOK SHARMA


debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext: Metaconfig parsing stopped at line 3.


'''Convert a file to all lower-case'''


debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose: Setting variable ‘VerboseMode’ to ‘FALSE’.


$ cat department.txt


debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.


100 FINANCE


debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for ‘/home/jsmith/.ssh2/ssh2_config’)


200 MARKETING


debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)


warning: Connecting to remotehost.example.com failed: No address associated to the name
300 PRODUCT DEVELOPMENT




'''Hack 15. Toggle SSH Session using SSH Escape Character'''
400 SALES




When you’ve logged on to the remotehost using ssh from the localhost, you may want to come back to the localhost to perform some activity and go back to remote host again. In this case, you don’t need to disconnect the ssh session to the remote host. Instead, follow the steps below.
$ '''tr A-Z a-z <nowiki>< </nowiki>department.txt'''




# Login to remotehost from localhost: localhost$ ssh -l jsmith remotehost
100 finance


27'''Linux 101 Hacks'''www.thegeekstuff.com


200 marketing


# Now you are connected to the remotehost: remotehost$


# To come back to the localhost temporarily, type the escape character ~ and Control-Z.
300 product development


<nowiki>When you type ~ you will not see that immediately on the screen until you press <Control-Z> and press enter. So, on the remotehost in a new line enter the following key strokes for the below to work: ~<Control-Z></nowiki>


400 sales


remotehost$ '''~^Z'''


=='''Hack 22. Xargs Command'''==


<nowiki>[1]+ </nowiki>Stopped ssh -l jsmith remotehost


xargs is a very powerful command that takes output of a command and pass it as argument of another command. Following are some practical examples on how to use xargs effectively.


localhost$


1. When you are trying to delete too many files using rm, you may get error message: /bin/rm Argument list too long – Linux. Use xargs to avoid this problem.


4. Now you are back to the localhost and the ssh remotehost client session runs as a typical UNIX background job, which you can check as shown below:


find ~ -name ‘*.log’ -print0 | xargs -0 rm -f


localhost$ '''jobs'''


2. Get a list of all the *.conf file under /etc/. There are different ways to get the same result. Following example is only to demonstrate the use of xargs. The output of the find command in this example is passed to the ls –l one by one using xargs.


<nowiki>[1]+ </nowiki>Stopped ssh -l jsmith remotehost


#
## find /etc -name "*.conf" | xargs ls –l


5. You can go back to the remote host ssh without entering the password again by bringing the background ssh remotehost session job to foreground on the localhost.
# If you have a file with list of URLs that you would like to download, you can use xargs as shown below.  


#
## cat url-list.txt | xargs wget –c


localhost$ '''fg %1'''
# Find out all the jpg images and archive it.


#
## find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz


ssh -l jsmith remotehost
# Copy all the images to an external hard-drive.


#
## ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory


remotehost$28'''Linux 101 Hacks'''www.thegeekstuff.com
=='''Hack 23. Sort Command'''==




'''Hack 16. SSH Session Statistics using SSH Escape Character'''
Sort command sorts the lines of a text file. Following are several practical examples on how to use the sort command based on the following sample text file that has employee information in the format:




To get some useful statistics about the current ssh session, do the following. This works only on SSH2 client.
employee_name:employee_id:department_name.




# Login to remotehost from localhost. localhost$ ssh -l jsmith remotehost
$ '''cat names.txt'''


# On the remotehost, type ssh escape character ~ followed by s as shown below. This will display lot of useful statistics about the current SSH connection.


remotehost$ <nowiki>[</nowiki>'''Note:''' The ~s is not visible on the command line when you type.]
Emma Thomas:100:Marketing


remote host: remotehost local host: localhost


remote version: SSH-1.99-OpenSSH_3.9p1 local version: SSH-2.0-3.2.9.1 SSH Secure
Alex Jason:200:Sales




Shell (non-commercial) compressed bytes in: 1506
Madison Randy:300:Product Development




uncompressed bytes in: 1622 compressed bytes out: 4997 uncompressed bytes out: 5118 packets in: 15
Sanjay Gupta:400:Support




packets out: 24 rekeys: 0 Algorithms:
Nisha Singh:500:Sales


Chosen key exchange algorithm: diffie-hellman-group1-sha1
'''Sort a text file in ascending order'''


Chosen host key algorithm: ssh-dss


$ '''sort names.txt'''


Common host key algorithms: ssh-dss,ssh-rsa Algorithms client to server:


Cipher: aes128-cbc MAC: hmac-sha1 Compression: zlib29'''Linux 101 Hacks'''www.thegeekstuff.com
Alex Jason:200:Sales




Algorithms server to client:
Emma Thomas:100:Marketing




Cipher: aes128-cbc
Madison Randy:300:Product Development




MAC: hmac-sha1
Nisha Singh:500:Sales




Compression: zlib
Sanjay Gupta:400:Support




localhost$
'''Sort a text file in descending order'''




'''Additional SSH Info'''
$ '''sort -r names.txt'''




On a side note, to setup SSH key based authentication, refer [http://www.thegeekstuff.com/2008/06/perform-ssh-and-scp-without-entering-password-on-openssh/ ][http://www.thegeekstuff.com/2008/06/perform-ssh-and-scp-without-entering-password-on-openssh/ openSSH][http://www.thegeekstuff.com/2008/06/perform-ssh-and-scp-without-entering-password-on-openssh/  ]and [http://www.thegeekstuff.com/2008/06/comprehensive-guide-for-ssh2-key-based-authentication-setup/ ][http://www.thegeekstuff.com/2008/06/comprehensive-guide-for-ssh2-key-based-authentication-setup/ SSH2 ]tutorials.30'''Linux 101 Hacks'''www.thegeekstuff.com
Sanjay Gupta:400:Support




'''Chapter 4: Essential Linux Commands'''
Nisha Singh:500:Sales




'''Hack 17. Grep Command'''
Madison Randy:300:Product Development




grep command is used to search files for a specific text. This is incredibly powerful command with lot of options.
Emma Thomas:100:Marketing




Syntax: grep <nowiki>[options] </nowiki>pattern <nowiki>[files]</nowiki>
Alex Jason:200:Sales




'''How can I find all lines matching a specific keyword on a file?'''
'''Sort a colon delimited text file on 2<sup>nd</sup> field (employee_id)'''




In this example, grep looks for the text John inside /etc/passwd file and displays all the matching lines.
$ '''sort -t: -k 2 names.txt'''




<nowiki># </nowiki>grep John /etc/passwd
Emma Thomas:100:Marketing




jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash
Alex Jason:200:Sales




Option -v, will display all the lines except the match. In the example below, it displays all the records from /etc/password that doesn't match John.
Madison Randy:300:Product Development




'''Note: '''There are several lines in the /etc/password that doesn’t contain the''' '''word John. Only the first line of the output is shown below.
Sanjay Gupta:400:Support




<nowiki># </nowiki>grep -v John /etc/passwd
Nisha Singh:500:Sales




jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash
'''Sort a tab delimited text file on 3rd field (department_name) and suppress duplicates'''




'''How many lines matched the text pattern in a particular file?'''
$ '''sort -t: -u -k 3 names.txt'''




In the example below, it displays the total number of lines that contains the text John in /etc/passwd file.31'''Linux 101 Hacks'''www.thegeekstuff.com
Emma Thomas:100:Marketing




<nowiki># grep -c John /etc/passwd 2</nowiki>
Madison Randy:300:Product Development




You can also get the total number of lines that did not match the specific pattern by passing option -cv.
Alex Jason:200:Sales




<nowiki># grep -cv John /etc/passwd 39</nowiki>
Sanjay Gupta:400:Support




'''How to search a text by ignoring the case?'''
'''Sort the passwd file by the 3<sup>rd</sup> field (numeric userid)'''




Pass the option -i (ignore case), which will ignore the case while searching.
$ '''sort -t: -k 3n /etc/passwd | more'''




<nowiki># </nowiki>grep -i john /etc/passwd
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin




jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash
'''Sort /etc/hosts file by ip-addres'''




'''How do I search all subdirectories for a text matching a specific pattern?'''
$ '''sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts'''




Use option -r (recursive) for this purpose. In the example below, it will search for the text "John" by ignoring the case inside all the subdirectories under /home/users.
127.0.0.1 localhost.localdomain localhost 192.168.100.101 dev-db.thegeekstuff.com dev-db 192.168.100.102 prod-db.thegeekstuff.com prod-db 192.168.101.20 dev-web.thegeekstuff.com dev-web 192.168.101.21 prod-web.thegeekstuff.com prod-web




This will display the output in the format of "filename: line that matching the pattern". You can also pass the option -l, which will display only the name of the file that matches the pattern.
'''Combine sort with other commands'''




<nowiki># </nowiki>grep -ri john /home/users
o '''ps –ef | sort''' : Sort the output of process list




/home/users/subdir1/letter.txt:John, Thanks for your contribution.
# '''ls -al | sort +4n ''': List the files in the ascending order of the file-size. i.e sorted by 5<sup>th</sup> filed and displaying smallest files first.  


/home/users/name_list.txt:John Smith32'''Linux 101 Hacks'''www.thegeekstuff.com
# '''ls -al | sort +4nr ''': List the files in the descending order of the''' '''file-size. i.e sorted by 5<sup>th</sup> filed and displaying largest files first.  




/home/users/name_list.txt:John Doe




<nowiki># </nowiki>grep -ril john /root
=='''Hack 24. Uniq Command'''==




/home/users/subdir1/letter.txt /home/users/name_list.txt
Uniq command is mostly used in combination with sort command, as uniq removes duplicates only from a sorted file. i.e In order for uniq to work, all the duplicate entries should be in the adjacent lines. Following are some common examples.




'''Hack 18. Find Command'''
1. When you have an employee file with duplicate entries, you can do the following to remove duplicates.




find is frequently used command to find files in the UNIX filesystem based on numerous conditions. Let us review some practice examples of find command.
$ sort namesd.txt | uniq




Syntax: find <nowiki>[pathnames] </nowiki><nowiki>[conditions]</nowiki>
$ sort –u namesd.txt




'''How to find files containing a specific word in its name?'''
2. If you want to know how many lines are duplicates, do the following. The first field in the following examples indicates how many duplicates where found for that particular line. So, in this example the lines beginning with Alex and Emma were found twice in the namesd.txt file.




The following command looks for all the files under /etc directory with mail in the filename.
$ '''sort namesd.txt | uniq –c'''




<nowiki># </nowiki>find /etc -name "*mail*"
2 Alex Jason:200:Sales




'''How to find all the files greater than certain size?'''
2 Emma Thomas:100:Marketing




The following command will list all the files in the system greater than 100MB.
1 Madison Randy:300:Product Development




<nowiki># </nowiki>find / -type f -size +100M
1 Nisha Singh:500:Sales




'''How to find files that are not modified in the last x number of days?'''
1 Sanjay Gupta:400:Support




The following command will list all the files that were modified more than 60 days ago under the current directory.33'''Linux 101 Hacks'''www.thegeekstuff.com
# The following displays only the entries that are duplicates. $ '''sort namesd.txt | uniq –cd'''  


2 Alex Jason:200:Sales


<nowiki># </nowiki>find . -mtime +60


2 Emma Thomas:100:Marketing


'''How to find files that are modified in the last x number of days?'''


=='''Hack 25. Cut Command'''==


The following command will list all the files that were modified in the last two days under the current directory.


Cut command can be used to display only specific columns from a text file or other command outputs.


<nowiki># </nowiki>find . –mtime -2


Following are some of the examples.


'''How to delete all the archive files with extension *.tar.gz and greater than 100MB?'''


'''Display the 1<sup>st</sup> field (employee name) from a colon delimited file'''


Please be careful while executing the following command as you don’t want to delete the files by mistake. The best practice is to execute the same command with ls –l to make sure you know which files will get deleted when you execute the command with rm.


$ '''cut -d: -f 1 names.txt'''


# find / -type f -name <nowiki>*.tar.gz </nowiki>-size +100M -exec ls -l {} \;


# find / -type f -name <nowiki>*.tar.gz </nowiki>-size +100M -exec rm -f {} \;
Emma Thomas


'''How to archive all the files that are not modified in the last x number of days?'''


Alex Jason


The following command finds all the files not modified in the last 60 days under /home/jsmith directory and creates an archive files under /tmp in the format of ddmmyyyy_archive.tar.


Madison Randy


<nowiki># find /home/jsmith -type f -mtime +60 | xargs tar -cvf /tmp/`date '+%d%m%Y'_archive.tar`</nowiki>


Sanjay Gupta


On a side note, you can perform lot of file related activities (including finding files) using [http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ ][http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ midnight commander GUI], a powerful text based file manager for Unix.34'''Linux 101 Hacks'''www.thegeekstuff.com


Nisha Singh


'''Hack 19. Suppress Standard Output and Error'''


'''Display 1<sup>st</sup> and 3<sup>rd</sup> field from a colon delimited file'''


'''Message'''


$ '''cut -d: -f 1,3 names.txt'''


Sometime while debugging a shell script, you may not want to see either the standard output or standard error message. Use /dev/null as shown below for suppressing the output.


Emma Thomas:Marketing


'''Suppress standard output using > /dev/null'''


Alex Jason:Sales


This will be very helpful when you are debugging shell scripts, where you don’t want to display the echo statement and interested in only looking at the error messages.


Madison Randy:Product Development


# cat file.txt > /dev/null


# ./shell-script.sh > /dev/null
Sanjay Gupta:Support


'''Suppress standard error using 2> /dev/null'''


Nisha Singh:Sales


This is also helpful when you are interested in viewing only the standard output and don’t want to view the error messages.


'''Display only the first 8 characters of every line in a file'''


# cat invalid-file-name.txt 2> /dev/null


# ./shell-script.sh 2> /dev/null
$ '''cut -c 1-8 names.txt'''


'''Hack 20. Join Command'''


Emma Tho


Join command combines lines from two files based on a common field.


Alex Jas


In the example below, we have two files – employee.txt and salary.txt. Both have employee-id as common field. So, we can use join command to combine35'''Linux 101 Hacks'''www.thegeekstuff.com


Madison


the data from these two files using employee-id as shown below.


Sanjay G


$ cat employee.txt


Nisha Si


100 Jason Smith
'''Misc Cut command examples'''




200 John Doe
# '''cut -d: -f1 /etc/passwd '''Displays the unix login names for all the users''' '''in the system.


# '''free | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2 '''Displays the total''' '''memory available on the system.


300 Sanjay Gupta
=='''Hack 26. Stat Command'''==




400 Ashok Sharma
Stat command can be used either to check the status/properties of a single file or the filesystem.




$ cat bonus.txt
'''Display statistics of a file or directory.'''




100 $5,000


{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''stat /etc/my.cnf'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


200 $500
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">File:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| `/etc/my.cnf'
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Size:</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 346 Blocks: 16 IO Block: 4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| regular file


300 $3,000
|-
 
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Device:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 801h/2049d
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode: 279856
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Links: 1


400 $1,250
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Access:</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| (0644/-rw-r--r--) Uid: (
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0/</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root)
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Gid:</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| (
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0/</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">root)</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


$ '''join employee.txt bonus.txt'''
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Access:</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2009-01-01 02:58:30.000000000 -0800
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Modify:</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2006-06-01 20:42:27.000000000 -0700
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


100 Jason Smith $5,000
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Change:</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2007-02-02 14:17:27.000000000 -0800
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''stat /home/ramesh'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


200 John Doe $500
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">File:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| `/home/ramesh'
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Size:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Blocks: 8
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| IO Block:


300 Sanjay Gupta $3,000
|-
 
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">directory</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


400 Ashok Sharma $1,250
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Device:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 803h/2051d
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode: 5521409
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Links: 7


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Access:</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| (0755/drwxr-xr-x) Uid: ( 401/ramesh)
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Gid: (</div>


'''Hack 21. Change the Case'''
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 401/ramesh)
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
Access: 2009-01-01 12:17:42.000000000 -0800


'''Convert a file to all upper-case'''


Modify: 2009-01-01 12:07:33.000000000 -0800


$ cat employee.txt


Change: 2009-01-09 12:07:33.000000000 -080043
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


100 Jason Smith
|-
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Display the status of the filesystem using option –f'''
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''stat -f /'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


200 John Doe
|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">File:</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| "/"
300 Sanjay Gupta
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ID:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Namelen:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 255
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Type: ext2/ext3


400 Ashok Sharma
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Blocks:</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Total: 2579457
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free: 2008027
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Available:


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1876998</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Size: 4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


$ '''tr a-z A-Z <nowiki>< </nowiki>employee.txt'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Inodes:</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Total: 1310720
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free: 1215892
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}


100 JASON SMITH
=='''Hack 27. Diff Command'''==




200 JOHN DOE
diff command compares two different files and reports the difference. The output is very cryptic and not straight forward to read.




300 SANJAY GUPTA36'''Linux 101 Hacks'''www.thegeekstuff.com
Syntax: diff <nowiki>[options] </nowiki>file1 file2




400 ASHOK SHARMA
'''What was modified in my new file when compare to my old file?'''




'''Convert a file to all lower-case'''
The option -w in the diff command will ignore the white space while performing the comparison.




$ cat department.txt
In the following diff output:




100 FINANCE
# The lines above ---, indicates the changes happened in first file in the diff command (i.e name_list.txt).


#
## <nowiki>The lines below ---, indicates the changes happened to the second file in the diff command (i.e name_list_new.txt). The lines that belong to the first file starts with < and the lines of second file starts with >. </nowiki>


200 MARKETING
# '''diff -w name_list.txt name_list_new.txt '''




300 PRODUCT DEVELOPMENT




400 SALES
2c2,3




$ '''tr A-Z a-z <nowiki>< </nowiki>department.txt'''
<nowiki>< </nowiki>John Doe




100 finance
---




200 marketing
# John M Doe


# Jason Bourne


300 product development
=='''Hack 28. Display total connect time of users'''==




400 sales
Ac command will display the statistics about the user’s connect time.




'''Hack 22. Xargs Command'''
'''Connect time for the current logged in user'''




xargs is a very powerful command that takes output of a command and pass it as argument of another command. Following are some practical examples on how to use xargs effectively.
With the option –d, it will break down the output for the individual days. In this example, I’ve been logged in to the system for more than 6 hours today. On Dec 1<sup>st</sup>, I was logged in for about 1 hour.




1. When you are trying to delete too many files using rm, you may get error message: /bin/rm Argument list too long – Linux. Use xargs to avoid this problem.


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''ac'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''–d'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


find ~ -name ‘*.log’ -print0 | xargs -0 rm -f
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.08</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.99</div>


2. Get a list of all the *.conf file under /etc/. There are different ways to get the same result. Following example is only to demonstrate the use of xargs. The output of the find command in this example is passed to the ls –l one by one using xargs.37'''Linux 101 Hacks'''www.thegeekstuff.com
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3.39</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4.50</div>


#
|-
## find /etc -name "*.conf" | xargs ls –l
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Today
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">6.10</div>


# If you have a file with list of URLs that you would like to download, you can use xargs as shown below.
|}
'''Connect time for all the users'''


#
## cat url-list.txt | xargs wget –c


# Find out all the jpg images and archive it.  
To display connect time for all the users use –p as shown below. Please note that this indicates the cumulative connect time for the individual users.


#
## find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz


# Copy all the images to an external hard-drive.
$ '''ac -p'''


#
## ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory


'''Hack 23. Sort Command'''


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| john
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.64


Sort command sorts the lines of a text file. Following are several practical examples on how to use the sort command based on the following sample text file that has employee information in the format:
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| madison
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.06</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| sanjay
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 88.17


employee_name:employee_id:department_name.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| nisha
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 105.92


|}


$ '''cat names.txt'''


ramesh111.42


Emma Thomas:100:Marketing


total 309.21


Alex Jason:200:Sales


'''Connect time for a specific user'''


Madison Randy:300:Product Development


To get a connect time report for a specific user, execute the following:


Sanjay Gupta:400:Support




Nisha Singh:500:Sales38'''Linux 101 Hacks'''www.thegeekstuff.com
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''ac'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''-d'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''sanjay'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">12.85</div>


'''Sort a text file in ascending order'''
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Aug 25
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5.05</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sep
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.03</div>


$ '''sort names.txt'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sep
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5.37</div>


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec 24
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8.15</div>


Alex Jason:200:Sales
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec 29
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.42</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Today
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2.95</div>


Emma Thomas:100:Marketing
|}
 


Madison Randy:300:Product Development




Nisha Singh:500:Sales
='''Chapter 5: PS1, PS2, PS3, PS4 and'''=




Sanjay Gupta:400:Support
'''PROMPT_COMMAND'''




'''Sort a text file in descending order'''
=='''Hack 29. PS1 - Default Interaction Prompt'''==




$ '''sort -r names.txt'''
The default interactive prompt on your Linux can be modified as shown below to something useful and informative. In the following example, the default PS1 was “\s-\v\$”, which displays the shell name and the version number. Let us change this default behavior to display the username, hostname and current working directory name as shown below.




Sanjay Gupta:400:Support
-bash-3.2$ '''export PS1="\u@\h \w> "'''




Nisha Singh:500:Sales
ramesh@dev-db ~> cd /etc/mail




Madison Randy:300:Product Development
ramesh@dev-db /etc/mail>




Emma Thomas:100:Marketing
<nowiki>[</nowiki>'''Note''': Prompt changed to "username@hostname current-dir>" format]




Alex Jason:200:Sales
Following PS1 codes are used in this example:




'''Sort a colon delimited text file on 2<sup>nd</sup> field (employee_id)'''
o \u – Username




$ '''sort -t: -k 2 names.txt'''
o \h – Hostname




Emma Thomas:100:Marketing
# \w - Full pathname of current directory. Please note that when you are in the home directory, this will display only ~ as shown above


Note that there is a space at the end in the value of PS1. Personally, I prefer a space at the end of the prompt for better readability.


Alex Jason:200:Sales


Make this setting permanent by adding export PS1=”\u@\h \w> ” to either


Madison Randy:300:Product Development


.bash_profile (or) .bashrc as shown below.


Sanjay Gupta:400:Support
ramesh@dev-db ~> vi ~/.bash_profile




Nisha Singh:500:Sales
ramesh@dev-db ~> vi ~/.bashrc




'''Sort a tab delimited text file on 3rd field (department_name) and suppress duplicates'''
<nowiki>[</nowiki>'''Note''': Add export PS1="\u@\h \w> " to one of the above files]




$ '''sort -t: -u -k 3 names.txt'''39'''Linux 101 Hacks'''www.thegeekstuff.com
Refer to the next chapter for several practical examples of PS1 usage in detail.




Emma Thomas:100:Marketing
=='''Hack 30. PS2 - Continuation Interactive Prompt'''==




Madison Randy:300:Product Development
A very long command can be broken down to multiple lines by giving \ at the end of the line. The default interactive prompt for a multi-line command is “> “. Let us change this default behavior to display “continue->” by using PS2 environment variable as shown below.




Alex Jason:200:Sales
'''ramesh@dev-db ~> '''myisamchk --silent --force --fast --''' '''update-state \




Sanjay Gupta:400:Support
# --key_buffer_size=512M --sort_buffer_size=512M \


# --read_buffer_size=4M --write_buffer_size=4M \


'''Sort the passwd file by the 3<sup>rd</sup> field (numeric userid)'''
# /var/lib/mysql/bugs/*.MYI


<nowiki>[</nowiki>'''Note:''' This uses the default ">" for continuation prompt]


$ '''sort -t: -k 3n /etc/passwd | more'''


ramesh@dev-db ~> '''export PS2="continue-> "'''


root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


'''ramesh@dev-db ~> '''myisamchk --silent --force --fast --''' '''update-state \


'''Sort /etc/hosts file by ip-addres'''


'''continue-> '''--key_buffer_size=512M --''' '''sort_buffer_size=512M \


$ '''sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts'''


'''continue-> '''--read_buffer_size=4M --write_buffer_size=4M


127.0.0.1 localhost.localdomain localhost 192.168.100.101 dev-db.thegeekstuff.com dev-db 192.168.100.102 prod-db.thegeekstuff.com prod-db 192.168.101.20 dev-web.thegeekstuff.com dev-web 192.168.101.21 prod-web.thegeekstuff.com prod-web


\


'''Combine sort with other commands'''


'''continue-> '''/var/lib/mysql/bugs/*.MYI


o '''ps –ef | sort''' : Sort the output of process list


<nowiki>[</nowiki>'''Note:''' This uses the modified "continue-> " for


# '''ls -al | sort +4n ''': List the files in the ascending order of the file-size. i.e sorted by 5<sup>th</sup> filed and displaying smallest files first.


# '''ls -al | sort +4nr ''': List the files in the descending order of the''' '''file-size. i.e sorted by 5<sup>th</sup> filed and displaying largest files first.
continuation prompt]


40'''Linux 101 Hacks'''www.thegeekstuff.com


I found it very helpful and easy to read, when I break my long commands into multiple lines using \. I have also seen others who don’t like to break-up long commands.


'''Hack 24. Uniq Command'''


=='''Hack 31. PS3 - Prompt used by “select” inside shell script'''==


Uniq command is mostly used in combination with sort command, as uniq removes duplicates only from a sorted file. i.e In order for uniq to work, all the duplicate entries should be in the adjacent lines. Following are some common examples.


You can define a custom prompt for the select loop inside a shell script, using the PS3 environment variable, as explained below.


1. When you have an employee file with duplicate entries, you can do the following to remove duplicates.


'''Shell script and output WITHOUT PS3:'''


$ sort namesd.txt | uniq


ramesh@dev-db ~> '''cat ps3.sh''' select i in mon tue wed exit do


$ sort –u namesd.txt


case $i in


2. If you want to know how many lines are duplicates, do the following. The first field in the following examples indicates how many duplicates where found for that particular line. So, in this example the lines beginning with Alex and Emma were found twice in the namesd.txt file.


mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;;


$ '''sort namesd.txt | uniq –c'''


esac done


2 Alex Jason:200:Sales


ramesh@dev-db ~> '''./ps3.sh''' 1) mon


2 Emma Thomas:100:Marketing
2) tue




1 Madison Randy:300:Product Development
3) wed




1 Nisha Singh:500:Sales
4) exit




1 Sanjay Gupta:400:Support
'''<nowiki>#? </nowiki>1'''




# The following displays only the entries that are duplicates. $ '''sort namesd.txt | uniq –cd'''
Monday


2 Alex Jason:200:Sales


'''<nowiki>#? </nowiki>4'''


2 Emma Thomas:100:Marketing41'''Linux 101 Hacks'''www.thegeekstuff.com


<nowiki>[</nowiki>'''Note''': This displays the default "#?" for select command prompt]


'''Hack 25. Cut Command'''


'''Shell script and output WITH PS3:'''


Cut command can be used to display only specific columns from a text file or other command outputs.


ramesh@dev-db ~> '''cat ps3.sh''' PS3="Select a day (1-4): " select i in mon tue wed exit do


Following are some of the examples.


case $i in


'''Display the 1<sup>st</sup> field (employee name) from a colon delimited file'''


mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;;


$ '''cut -d: -f 1 names.txt'''


esac done


Emma Thomas


ramesh@dev-db ~> '''./ps3.sh''' 1) mon


Alex Jason
2) tue




Madison Randy
3) wed




Sanjay Gupta
4) exit




Nisha Singh
'''Select a day (1-4): 1'''




'''Display 1<sup>st</sup> and 3<sup>rd</sup> field from a colon delimited file'''
Monday




$ '''cut -d: -f 1,3 names.txt'''
'''Select a day (1-4): 4'''




Emma Thomas:Marketing
<nowiki>[</nowiki>'''Note''': This displays the modified "Select a day (1-4):" for select command prompt]




Alex Jason:Sales
=='''Hack 32. PS4 - Used by “set -x” to prefix tracing output'''==




Madison Randy:Product Development
The PS4 shell variable defines the prompt that gets displayed, when you execute a shell script in debug mode as shown below.




Sanjay Gupta:Support
'''Shell script and output WITHOUT PS4:'''


ramesh@dev-db ~> '''cat ps4.sh'''


Nisha Singh:Sales


set -x


'''Display only the first 8 characters of every line in a file'''


echo "PS4 demo script" ls -l /etc/ | wc -l du -sh ~


$ '''cut -c 1-8 names.txt'''


ramesh@dev-db ~> '''./ps4.sh'''


Emma Tho


++ echo 'PS4 demo script' PS4 demo script


Alex Jas
# ls -l /etc/


# wc -l


Madison
243




Sanjay G
++ du -sh /home/ramesh 48K /home/ramesh




Nisha Si42'''Linux 101 Hacks'''www.thegeekstuff.com
<nowiki>[</nowiki>'''Note''': This displays the default "++" while tracing the output using set -x]




'''Misc Cut command examples'''
'''Shell script and output WITH PS4:'''




# '''cut -d: -f1 /etc/passwd '''Displays the unix login names for all the users''' '''in the system.
The PS4 defined below in the ps4.sh has the following two codes: o $0 - indicates the name of script


# '''free | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2 '''Displays the total''' '''memory available on the system.


'''Hack 26. Stat Command'''
o $LINENO - displays the current line number within the script




Stat command can be used either to check the status/properties of a single file or the filesystem.
ramesh@dev-db ~> '''cat ps4.sh'''




'''Display statistics of a file or directory.'''
export PS4='$0.$LINENO+ ' set -x




echo "PS4 demo script" ls -l /etc/ | wc -l du -sh ~


{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''stat /etc/my.cnf'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
ramesh@dev-db ~> '''./ps4.sh'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">File:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| `/etc/my.cnf'
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Size:</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 346 Blocks: 16 IO Block: 4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| regular file


|-
../ps4.sh.3+ echo 'PS4 demo script' PS4 demo script
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Device:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 801h/2049d
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode: 279856
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Links: 1


|-
../ps4.sh.4+ ls -l /etc/
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Access:</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| (0644/-rw-r--r--) Uid: (
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0/</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root)
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Gid:</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| (
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0/</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">root)</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
../ps4.sh.4+ wc -l
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Access:</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2009-01-01 02:58:30.000000000 -0800
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Modify:</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2006-06-01 20:42:27.000000000 -0700
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
243
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Change:</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2007-02-02 14:17:27.000000000 -0800
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''stat /home/ramesh'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
../ps4.sh.5+ du -sh /home/ramesh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">File:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| `/home/ramesh'
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Size:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Blocks: 8
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| IO Block:


|-
48K/home/ramesh
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4096
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">directory</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
<nowiki>[</nowiki>'''Note''': This displays the modified "{script-name}.{line-number}+" while tracing the output using set -x]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Device:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 803h/2051d
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode: 5521409
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Links: 7


|-
=='''Hack 33. PROMPT_COMMAND'''==
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Access:</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| (0755/drwxr-xr-x) Uid: ( 401/ramesh)
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Gid: (</div>


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 401/ramesh)
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
Bash shell executes the content of the PROMPT_COMMAND just before displaying the PS1 variable.
Access: 2009-01-01 12:17:42.000000000 -0800




Modify: 2009-01-01 12:07:33.000000000 -0800
ramesh@dev-db ~> '''export PROMPT_COMMAND="date +%k:%m:%S"'''




Change: 2009-01-09 12:07:33.000000000 -080043
22:08:42 ramesh@dev-db ~>
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


|-
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Display the status of the filesystem using option –f'''
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
<nowiki>[</nowiki>'''Note''': This displays the PROMPT_COMMAND and PS1 output on different lines]
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''stat -f /'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">File:</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| "/"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
If you want to display the value of PROMPT_COMMAND in the same line as the PS1, use the echo -n as shown below.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ID:</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Namelen:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 255
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Type: ext2/ext3


|-
ramesh@dev-db ~> '''export PROMPT_COMMAND="echo -n <nowiki>[$(date</nowiki>'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Blocks:</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Total: 2579457
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free: 2008027
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Available:


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1876998</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Size: 4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
'''+%k:%m:%S)]"'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Inodes:</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Total: 1310720
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free: 1215892
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
'''Hack 27. Diff Command'''


<nowiki>[22:08:51]ramesh@dev-db </nowiki>~>


diff command compares two different files and reports the difference. The output is very cryptic and not straight forward to read.


<nowiki>[</nowiki>'''Note''': This displays the PROMPT_COMMAND and PS1 output on the same line]


Syntax: diff <nowiki>[options] </nowiki>file1 file2
='''Chapter 6: Colorful and Functional Shell'''=




'''What was modified in my new file when compare to my old file?'''
'''Prompt Using PS1'''




The option -w in the diff command will ignore the white space while performing the comparison.
=='''Hack 34. Display username, hostname and basename of directory in the prompt'''==




In the following diff output:
The PS1 in this example displays following three information in the prompt:




# The lines above ---, indicates the changes happened in first file in the diff command (i.e name_list.txt).
o \u – Username o \h – Hostname


#
## <nowiki>The lines below ---, indicates the changes happened to the second file in the diff command (i.e name_list_new.txt). The lines that belong to the first file starts with < and the lines of second file starts with >. </nowiki>


# '''diff -w name_list.txt name_list_new.txt '''
o \W – Base name of the current working directory


44'''Linux 101 Hacks'''www.thegeekstuff.com


-bash-3.2$ '''export PS1="\u@\h \W> "'''


2c2,3


ramesh@dev-db ~> cd /etc/mail


<nowiki>< </nowiki>John Doe


ramesh@dev-db mail>


---


=='''Hack 35. Display current time in the prompt'''==


# John M Doe


# Jason Bourne
In the PS1 environment variable, you can directly execute any Linux command, by specifying in the format $(linux_command). In the following example, the command $(date) is executed to display the current time inside the prompt.


'''Hack 28. Display total connect time of users'''


ramesh@dev-db ~> '''export PS1="\u@\h <nowiki>[\$(date</nowiki>'''


Ac command will display the statistics about the user’s connect time.


'''+%k:%m:%S)]> "'''


'''Connect time for the current logged in user'''


ramesh@dev-db <nowiki>[11:09:56]></nowiki>


With the option –d, it will break down the output for the individual days. In this example, I’ve been logged in to the system for more than 6 hours today. On Dec 1<sup>st</sup>, I was logged in for about 1 hour.


You can also use \t to display the current time in the hh:mm:ss format as shown below:




{| style="border-spacing:0;"
ramesh@dev-db ~> '''export PS1="\u@\h <nowiki>[\t]> </nowiki>"'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''ac'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''–d'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.08</div>


|-
ramesh@dev-db <nowiki>[12:42:55]></nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.99</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3.39</div>


|-
You can also use \@ to display the current time in 12-hour am/pm format as shown below:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4.50</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Today
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">6.10</div>


|}
ramesh@dev-db ~> '''export <nowiki>PS1="[\@] </nowiki>\u@\h> "'''
'''Connect time for all the users'''




To display connect time for all the users use –p as shown below. Please note that this indicates the cumulative connect time for the individual users.
<nowiki>[04:12 </nowiki>PM] ramesh@dev-db>




$ '''ac -p'''
=='''Hack 36. Display output of any command in the prompt'''==




You can display output of any Linux command in the prompt. The following example displays three items separated by | (pipe) in the command prompt:


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| john
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.64


|-
o \!: The history number of the command
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| madison
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.06</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| sanjay
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 88.17


|-
o \h: hostname
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| nisha
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 105.92


|}
45'''Linux 101 Hacks'''www.thegeekstuff.com


# $kernel_version: The output of the uname -r command from $kernel_version variable


ramesh111.42
o \$?: Status of the last command




total 309.21
ramesh@dev-db ~> '''kernel_version=$(uname -r)'''




'''Connect time for a specific user'''
ramesh@dev-db ~> '''export PS1="\!|\h|$kernel_version|\$?> "'''




To get a connect time report for a specific user, execute the following:
473|dev-db|2.6.25-14.fc9.i686|0>




=='''Hack 37. Change foreground color of the prompt'''==


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''ac'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''-d'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''sanjay'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
Display prompt in blue color, along with username, host and current directory information
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">12.85</div>


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Aug 25
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5.05</div>


|-
$ '''export <nowiki>PS1="\e[0;34m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sep
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.03</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sep
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5.37</div>


|-
<nowiki>[</nowiki>'''Note''': This is for light blue prompt]
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec 24
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8.15</div>


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec 29
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.42</div>


|-
$ '''export <nowiki>PS1="\e[1;34m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Today
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2.95</div>


|}
46'''Linux 101 Hacks'''www.thegeekstuff.com


<nowiki>[</nowiki>'''Note''': This is for dark blue prompt]


'''Chapter 5: PS1, PS2, PS3, PS4 and'''


o <nowiki>\e[ - Indicates the beginning of color prompt</nowiki>


'''PROMPT_COMMAND'''


# x;ym - Indicates color code. Use the color code values mentioned below.


'''Hack 29. PS1 - Default Interaction Prompt'''
o <nowiki>\e[m - indicates the end of color prompt</nowiki>




The default interactive prompt on your Linux can be modified as shown below to something useful and informative. In the following example, the default PS1 was “\s-\v\$”, which displays the shell name and the version number. Let us change this default behavior to display the username, hostname and current working directory name as shown below.
'''Color Code Table:'''




-bash-3.2$ '''export PS1="\u@\h \w> "'''
Black 0;30




ramesh@dev-db ~> cd /etc/mail
Blue 0;34




ramesh@dev-db /etc/mail>
Green 0;32




<nowiki>[</nowiki>'''Note''': Prompt changed to "username@hostname current-dir>" format]
Cyan 0;36




Following PS1 codes are used in this example:
Red 0;31




o \u – Username
Purple 0;35




o \h – Hostname
Brown 0;33




# \w - Full pathname of current directory. Please note that when you are in the home directory, this will display only ~ as shown above
<nowiki>[</nowiki>'''Note''': Replace 0 with 1 for dark color]


Note that there is a space at the end in the value of PS1. Personally, I prefer a space at the end of the prompt for better readability.


Make the color change permanent by adding the following lines your ~/.bash_profile or ~/.bashrc


Make this setting permanent by adding export PS1=”\u@\h \w> ” to either


$ vi ~/.bash_profile


.bash_profile (or) .bashrc as shown below.47'''Linux 101 Hacks'''www.thegeekstuff.com
<nowiki>STARTCOLOR='\e[0;34m'; ENDCOLOR="\e[0m"</nowiki>


export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"


ramesh@dev-db ~> vi ~/.bash_profile


=='''Hack 38. Change background color of the prompt'''==


ramesh@dev-db ~> vi ~/.bashrc


<nowiki>Change the background color by specifying \e[{code}m in the PS1 prompt as shown below.</nowiki>


<nowiki>[</nowiki>'''Note''': Add export PS1="\u@\h \w> " to one of the above files]


$ '''export <nowiki>PS1="\e[47m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"'''


Refer to the next chapter for several practical examples of PS1 usage in detail.


<nowiki>[</nowiki>'''Note''': This is for Light Gray background]


'''Hack 30. PS2 - Continuation Interactive Prompt'''


Combination of background and foreground.


A very long command can be broken down to multiple lines by giving \ at the end of the line. The default interactive prompt for a multi-line command is “> “. Let us change this default behavior to display “continue->” by using PS2 environment variable as shown below.


$ '''export <nowiki>PS1="\e[0;34m\e[47m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"'''


'''ramesh@dev-db ~> '''myisamchk --silent --force --fast --''' '''update-state \


<nowiki>[</nowiki>'''Note''': This is for Light Blue foreground and Light Gray background]


# --key_buffer_size=512M --sort_buffer_size=512M \


# --read_buffer_size=4M --write_buffer_size=4M \
Add the following to your ~/.bash_profile or ~/.bashrc to make the above background and foreground color permanent.


# /var/lib/mysql/bugs/*.MYI


<nowiki>[</nowiki>'''Note:''' This uses the default ">" for continuation prompt]
<nowiki>$ vi ~/.bash_profile STARTFGCOLOR='\e[0;34m'; STARTBGCOLOR="\e[47m" ENDCOLOR="\e[0m"</nowiki>




ramesh@dev-db ~> '''export PS2="continue-> "'''
export PS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h \w> $ENDCOLOR"




'''ramesh@dev-db ~> '''myisamchk --silent --force --fast --''' '''update-state \
Play around by using the following background color and choose the one that match your taste:




'''continue-> '''--key_buffer_size=512M --''' '''sort_buffer_size=512M \
o <nowiki>\e[40m</nowiki>




'''continue-> '''--read_buffer_size=4M --write_buffer_size=4M
o <nowiki>\e[41m</nowiki>




\
o <nowiki>\e[42m</nowiki>




'''continue-> '''/var/lib/mysql/bugs/*.MYI
o <nowiki>\e[43m</nowiki>




<nowiki>[</nowiki>'''Note:''' This uses the modified "continue-> " for48'''Linux 101 Hacks'''www.thegeekstuff.com
o <nowiki>\e[44m</nowiki>




continuation prompt]
o <nowiki>\e[45m</nowiki>




I found it very helpful and easy to read, when I break my long commands into multiple lines using \. I have also seen others who don’t like to break-up long commands.
o <nowiki>\e[46m</nowiki>




'''Hack 31. PS3 - Prompt used by “select” inside shell script'''
o <nowiki>\e[47m</nowiki>




You can define a custom prompt for the select loop inside a shell script, using the PS3 environment variable, as explained below.
=='''Hack 39. Display multiple colors in the prompt'''==




'''Shell script and output WITHOUT PS3:'''
You can also display multiple colors in the same prompt. Add the following function to your ~/.bash_profile




ramesh@dev-db ~> '''cat ps3.sh''' select i in mon tue wed exit do
function prompt {




case $i in
<nowiki>local BLUE="\[\033[0;34m\]" local DARK_BLUE="\[\033[1;34m\]” local RED=”\[\033[0;31m\]” local DARK_RED=”\[\033[1;31m\]” local NO_COLOR=”\[\033[0m\]” case $TERM in</nowiki>




mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;;
<nowiki>xterm*|rxvt*) TITLEBAR=’\[\033]0;\u@\h:\w\007\]’</nowiki>




esac done
<nowiki>;;</nowiki>




ramesh@dev-db ~> '''./ps3.sh''' 1) mon
<nowiki>*) TITLEBAR=”"</nowiki>


2) tue


<nowiki>;;</nowiki>


3) wed


esac


4) exit


<nowiki>PS1=”\u@\h [\t]> ” PS1=”${TITLEBAR}\</nowiki>


'''<nowiki>#? </nowiki>1'''


<nowiki>$BLUE\u@\h $RED[\t]>$NO_COLOR ” PS2=’continue-> ‘ PS4=’$0.$LINENO+ ‘</nowiki>


Monday
}


You can re-login for the changes to take effect or source the .bash_profile as shown below.


'''<nowiki>#? </nowiki>4'''


$. ./.bash_profile


<nowiki>[</nowiki>'''Note''': This displays the default "#?" for select command prompt]49'''Linux 101 Hacks'''www.thegeekstuff.com


$ '''prompt'''


'''Shell script and output WITH PS3:'''


ramesh@dev-db <nowiki>[13:02:13]></nowiki>


ramesh@dev-db ~> '''cat ps3.sh''' PS3="Select a day (1-4): " select i in mon tue wed exit do


=='''Hack 40. Change the prompt color using tput'''==


case $i in


You can also change color of the PS1 prompt using tput as shown below:


mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;;


<nowiki>$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]“</nowiki>


esac done


tput Color Capabilities:


ramesh@dev-db ~> '''./ps3.sh''' 1) mon


2) tue
o <nowiki>tput setab [1-7] - Set a background color using ANSI escape</nowiki> o <nowiki>tput setb [1-7] - Set a background color</nowiki>




3) wed
o <nowiki>tput setaf [1-7] - Set a foreground color using ANSI escape</nowiki> o <nowiki>tput setf [1-7] - Set a foreground color</nowiki>




4) exit
tput Text Mode Capabilities:




'''Select a day (1-4): 1'''
o tput bold - Set bold mode




Monday
o tput dim - turn on half-bright mode o tput smul - begin underline mode o tput rmul - exit underline mode




'''Select a day (1-4): 4'''
o tput rev - Turn on reverse mode58
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o tput smso - Enter standout mode (bold on rxvt)


<nowiki>[</nowiki>'''Note''': This displays the modified "Select a day (1-4):" for select command prompt]
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tput rmso - Exit standout mode
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o tput sgr0 - Turn off all attributes
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


'''Hack 32. PS4 - Used by “set -x” to prefix tracing output'''
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Color Code for tput:
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Black
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


The PS4 shell variable defines the prompt that gets displayed, when you execute a shell script in debug mode as shown below.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Red
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Green
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


'''Shell script and output WITHOUT PS4:'''50'''Linux 101 Hacks'''www.thegeekstuff.com
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Yellow
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Blue
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


ramesh@dev-db ~> '''cat ps4.sh'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 5
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Magenta
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 6
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Cyan
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


set -x
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 7
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| - White
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}


echo "PS4 demo script" ls -l /etc/ | wc -l du -sh ~
=='''Hack 41. Create your own prompt using the available codes for PS1 variable'''==




ramesh@dev-db ~> '''./ps4.sh'''
Use the following codes and create your own personal PS1 Linux prompt that is functional and suites your taste.




++ echo 'PS4 demo script' PS4 demo script
o \a an ASCII bell character (07)


# ls -l /etc/


# wc -l
# \d the date in “Weekday Month Date” format (e.g., “Tue May 26″)


243
# \D{format} - the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required


o \e an ASCII escape character (033)59
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


++ du -sh /home/ramesh 48K /home/ramesh
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o \h the hostname up to the first part
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \H the hostname
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


<nowiki>[</nowiki>'''Note''': This displays the default "++" while tracing the output using set -x]
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o \j the number of jobs currently managed by the shell


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o \l the basename of the shell’s terminal device name


'''Shell script and output WITH PS4:'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \n newline
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \r carriage return
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


The PS4 defined below in the ps4.sh has the following two codes: o $0 - indicates the name of script
|}
# \s the name of the shell, the basename of $0 (the portion following the final slash)


o \t the current time in 24-hour HH:MM:SS format o \T the current time in 12-hour HH:MM:SS format o \@ the current time in 12-hour am/pm format o \A the current time in 24-hour HH:MM format


o $LINENO - displays the current line number within the script


o \u the username of the current user o \v the version of bash (e.g., 2.00)


ramesh@dev-db ~> '''cat ps4.sh'''


o \V the release of bash, version + patch level (e.g., 2.00.0)


export PS4='$0.$LINENO+ ' set -x


# \w the current working directory, with $HOME abbreviated with a tilde


echo "PS4 demo script" ls -l /etc/ | wc -l du -sh ~
# \W the basename of the current working directory, with $HOME abbreviated with a tilde


o \! the history number of this command


ramesh@dev-db ~> '''./ps4.sh'''


o \# the command number of this command


../ps4.sh.3+ echo 'PS4 demo script' PS4 demo script


../ps4.sh.4+ ls -l /etc/51'''Linux 101 Hacks'''www.thegeekstuff.com
o \$ if the effective UID is 0, a #, otherwise a $




../ps4.sh.4+ wc -l
o \nnn the character corresponding to the octal number nnn o \\ a backslash




243
# <nowiki>\[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt </nowiki>




../ps4.sh.5+ du -sh /home/ramesh




48K/home/ramesh
o \] end a sequence of non-printing character




<nowiki>[</nowiki>'''Note''': This displays the modified "{script-name}.{line-number}+" while tracing the output using set -x]
=='''Hack 42. Use bash shell function inside PS1 variable'''==




'''Hack 33. PROMPT_COMMAND'''
You can also invoke a bash shell function in the PS1 as shown below.




Bash shell executes the content of the PROMPT_COMMAND just before displaying the PS1 variable.
ramesh@dev-db ~> function httpdcount {




ramesh@dev-db ~> '''export PROMPT_COMMAND="date +%k:%m:%S"'''
# ps aux | grep httpd | grep -v grep | wc -l


# }


22:08:42 ramesh@dev-db ~>
ramesh@dev-db ~> '''export PS1="\u@\h <nowiki>[`httpdcount`]> </nowiki>"'''




<nowiki>[</nowiki>'''Note''': This displays the PROMPT_COMMAND and PS1 output on different lines]
ramesh@dev-db <nowiki>[12]></nowiki>




If you want to display the value of PROMPT_COMMAND in the same line as the PS1, use the echo -n as shown below.
<nowiki>[</nowiki>'''Note''': This displays the total number of running httpd processes]




ramesh@dev-db ~> '''export PROMPT_COMMAND="echo -n <nowiki>[$(date</nowiki>'''
You can add the following line to your ~/.bash_profile or ~/.bashrc to make this change permanent:




'''+%k:%m:%S)]"'''
$ vi .bash_profile function httpdcount {


ps aux | grep httpd | grep -v grep | wc -l


<nowiki>[22:08:51]ramesh@dev-db </nowiki>~>


}


<nowiki>[</nowiki>'''Note''': This displays the PROMPT_COMMAND and PS1 output on the same line]52'''Linux 101 Hacks'''www.thegeekstuff.com


export PS1='\u@\h <nowiki>[`httpdcount`]> </nowiki>'


'''Chapter 6: Colorful and Functional Shell'''


=='''Hack 43. Use shell script inside PS1 variable'''==


'''Prompt Using PS1'''


You can also invoke a shell script inside the PS1 variable. In the example below, the ~/bin/totalfilesize.sh, which calculates the total filesize of the current directory, is invoked inside the PS1 variable.


'''Hack 34. Display username, hostname and basename of directory in the prompt'''


ramesh@dev-db ~> cat ~/bin/totalfilesize.sh


The PS1 in this example displays following three information in the prompt:


for filesize in $(ls -l . | grep "^-" | awk '{print $5}')


o \u – Username o \h – Hostname


do


o \W – Base name of the current working directory


let totalsize=$totalsize+$filesize done


-bash-3.2$ '''export PS1="\u@\h \W> "'''


echo -n "$totalsize"


ramesh@dev-db ~> cd /etc/mail


ramesh@dev-db ~> export PATH=$PATH:~/bin


ramesh@dev-db mail>


ramesh@dev-db ~> '''export PS1="\u@\h'''


'''Hack 35. Display current time in the prompt'''


'''<nowiki>[\$(totalfilesize.sh) </nowiki>bytes]> "'''


In the PS1 environment variable, you can directly execute any Linux command, by specifying in the format $(linux_command). In the following example, the command $(date) is executed to display the current time inside the prompt.


ramesh@dev-db <nowiki>[534 </nowiki>bytes]> cd /etc/mail


ramesh@dev-db ~> '''export PS1="\u@\h <nowiki>[\$(date</nowiki>'''


ramesh@dev-db <nowiki>[167997 </nowiki>bytes]>


'''+%k:%m:%S)]> "'''


<nowiki>[</nowiki>'''Note''': This executes the totalfilesize.sh to display the total file size of the current directory in the PS1 prompt]


ramesh@dev-db <nowiki>[11:09:56]></nowiki>53'''Linux 101 Hacks'''www.thegeekstuff.com
='''Chapter 7: Archive and Compression'''=




You can also use \t to display the current time in the hh:mm:ss format as shown below:
=='''Hack 44. Zip command basics'''==




ramesh@dev-db ~> '''export PS1="\u@\h <nowiki>[\t]> </nowiki>"'''
'''How to zip multiple files?'''




ramesh@dev-db <nowiki>[12:42:55]></nowiki>
syntax: zip {.zip file-name} {file-names}




You can also use \@ to display the current time in 12-hour am/pm format as shown below:
# zip var-log-files.zip /var/log/* adding: var/log/acpid (deflated 81%)


adding: var/log/anaconda.log (deflated 79%) adding: var/log/anaconda.syslog (deflated 73%) adding: var/log/anaconda.xlog (deflated 82%) adding: var/log/audit/ (stored 0%)


ramesh@dev-db ~> '''export <nowiki>PS1="[\@] </nowiki>\u@\h> "'''
adding: var/log/boot.log (stored 0%) adding: var/log/boot.log.1 (deflated 40%) adding: var/log/boot.log.2 (deflated 42%) adding: var/log/boot.log.3 (deflated 40%) adding: var/log/boot.log.4 (deflated 40%)




<nowiki>[04:12 </nowiki>PM] ramesh@dev-db>
'''How to zip a directory and it’s files recursively?'''




'''Hack 36. Display output of any command in the prompt'''
<nowiki># zip -r var-log-dir.zip /var/log/ updating: var/log/ (stored 0%)</nowiki>


adding: var/log/wtmp (deflated 78%)


You can display output of any Linux command in the prompt. The following example displays three items separated by | (pipe) in the command prompt:


adding: var/log/scrollkeeper.log (deflated 94%) adding: var/log/rpmpkgs.3 (deflated 68%) adding: var/log/spooler (stored 0%)


o \!: The history number of the command


adding: var/log/cron.2 (deflated 90%) adding: var/log/spooler.1 (stored 0%) adding: var/log/spooler.4 (stored 0%) adding: var/log/httpd/ (stored 0%) adding: var/log/rpmpkgs.1 (deflated 68%)


o \h: hostname


adding: var/log/anaconda.log (deflated 79%) adding: var/log/secure.2 (deflated 93%)


# $kernel_version: The output of the uname -r command from $kernel_version variable
'''How to unzip a *.zip compressed file?'''


o \$?: Status of the last command


<nowiki># unzip var-log.zip Archive: var-log.zip</nowiki>


ramesh@dev-db ~> '''kernel_version=$(uname -r)'''


inflating: var/log/acpid inflating: var/log/anaconda.log inflating: var/log/anaconda.syslog inflating: var/log/anaconda.xlog creating: var/log/audit/


ramesh@dev-db ~> '''export PS1="\!|\h|$kernel_version|\$?> "'''


To see a detailed output during unzip pass the –v option as shown below.


473|dev-db|2.6.25-14.fc9.i686|0>54'''Linux 101 Hacks'''www.thegeekstuff.com




'''Hack 37. Change foreground color of the prompt'''
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''unzip -v var-log.zip'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Archive:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var-log.zip
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


Display prompt in blue color, along with username, host and current directory information
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Length
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Method
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Size</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ratio</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Date
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Time</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| CRC-32
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Name
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


$ '''export <nowiki>PS1="\e[0;34m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>--------</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>------</nowiki>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>------- </nowiki><nowiki>-----</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>----</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>----</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>------</nowiki>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ---


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


<nowiki>[</nowiki>'''Note''': This is for light blue prompt]
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1916</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Defl:N
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">369</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>81%</center>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-08-08 14:27</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| e2ffdc0c
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/acpid
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


$ '''export <nowiki>PS1="\e[1;34m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13546</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Defl:N
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2900</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>79%</center>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-02-07 14:25</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 34cc03a1
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/anaconda.log
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


<nowiki>[</nowiki>'''Note''': This is for dark blue prompt]
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| skip..
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7680</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Defl:N
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">411</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>95%</center>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">12-30-08 10:55</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| fe876ee9
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


o <nowiki>\e[ - Indicates the beginning of color prompt</nowiki>
|-
 
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/wtmp.1
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
# x;ym - Indicates color code. Use the color code values mentioned below.  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


o <nowiki>\e[m - indicates the end of color prompt</nowiki>
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">40981</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Defl:N
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7395</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>82%</center>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-08-08 14:28</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 6386a95e
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/Xorg.0.log
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


'''Color Code Table:'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>--------</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>-------</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>---</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ---


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>----</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


Black 0;30
|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">41406991</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
Blue 0;34
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2809229</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>93%</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 56


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| files
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


Green 0;32
|}
'''How to list a content of zip file with uncompressing it?'''




Cyan 0;36


{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''unzip -l var-log.zip'''
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


Red 0;31
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Archive:</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var-log.zip
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Length</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Date
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Time
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Name


Purple 0;35
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>--------</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>----</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>----</nowiki>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>----</nowiki>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1916</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-08-08</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 14:27
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/acpid


Brown 0;33
|}
64
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''Linux 101 Hacks'''</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13546</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-02-07</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">14:25</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/anaconda.log


<nowiki>[</nowiki>'''Note''': Replace 0 with 1 for dark color]
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">..skip..</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">40981</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-08-08</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">14:28</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/Xorg.0.log


Make the color change permanent by adding the following lines your ~/.bash_profile or ~/.bashrc
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">40981</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-08-07</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">14:56</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/Xorg.0.log.old


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>--------</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>-------</nowiki>


$ vi ~/.bash_profile55'''Linux 101 Hacks'''www.thegeekstuff.com
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">41406991</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 56 files


|}


<nowiki>STARTCOLOR='\e[0;34m'; ENDCOLOR="\e[0m"</nowiki>
=='''Hack 45. Advanced compression using zip command.'''==


export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"


There are 10 levels of compression provided by zip command.


'''Hack 38. Change background color of the prompt'''


# Level 0 is the lowest level, where it just archives the file without any compression.


<nowiki>Change the background color by specifying \e[{code}m in the PS1 prompt as shown below.</nowiki>
o Level 1 will perform little compression. But, will be very fast.




$ '''export <nowiki>PS1="\e[47m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"'''
o Level 6 is the default level of compression.




<nowiki>[</nowiki>'''Note''': This is for Light Gray background]
# Level 9 is the maximum compression. This will be slower when compared to default level. In my opinion, unless you are compressing a huge file, you should always use level 9.


In the example below, I used Level 0, default Level 6, and Level 9 compression on a same directory. See the compressed file size yourself.


Combination of background and foreground.


# zip var-log-files-default.zip /var/log/*


$ '''export <nowiki>PS1="\e[0;34m\e[47m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"'''
# zip -0 var-log-files-0.zip /var/log/*


# zip -9 var-log-files-9.zip /var/log/*


<nowiki>[</nowiki>'''Note''': This is for Light Blue foreground and Light Gray background]
# ls -ltr




Add the following to your ~/.bash_profile or ~/.bashrc to make the above background and foreground color permanent.
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -rw-r--r--
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2817248</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jan
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13:05</div>


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var-log-files-default.zip
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


<nowiki>$ vi ~/.bash_profile STARTFGCOLOR='\e[0;34m'; STARTBGCOLOR="\e[47m" ENDCOLOR="\e[0m"</nowiki>
|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -rw-r--r--
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
export PS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h \w> $ENDCOLOR"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">41415301</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jan
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13:05</div>


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var-log-files-0.zip
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


Play around by using the following background color and choose the one that match your taste:
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -rw-r--r--
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2582610</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jan
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13:06</div>


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var-log-files-9.zip
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


o <nowiki>\e[40m</nowiki>56'''Linux 101 Hacks'''www.thegeekstuff.com
|}




o <nowiki>\e[41m</nowiki>




o <nowiki>\e[42m</nowiki>
=='''Hack 46. Password Protection of Zip files'''==




o <nowiki>\e[43m</nowiki>
Pass the option –P to the zip command to assign a password to the zip file.




o <nowiki>\e[44m</nowiki>
<nowiki># </nowiki>zip -P mysecurepwd var-log-protected.zip /var/log/*




o <nowiki>\e[45m</nowiki>
The above option is good if you are using the command inside a shell-script for background jobs. However, when you are performing the compression interactively on the command-line, you don’t want the password to be visible in the history. So, use the option –e as shown below to assign the password.




o <nowiki>\e[46m</nowiki>
<nowiki># zip -e var-log-protected.zip /var/log/* Enter password:</nowiki>




o <nowiki>\e[47m</nowiki>
Verify password:




'''Hack 39. Display multiple colors in the prompt'''
updating: var/log/acpid (deflated 81%) updating: var/log/anaconda.log (deflated 79%)




You can also display multiple colors in the same prompt. Add the following function to your ~/.bash_profile
When you are uncompressing a password protected file, it will ask for the password as shown below.




function prompt {
<nowiki># unzip var-log-protected.zip Archive: var-log-protected.zip</nowiki>




<nowiki>local BLUE="\[\033[0;34m\]" local DARK_BLUE="\[\033[1;34m\]” local RED=”\[\033[0;31m\]” local DARK_RED=”\[\033[1;31m\]” local NO_COLOR=”\[\033[0m\]” case $TERM in</nowiki>
<nowiki>[var-log-protected.zip] </nowiki>var/log/acpid password:




<nowiki>xterm*|rxvt*) TITLEBAR=’\[\033]0;\u@\h:\w\007\]’</nowiki>
=='''Hack 47. Validate a zip archive'''==




<nowiki>;;</nowiki>
Sometime you may want to validate a zip archive without extracting it. To test the validity of the zip file, pass option –t as shown below.




<nowiki>*) TITLEBAR=”"</nowiki>


{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki># </nowiki>'''unzip -t var-log.zip'''</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


<nowiki>;;</nowiki>
|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Archive:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">var-log.zip</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


esac
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| testing:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">var/log/acpid</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| OK
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| testing:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">var/log/anaconda.log</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| OK


|}


<nowiki>PS1=”\u@\h [\t]> ” PS1=”${TITLEBAR}\</nowiki>


testing: var/log/anaconda.syslog OK


<nowiki>$BLUE\u@\h $RED[\t]>$NO_COLOR ” PS2=’continue-> ‘ PS4=’$0.$LINENO+ ‘</nowiki>


}57'''Linux 101 Hacks'''www.thegeekstuff.com
skip...




You can re-login for the changes to take effect or source the .bash_profile as shown below.
testing: var/log/wtmpOK




$. ./.bash_profile
testing: var/log/wtmp.1OK




$ '''prompt'''
testing: var/log/Xorg.0.logOK




ramesh@dev-db <nowiki>[13:02:13]></nowiki>
No errors detected in compressed data of var-log.zip.




'''Hack 40. Change the prompt color using tput'''
=='''Hack 48. Tar Command Basics'''==




You can also change color of the PS1 prompt using tput as shown below:
tar command (tape archive) is used to convert a group of files into an archive.




<nowiki>$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]</nowiki>
<nowiki>Syntax: tar [options] [tar-archive-name] [other-file-names]</nowiki>




tput Color Capabilities:
'''How can I create a single backup file of all files and subdirectories under my home directory?'''




o <nowiki>tput setab [1-7] - Set a background color using ANSI escape</nowiki> o <nowiki>tput setb [1-7] - Set a background color</nowiki>
The following command creates a single archive backup file called my_home_directory.tar under /tmp. This archive will contain all the files and subdirectories under /home/jsmith.




o <nowiki>tput setaf [1-7] - Set a foreground color using ANSI escape</nowiki> o <nowiki>tput setf [1-7] - Set a foreground color</nowiki>
o Option c, stands for create an archive.




tput Text Mode Capabilities:
# Option v stands for verbose mode, displays additional information while executing the command.


#
## Option f indicates the archive file name mentioned in the command.


o tput bold - Set bold mode
# tar cvf /tmp/my_home_directory.tar /home/jsmith




o tput dim - turn on half-bright mode o tput smul - begin underline mode o tput rmul - exit underline mode


'''How do I view all the files inside the tar archive?'''


o tput rev - Turn on reverse mode58
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


|-
Option t will display all the files from the tar archive.
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o tput smso - Enter standout mode (bold on rxvt)


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tput rmso - Exit standout mode
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
<nowiki># </nowiki>tar tvf /tmp/my_home_directory.tar
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o tput sgr0 - Turn off all attributes
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Color Code for tput:
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
'''How do I extract all the files from a tar archive?'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Black
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Red
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
Option x will extract the files from the tar archive as shown below. This will extract the content to the current directory location from where the command is executed.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Green
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Yellow
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
<nowiki># </nowiki>tar xvf /tmp/my_home_directory.tar
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Blue
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 5
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Magenta
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
'''How do I extract tar.gz files to a specific directory?'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 6
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| – Cyan
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 7
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| - White
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
<nowiki># tar xvfz /tmp/my_home_directory.tar.gz –C /home/ramesh</nowiki>
'''Hack 41. Create your own prompt using the available codes for PS1 variable'''




Use the following codes and create your own personal PS1 Linux prompt that is functional and suites your taste.
=='''Hack 49. Combine gzip, bzip2 with tar'''==




o \a an ASCII bell character (07)
'''How to use gzip with tar?'''




# \d the date in “Weekday Month Date” format (e.g., “Tue May 26″)
Add ''option z'' to the tar command when dealing with tar.gz compressed file.


# \D{format} - the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required


o \e an ASCII escape character (033)59
# tar cvfz /tmp/my_home_directory.tar.gz /home/jsmith
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


|-
# tar xvfz /tmp/my_home_directory.tar.gz
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o \h the hostname up to the first part
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
# tar tvfz /tmp/my_home_directory.tar.gz
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \H the hostname
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
'''Note: '''Using gzip is faster when compared to bzip2.
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o \j the number of jobs currently managed by the shell


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o \l the basename of the shell’s terminal device name


|-
'''How to use bzip2 with tar?'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \n newline
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \r carriage return
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
Add ''option j'' to the tar command when dealing with tar.bz2 compressed file.
# \s the name of the shell, the basename of $0 (the portion following the final slash)


o \t the current time in 24-hour HH:MM:SS format o \T the current time in 12-hour HH:MM:SS format o \@ the current time in 12-hour am/pm format o \A the current time in 24-hour HH:MM format


# tar cvfj /tmp/my_home_directory.tar.bz2 /home/jsmith


o \u the username of the current user o \v the version of bash (e.g., 2.00)
# tar xvfj /tmp/my_home_directory.tar.bz2


# tar tvfj /tmp/my_home_directory.tar.bz2


o \V the release of bash, version + patch level (e.g., 2.00.0)
'''Note: '''Using bizp2 gives higher level of compression when compared to gzip.




# \w the current working directory, with $HOME abbreviated with a tilde
='''Chapter 8: Command Line History'''=


# \W the basename of the current working directory, with $HOME abbreviated with a tilde


o \! the history number of this command
When you are using Linux command line frequently, using the history effectively can be a major productivity boost. In fact, once you have mastered the 15 examples that I’ve provided here, you’ll find using command line more enjoyable and fun.




o \# the command number of this command
=='''Hack 50. Display TIMESTAMP in history using HISTTIMEFORMAT'''==




o \$ if the effective UID is 0, a #, otherwise a $
Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timestamp along with the command as shown below.




o \nnn the character corresponding to the octal number nnn o \\ a backslash
# '''export HISTTIMEFORMAT=’%F %T ‘ '''


# history | more


# <nowiki>\[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt </nowiki>
#  
## 2008-08-05 19:02:39 service network restart


60'''Linux 101 Hacks'''www.thegeekstuff.com
#
## 2008-08-05 19:02:39 exit
## 2008-08-05 19:02:39 id
## 2008-08-05 19:02:39 cat /etc/redhat-release


'''Note''': You can also setup the following alias to view the recent history''' '''commands.


o \] end a sequence of non-printing character


alias h1='history 10' alias h2='history 20' alias h3='history 30'


'''Hack 42. Use bash shell function inside PS1 variable'''


=='''Hack 51. Search the history using Control+R'''==


You can also invoke a bash shell function in the PS1 as shown below.


I strongly believe that this may be your most frequently used feature of history. When you’ve already executed a very long command, you can simply


ramesh@dev-db ~> function httpdcount {


search history using a keyword and re-execute the same command without having to type it fully. Press Control+R and type the keyword.


# ps aux | grep httpd | grep -v grep | wc -l


# }
In the following example, I searched for red, which displayed the previous command “cat /etc/redhat-release” in the history that contained the word red.


ramesh@dev-db ~> '''export PS1="\u@\h <nowiki>[`httpdcount`]> </nowiki>"'''


<nowiki># </nowiki><nowiki>[</nowiki>'''Note''': Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt as shown below]


ramesh@dev-db <nowiki>[12]></nowiki>


'''(reverse-i-search)`red‘: cat /etc/redhat-release'''


<nowiki>[</nowiki>'''Note''': This displays the total number of running httpd processes]


<nowiki>[Note: Press enter when you see your command, which will execute the command from the history]</nowiki>


You can add the following line to your ~/.bash_profile or ~/.bashrc to make this change permanent:


<nowiki># </nowiki>'''cat /etc/redhat-release'''


$ vi .bash_profile function httpdcount {


ps aux | grep httpd | grep -v grep | wc -l
Fedora release 9 (Sulphur)




}
Sometimes you want to edit a command from history before executing it. For e.g. you can search for httpd, which will display service httpd stop from the command history, select this command and change the stop to start and re-execute it again as shown below.




export PS1='\u@\h <nowiki>[`httpdcount`]> </nowiki>'
<nowiki># </nowiki><nowiki>[</nowiki>'''Note''': Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]


'''(reverse-i-search)`httpd‘: service httpd stop'''


'''Hack 43. Use shell script inside PS1 variable'''


<nowiki>[Note: Press either left arrow or right arrow key when you see your command, which will display the command for you to edit, before executing it]</nowiki>


You can also invoke a shell script inside the PS1 variable. In the example below, the ~/bin/totalfilesize.sh, which calculates the total filesize of the current directory, is invoked inside the PS1 variable.


'''<nowiki># </nowiki>service httpd start'''


ramesh@dev-db ~> cat ~/bin/totalfilesize.sh61'''Linux 101 Hacks'''www.thegeekstuff.com
=='''Hack 52. Repeat previous command quickly using 4 different methods'''==




for filesize in $(ls -l . | grep "^-" | awk '{print $5}')
Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command.




do
# Use the '''up arrow''' to view the previous command and press enter to execute it.


# '''Type !! '''and press enter from the command line''' '''


let totalsize=$totalsize+$filesize done
# '''Type !-1 '''and press enter from the command line.''' '''


# '''Press Control+P '''will display the previous command, press enter''' '''to execute it


echo -n "$totalsize"
=='''Hack 53. Execute a specific command from history'''==




ramesh@dev-db ~> export PATH=$PATH:~/bin
In the following example, If you want to repeat the command #4, execute !4 as shown below.




ramesh@dev-db ~> '''export PS1="\u@\h'''
# history | more


#
## service network restart
## exit
## id
## cat /etc/redhat-release


'''<nowiki>[\$(totalfilesize.sh) </nowiki>bytes]> "'''
# '''!4 '''


cat /etc/redhat-release Fedora release 9 (Sulphur)


ramesh@dev-db <nowiki>[534 </nowiki>bytes]> cd /etc/mail


=='''Hack 54. Execute previous command that starts with a specific word'''==


ramesh@dev-db <nowiki>[167997 </nowiki>bytes]>


Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example, typing !ps and enter, executed the previous command starting with ps, which is ‘ps aux | grep yp’.


<nowiki>[</nowiki>'''Note''': This executes the totalfilesize.sh to display the total file size of the current directory in the PS1 prompt]62'''Linux 101 Hacks'''www.thegeekstuff.com




'''Chapter 7: Archive and Compression'''
{| style="border-spacing:0;"
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''!ps'''
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
'''Hack 44. Zip command basics'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
'''How to zip multiple files?'''
 
 
syntax: zip {.zip file-name} {file-names}
 
 
# zip var-log-files.zip /var/log/* adding: var/log/acpid (deflated 81%)
 
adding: var/log/anaconda.log (deflated 79%) adding: var/log/anaconda.syslog (deflated 73%) adding: var/log/anaconda.xlog (deflated 82%) adding: var/log/audit/ (stored 0%)
 
adding: var/log/boot.log (stored 0%) adding: var/log/boot.log.1 (deflated 40%) adding: var/log/boot.log.2 (deflated 42%) adding: var/log/boot.log.3 (deflated 40%) adding: var/log/boot.log.4 (deflated 40%)
 
 
'''How to zip a directory and it’s files recursively?'''
 
 
<nowiki># zip -r var-log-dir.zip /var/log/ updating: var/log/ (stored 0%)</nowiki>
 
adding: var/log/wtmp (deflated 78%)
 
 
adding: var/log/scrollkeeper.log (deflated 94%) adding: var/log/rpmpkgs.3 (deflated 68%) adding: var/log/spooler (stored 0%)
 
 
adding: var/log/cron.2 (deflated 90%) adding: var/log/spooler.1 (stored 0%) adding: var/log/spooler.4 (stored 0%) adding: var/log/httpd/ (stored 0%) adding: var/log/rpmpkgs.1 (deflated 68%)
 
 
adding: var/log/anaconda.log (deflated 79%) adding: var/log/secure.2 (deflated 93%)63'''Linux 101 Hacks'''www.thegeekstuff.com
 
 
'''How to unzip a *.zip compressed file?'''
 
 
<nowiki># unzip var-log.zip Archive: var-log.zip</nowiki>
 
 
inflating: var/log/acpid inflating: var/log/anaconda.log inflating: var/log/anaconda.syslog inflating: var/log/anaconda.xlog creating: var/log/audit/
 
 
To see a detailed output during unzip pass the –v option as shown below.
 
 
 
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''unzip -v var-log.zip'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Archive:
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ps aux | grep yp
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var-log.zip
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Length
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Method
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">root</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Size</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>16947</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ratio</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Date
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Time</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">36516</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| CRC-32
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1264</div>
| colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ?


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Name
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sl
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13:10</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>0:00 ypbind</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>--------</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>------</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">root</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>------- </nowiki><nowiki>-----</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>17503</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>----</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>----</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>------</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4124</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ---
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">740</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/0


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S+
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">19:19</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 grep
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| yp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|}
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1916</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Defl:N
=='''Hack 55. Control the total number of lines in the history using HISTSIZE'''==
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">369</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>81%</center>
 
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-08-08 14:27</div>
Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 450 command will be stored in the bash history.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| e2ffdc0c
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/acpid
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
<nowiki># </nowiki>vi ~/.bash_profile
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13546</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Defl:N
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2900</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>79%</center>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-02-07 14:25</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 34cc03a1
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/anaconda.log
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
HISTSIZE=450
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| skip..
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7680</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Defl:N
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">411</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>95%</center>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">12-30-08 10:55</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| fe876ee9
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
HISTFILESIZE=450
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/wtmp.1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">40981</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Defl:N
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7395</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>82%</center>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-08-08 14:28</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 6386a95e
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
=='''Hack 56. Change the history file name using HISTFILE'''==
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/Xorg.0.log
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .commandline_warrior file instead of .bash_history file. I’m yet to figure out a practical use for this. I can see this getting used when you want to track commands executed from different terminals using different history file name.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>--------</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>-------</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>---</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ---


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>----</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
<nowiki># vi ~/.bash_profile HISTFILE=/root/.commandline_warrior</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">41406991</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2809229</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>93%</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 56


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| files
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
=='''Hack 57. Eliminate the continuous repeated entry from history using HISTCONTROL'''==
'''How to list a content of zip file with uncompressing it?'''




In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.


{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''unzip -l var-log.zip'''
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
# pwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Archive:</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var-log.zip
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
# pwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Length</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Date
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Time
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Name


|-
# pwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>--------</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>----</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>----</nowiki>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>----</nowiki>


|-
# history | tail -4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1916</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-08-08</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 14:27
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/acpid


|}
#
64
## pwd
{| style="border-spacing:0;"
## pwd
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''Linux 101 Hacks'''</div>
## pwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
## history | tail -4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


|-
<nowiki>[</nowiki>'''Note''': There are three pwd commands in history, after executing pwd 3 times as shown above]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13546</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-02-07</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">14:25</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/anaconda.log


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">..skip..</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
# '''export HISTCONTROL=ignoredups '''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">40981</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-08-08</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">14:28</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/Xorg.0.log


|-
# pwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">40981</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">02-08-07</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">14:56</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var/log/Xorg.0.log.old


|-
# pwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>--------</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>-------</nowiki>


|-
# pwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">41406991</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
# history | tail -3
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 56 files


|}
#
'''Hack 45. Advanced compression using zip command.'''
## export HISTCONTROL=ignoredups
## pwd
## history | tail -4




There are 10 levels of compression provided by zip command.




# Level 0 is the lowest level, where it just archives the file without any compression.
<nowiki>[</nowiki>'''Note''': There is only one pwd command in the history, even after executing pwd 3 times as shown above]


o Level 1 will perform little compression. But, will be very fast.


=='''Hack 58. Erase duplicates across the whole history using HISTCONTROL'''==


o Level 6 is the default level of compression.


The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.


# Level 9 is the maximum compression. This will be slower when compared to default level. In my opinion, unless you are compressing a huge file, you should always use level 9.


In the example below, I used Level 0, default Level 6, and Level 9 compression on a same directory. See the compressed file size yourself.
# '''export HISTCONTROL=erasedups '''


# pwd


# zip var-log-files-default.zip /var/log/*
# service httpd stop


# zip -0 var-log-files-0.zip /var/log/*
# history | tail -3


# zip -9 var-log-files-9.zip /var/log/*
#  
## pwd
## service httpd stop
## history | tail -3


# ls -ltr  
# ls -ltr  


# service httpd stop


{| style="border-spacing:0;"
# history | tail -6
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -rw-r--r--
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2817248</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jan
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13:05</div>


|-
#
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var-log-files-default.zip
## export HISTCONTROL=erasedups
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
## pwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
## history | tail -3
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
## ls –ltr
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
## service httpd stop
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
## history | tail -6


|-
<nowiki>[</nowiki>'''Note''': The previous service httpd stop after pwd got erased]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -rw-r--r--
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">41415301</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jan
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13:05</div>


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var-log-files-0.zip
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
=='''Hack 59. Force history not to remember a particular command using HISTCONTROL'''==
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -rw-r--r--
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2582610</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jan
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13:06</div>


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| var-log-files-9.zip
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below. I can see lot of junior sysadmins getting excited about this, as they can hide a command from the history.
65'''Linux 101 Hacks'''www.thegeekstuff.com




'''Hack 46. Password Protection of Zip files'''
It is good to understand how ignorespace works. But, as a best practice, don’t hide purposefully anything from history.




Pass the option –P to the zip command to assign a password to the zip file.
# '''export HISTCONTROL=ignorespace '''


# ls –ltr


<nowiki># </nowiki>zip -P mysecurepwd var-log-protected.zip /var/log/*
# pwd


# service httpd stop


The above option is good if you are using the command inside a shell-script for background jobs. However, when you are performing the compression interactively on the command-line, you don’t want the password to be visible in the history. So, use the option –e as shown below to assign the password.
<nowiki>[</nowiki>'''Note''': There is a space at the beginning of service, to ignore this command from history]




<nowiki># zip -e var-log-protected.zip /var/log/* Enter password:</nowiki>
# history | tail -3


#
## ls –ltr
## pwd
## history | tail -3


Verify password:
=='''Hack 60. Clear all the previous history using option -c'''==




updating: var/log/acpid (deflated 81%) updating: var/log/anaconda.log (deflated 79%)
Sometime you may want to clear all the previous history. However you may still want to keep the history moving forward.




When you are uncompressing a password protected file, it will ask for the password as shown below.
<nowiki># </nowiki>'''history -c'''




<nowiki># unzip var-log-protected.zip Archive: var-log-protected.zip</nowiki>
=='''Hack 61. Substitute words from history commands'''==




<nowiki>[var-log-protected.zip] </nowiki>var/log/acpid password:
When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.




'''Hack 47. Validate a zip archive'''
In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.




Sometime you may want to validate a zip archive without extracting it. To test the validity of the zip file, pass option –t as shown below.
# ls anaconda-ks.cfg anaconda-ks.cfg


# vi !!:$


vi anaconda-ks.cfg


{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki># </nowiki>'''unzip -t var-log.zip'''</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Archive:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">var-log.zip</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| testing:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">var/log/acpid</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| OK


|-
# cp anaconda-ks.cfg anaconda-ks.cfg.bak anaconda-ks.cfg
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| testing:
# vi !^
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">var/log/anaconda.log</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| OK


|}
vi anaconda-ks.cfg
66'''Linux 101 Hacks'''www.thegeekstuff.com




testing: var/log/anaconda.syslog OK
=='''Hack 62. Substitute a specific argument for a specific command'''==




skip...
In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.


# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2


testing: var/log/wtmpOK
ls -l /really/a/very/long/path/long-filename.txt




testing: var/log/wtmp.1OK
In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.




testing: var/log/Xorg.0.logOK
<nowiki># </nowiki>ls -l !cp:$




No errors detected in compressed data of var-log.zip.
ls -l /really/a/very/long/path/long-filename.txt




'''Hack 48. Tar Command Basics'''
=='''Hack 63. Disable the usage of history using HISTSIZE'''==




tar command (tape archive) is used to convert a group of files into an archive.
If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.




<nowiki>Syntax: tar [options] [tar-archive-name] [other-file-names]</nowiki>
# '''export HISTSIZE=0 '''


# history


'''How can I create a single backup file of all files and subdirectories under my home directory?'''
# <nowiki>[</nowiki>'''Note''': History did not display anything]


=='''Hack 64. Ignore specific commands from the history using HISTIGNORE'''==


The following command creates a single archive backup file called my_home_directory.tar under /tmp. This archive will contain all the files and subdirectories under /home/jsmith.


Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history.


o Option c, stands for create an archive.


Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.


# Option v stands for verbose mode, displays additional information while executing the command.


#  
# '''export HISTIGNORE=”pwd:ls:ls –ltr:” '''
## Option f indicates the archive file name mentioned in the command.


# tar cvf /tmp/my_home_directory.tar /home/jsmith
# pwd


67'''Linux 101 Hacks'''www.thegeekstuff.com
# ls


# ls -ltr


'''How do I view all the files inside the tar archive?'''
# service httpd stop


# history | tail -3


Option t will display all the files from the tar archive.
#
## export HISTIGNORE=”pwd:ls:ls -ltr:”
## service httpd stop
## history


<nowiki>[</nowiki>'''Note''': History did not display pwd and ls]


<nowiki># </nowiki>tar tvf /tmp/my_home_directory.tar
='''Chapter 9: System Administration Tasks'''=




'''How do I extract all the files from a tar archive?'''
=='''Hack 65. Partition using fdisk'''==




Option x will extract the files from the tar archive as shown below. This will extract the content to the current directory location from where the command is executed.
After you’ve installed brand new disks on your server, you have to use tools like fdisk to partition it accordingly.




<nowiki># </nowiki>tar xvf /tmp/my_home_directory.tar
Following are the 5 typical actions (commands) that you can execute inside fdisk.




'''How do I extract tar.gz files to a specific directory?'''
o n – New Partition creation




<nowiki># tar xvfz /tmp/my_home_directory.tar.gz –C /home/ramesh</nowiki>
o d – Delete an existing partition




'''Hack 49. Combine gzip, bzip2 with tar'''
o p - Print Partition Table




'''How to use gzip with tar?'''
o w – Write the changes to the partition table. i.e save.




Add ''option z'' to the tar command when dealing with tar.gz compressed file.
o q – Quit the fdisk utility




# tar cvfz /tmp/my_home_directory.tar.gz /home/jsmith
'''Create a partition'''


# tar xvfz /tmp/my_home_directory.tar.gz


# tar tvfz /tmp/my_home_directory.tar.gz
In the following example, I created a /dev/sda1 primary partition.


'''Note: '''Using gzip is faster when compared to bzip2.68'''Linux 101 Hacks'''www.thegeekstuff.com


<nowiki># </nowiki>'''fdisk /dev/sda'''


'''How to use bzip2 with tar?'''


Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel. Changes will remain in memory only,


Add ''option j'' to the tar command when dealing with tar.bz2 compressed file.


until you decide to write them. After that, of course, the previous content won't be recoverable.


# tar cvfj /tmp/my_home_directory.tar.bz2 /home/jsmith


# tar xvfj /tmp/my_home_directory.tar.bz2
The number of cylinders for this disk is set to 34893. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with:


# tar tvfj /tmp/my_home_directory.tar.bz2


'''Note: '''Using bizp2 gives higher level of compression when compared to gzip.69'''Linux 101 Hacks'''www.thegeekstuff.com
# software that runs at boot time (e.g., old versions of LILO)
# booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)


Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)


'''Chapter 8: Command Line History'''


Command (m for help): '''p'''


When you are using Linux command line frequently, using the history effectively can be a major productivity boost. In fact, once you have mastered the 15 examples that I’ve provided here, you’ll find using command line more enjoyable and fun.


Disk /dev/sda: 287.0 GB, 287005343744 bytes 255 heads, 63 sectors/track, 34893 cylinders


'''Hack 50. Display TIMESTAMP in history using HISTTIMEFORMAT'''
Units <nowiki>= </nowiki>cylinders of 16065 <nowiki>* </nowiki>512 <nowiki>= </nowiki>8225280 bytes




Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timestamp along with the command as shown below.
Device Boot Start EndBlocks Id System




# '''export HISTTIMEFORMAT=’%F %T ‘ '''
Command (m for help): '''n'''


# history | more


#
Command action
## 2008-08-05 19:02:39 service network restart


#
## 2008-08-05 19:02:39 exit
## 2008-08-05 19:02:39 id
## 2008-08-05 19:02:39 cat /etc/redhat-release


'''Note''': You can also setup the following alias to view the recent history''' '''commands.
e extended




alias h1='history 10' alias h2='history 20' alias h3='history 30'
p primary partition (1-4)




'''Hack 51. Search the history using Control+R'''
'''p'''




I strongly believe that this may be your most frequently used feature of history. When you’ve already executed a very long command, you can simply70'''Linux 101 Hacks'''www.thegeekstuff.com
Partition number (1-4): '''1'''




search history using a keyword and re-execute the same command without having to type it fully. Press Control+R and type the keyword.
First cylinder (1-34893, default 1): Using default value 1


Last cylinder or +size or +sizeM or +sizeK (1-34893, default 34893):


In the following example, I searched for red, which displayed the previous command “cat /etc/redhat-release” in the history that contained the word red.


Using default value 34893


<nowiki># </nowiki><nowiki>[</nowiki>'''Note''': Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt as shown below]


Command (m for help): '''w'''


'''(reverse-i-search)`red‘: cat /etc/redhat-release'''


The partition table has been altered!


<nowiki>[Note: Press enter when you see your command, which will execute the command from the history]</nowiki>


Calling ioctl() to re-read partition table.


<nowiki># </nowiki>'''cat /etc/redhat-release'''


Syncing disks.


Fedora release 9 (Sulphur)


'''Verify that the partition got created successfully'''


Sometimes you want to edit a command from history before executing it. For e.g. you can search for httpd, which will display service httpd stop from the command history, select this command and change the stop to start and re-execute it again as shown below.


<nowiki># </nowiki>'''fdisk /dev/sda'''


<nowiki># </nowiki><nowiki>[</nowiki>'''Note''': Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]


'''(reverse-i-search)`httpd‘: service httpd stop'''
The number of cylinders for this disk is set to 34893. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with:




<nowiki>[Note: Press either left arrow or right arrow key when you see your command, which will display the command for you to edit, before executing it]</nowiki>
# software that runs at boot time (e.g., old versions of LILO)
# booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)


'''Command (m for help): p'''


'''<nowiki># </nowiki>service httpd start'''71'''Linux 101 Hacks'''www.thegeekstuff.com


Disk /dev/sda: 287.0 GB, 287005343744 bytes 255 heads, 63 sectors/track, 34893 cylinders


'''Hack 52. Repeat previous command quickly using 4 different methods'''
Units <nowiki>= </nowiki>cylinders of 16065 <nowiki>* </nowiki>512 <nowiki>= </nowiki>8225280 bytes




Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command.


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Device Boot
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Start</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">End</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Blocks</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Id
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| System


# Use the '''up arrow''' to view the previous command and press enter to execute it.  
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sda1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">34893</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">280277991</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 83
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux


# '''Type !! '''and press enter from the command line''' '''
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Command (m for help): '''q'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


# '''Type !-1 '''and press enter from the command line.''' '''
|}


# '''Press Control+P '''will display the previous command, press enter''' '''to execute it
=='''Hack 66. Format a partition using mke2fsk'''==


'''Hack 53. Execute a specific command from history'''


After partitioning the disks, it is still not ready for usage, as we need to format the disk. At this stage, if you try to view the disk information, it will give the following error message indicating that no valid superblock is present.


In the following example, If you want to repeat the command #4, execute !4 as shown below.


<nowiki># </nowiki>'''tune2fs -l /dev/sda1'''


# history | more


#
tune2fs 1.35 (28-Feb-2004)
## service network restart
## exit
## id
## cat /etc/redhat-release


# '''!4 '''


cat /etc/redhat-release Fedora release 9 (Sulphur)72'''Linux 101 Hacks'''www.thegeekstuff.com
tune2fs: Bad magic number in super-block while trying to open /dev/sda1




'''Hack 54. Execute previous command that starts with a specific word'''
Couldn't find valid filesystem superblock.




Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example, typing !ps and enter, executed the previous command starting with ps, which is ‘ps aux | grep yp’.
To format the disk, use mke2fs as shown below.
 


<nowiki># </nowiki>'''mke2fs /dev/sda1'''




{| style="border-spacing:0;"
You can also pass the following optional parameter to the mke2fs.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''!ps'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ps aux | grep yp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">root</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>16947</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">36516</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1264</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ?


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sl
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13:10</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>0:00 ypbind</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
# -m 0 : reserved-blocks-percentage – This indicates the percentage of the filesystem blocks reserved for the root user. Default is 5%. In the following example, it is set to 0.  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">root</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>17503</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4124</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">740</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/0


|-
#
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S+
## -b 4096 : block-size specified in bytes. Valid values are 1024, 2048 and 4096 bytes per block.  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">19:19</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 grep
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| yp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
# '''mke2fs -m 0 -b 4096 /dev/sda1 '''
'''Hack 55. Control the total number of lines in the history using HISTSIZE'''


mke2fs 1.35 (28-Feb-2004) Filesystem label=


Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 450 command will be stored in the bash history.


OS type: Linux


<nowiki># </nowiki>vi ~/.bash_profile


Block size=4096 (log=2) Fragment size=4096 (log=2) 205344 inodes, 70069497 blocks


HISTSIZE=450


0 blocks (0.00%) reserved for the super user First data block=0


HISTFILESIZE=450
Maximum filesystem blocks=71303168 2139 block groups


32768 blocks per group, 32768 fragments per group


'''Hack 56. Change the history file name using HISTFILE'''


96 inodes per group


By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .commandline_warrior file instead of .bash_history file. I’m yet to figure out a practical use for this. I can see this getting used when you want to track commands executed from different terminals using different history file name.73'''Linux 101 Hacks'''www.thegeekstuff.com


Superblock backups stored on blocks:


<nowiki># vi ~/.bash_profile HISTFILE=/root/.commandline_warrior</nowiki>


32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872


'''Hack 57. Eliminate the continuous repeated entry from history using HISTCONTROL'''


Writing inode tables: done


In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.


Writing superblocks and filesystem accounting information: done


# pwd


# pwd
This filesystem will be automatically checked every 32 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.


# pwd


# history | tail -4
The above command will create an ext2 filesystem. To create an ext3 file system do the following:


#
## pwd
## pwd
## pwd
## history | tail -4


<nowiki>[</nowiki>'''Note''': There are three pwd commands in history, after executing pwd 3 times as shown above]
# '''mkfs.ext3 /dev/sda1 '''


# '''mke2fs –j /dev/sda1 '''


# '''export HISTCONTROL=ignoredups '''


# pwd


# pwd


# pwd
=='''Hack 67. Mount the partition'''==


# history | tail -3


#
After creating a partition and formatting, you can mount it to a mount point.
## export HISTCONTROL=ignoredups
## pwd
## history | tail -4


74'''Linux 101 Hacks'''www.thegeekstuff.com


First create a directory where the partition should be mounted.


<nowiki>[</nowiki>'''Note''': There is only one pwd command in the history, even after executing pwd 3 times as shown above]


# mkdir /home/database


'''Hack 58. Erase duplicates across the whole history using HISTCONTROL'''
Mount the file system.  




The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.
# '''mount /dev/sda1 /home/database '''


To automatically mount the filesystem after the reboot, add the following entry to the /etc/fstab


# '''export HISTCONTROL=erasedups '''


# pwd
/dev/sdaa /home/database ext3 defaults 0 2


# service httpd stop


# history | tail -3
=='''Hack 68. Fine tune the partition using tune2fs'''==


#
## pwd
## service httpd stop
## history | tail -3


# ls -ltr
Use the tune2fs –l /dev/sda1 to view the filesystem information as shown below.


# service httpd stop


# history | tail -6
<nowiki># </nowiki>'''tune2fs -l /dev/sda1''' tune2fs 1.35 (28-Feb-2004)


#
## export HISTCONTROL=erasedups
## pwd
## history | tail -3
## ls –ltr
## service httpd stop
## history | tail -6


<nowiki>[</nowiki>'''Note''': The previous service httpd stop after pwd got erased]75'''Linux 101 Hacks'''www.thegeekstuff.com
Filesystem volume name: /home/database




'''Hack 59. Force history not to remember a particular command using HISTCONTROL'''
Last mounted on:<nowiki><not </nowiki>available>




When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below. I can see lot of junior sysadmins getting excited about this, as they can hide a command from the history.
Filesystem UUID:f1234556-e123-1234-abcd-




It is good to understand how ignorespace works. But, as a best practice, don’t hide purposefully anything from history.
bbbbaaaaae11




# '''export HISTCONTROL=ignorespace '''
Filesystem magic number: 0xEF44


# ls –ltr


# pwd
Filesystem revision <nowiki>#:</nowiki>1 (dynamic)
 
# service httpd stop


<nowiki>[</nowiki>'''Note''': There is a space at the beginning of service, to ignore this command from history]


Filesystem features:resize_inode filetype


# history | tail -3


#
sparse_super
## ls –ltr
## pwd
## history | tail -3


'''Hack 60. Clear all the previous history using option -c'''


Default mount options:(none)


Sometime you may want to clear all the previous history. However you may still want to keep the history moving forward.


Filesystem state:not clean84
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">www.thegeekstuff.com</div>


<nowiki># </nowiki>'''history -c'''76'''Linux 101 Hacks'''www.thegeekstuff.com
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Errors behavior:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Continue
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Filesystem OS type:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


'''Hack 61. Substitute words from history commands'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode count:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1094912
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Block count:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 140138994
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved block count:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free blocks:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16848481
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free inodes:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1014969
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| First block:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


# ls anaconda-ks.cfg anaconda-ks.cfg
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Block size:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2048
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


# vi !!:$
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Fragment size:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2048
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


vi anaconda-ks.cfg
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved GDT blocks:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 512
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Blocks per group:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16384
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Fragments per group:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16384
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inodes per group:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 128
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


# cp anaconda-ks.cfg anaconda-ks.cfg.bak anaconda-ks.cfg
|-
# vi !^
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode blocks per group:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


vi anaconda-ks.cfg
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Filesystem created:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Tue Jul
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1 00:06:03 2008</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Last mount time:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Thu Aug 21 05:58:25 2008


'''Hack 62. Substitute a specific argument for a specific command'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Last write time:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Fri Jan
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2 15:40:36 2009</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mount count:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.77'''Linux 101 Hacks'''www.thegeekstuff.com
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Maximum mount count:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 20
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Last checked:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Tue Jul
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1 00:06:03 2008</div>


# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
|-
# ls -l !cp:2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Check interval:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 15552000 (6 months)


ls -l /really/a/very/long/path/long-filename.txt
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Next check after:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sat Dec 27 23:06:03 2008


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved blocks uid:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 (user root)


In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved blocks gid:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 (group root)


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| First inode:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 11
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


<nowiki># </nowiki>ls -l !cp:$
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode size:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 128
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Default directory hash:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tea
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


ls -l /really/a/very/long/path/long-filename.txt
|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Directory Hash Seed:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12345829-1236-4123-9aaa-


'''Hack 63. Disable the usage of history using HISTSIZE'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ccccc123292b
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
You can also use the tune2fs to tune the ex2/ext3 filesystem parameter. For example, if you want to change the Filesystem volume name, you can do it as shown below.


If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.


# '''tune2fs -l /dev/sda1 | grep volume '''


# '''export HISTSIZE=0 '''
Filesystem volume name: /home/database


# history


# <nowiki>[</nowiki>'''Note''': History did not display anything]
# '''tune2fs -L database-home /dev/emcpowera1 '''tune2fs 1.35 (28-Feb-2004)


'''Hack 64. Ignore specific commands from the history using HISTIGNORE'''




Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history.78'''Linux 101 Hacks'''www.thegeekstuff.com
<nowiki># </nowiki>'''tune2fs -l /dev/sda1 | grep volume'''




Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.
Filesystem volume name: database-home




# '''export HISTIGNORE=”pwd:ls:ls –ltr:” '''
=='''Hack 69. Create a swap file system.'''==


# pwd


# ls
Create a file for swap usage as shown below.


# ls -ltr


# service httpd stop
# '''dd if=/dev/zero of=/home/swap-fs bs=1M count=512 '''


# history | tail -3
512+0 records in


#
## export HISTIGNORE=”pwd:ls:ls -ltr:”
## service httpd stop
## history


<nowiki>[</nowiki>'''Note''': History did not display pwd and ls]79'''Linux 101 Hacks'''www.thegeekstuff.com
512+0 records out




'''Chapter 9: System Administration Tasks'''
# ls -l /home/swap-fs


-rw-r--r-- 1 root root 536870912 Jan 2 23:13 /home/swap-fs


'''Hack 65. Partition using fdisk'''


Use mkswap to setup a Linux swap area in the /home/swap-fs file that was created above.


After you’ve installed brand new disks on your server, you have to use tools like fdisk to partition it accordingly.


<nowiki># </nowiki>'''mkswap /home/swap-fs'''


Following are the 5 typical actions (commands) that you can execute inside fdisk.


Setting up swapspace version 1, size <nowiki>= </nowiki>536866 kB


o n – New Partition creation


Once the file is created and has been setup for Linux swap area, it is time to enable the swap using swapon as shown below.


o d – Delete an existing partition


<nowiki># </nowiki>'''swapon /home/swap-fs'''


o p - Print Partition Table


Add the following line to /etc/fstab and reboot the system for the swap to take into effect.


o w – Write the changes to the partition table. i.e save.


/home/swap-fs swap swap defaults 0 0


o q – Quit the fdisk utility


=='''Hack 70. Create a new user'''==


'''Create a partition'''


'''Add a new user – Basic method'''


In the following example, I created a /dev/sda1 primary partition.


Specify only the user name.


<nowiki># </nowiki>'''fdisk /dev/sda'''


<nowiki># </nowiki>useradd jsmith


Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel. Changes will remain in memory only,


'''Add a new user with additional Parameter'''


until you decide to write them. After that, of course, the previous content won't be recoverable.


You can also specify the following parameter to the useradd o -c : Description about the user.


The number of cylinders for this disk is set to 34893. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with:80'''Linux 101 Hacks'''www.thegeekstuff.com


#
## -e : expiry date of the user in mm/dd/yy format


# software that runs at boot time (e.g., old versions of LILO)
# '''adduser -c "John Smith - Oracle Developer" -e 12/31/09 jsmith '''
# booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)


Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Verify that the user got added successfully.




Command (m for help): '''p'''
<nowiki># grep jsmith /etc/passwd jsmith:x:510:510:John Smith - Oracle Developer:/home/jsmith:/bin/bash</nowiki>




Disk /dev/sda: 287.0 GB, 287005343744 bytes 255 heads, 63 sectors/track, 34893 cylinders
'''Change the user password.'''


Units <nowiki>= </nowiki>cylinders of 16065 <nowiki>* </nowiki>512 <nowiki>= </nowiki>8225280 bytes


<nowiki># </nowiki>'''passwd jsmith'''


Device Boot Start EndBlocks Id System


Changing password for user jsmith. New UNIX password:


Command (m for help): '''n'''


BAD PASSWORD: it is based on a dictionary word Retype new UNIX password:


Command action
passwd: all authentication tokens updated successfully.


'''Note: '''Make sure to follow''' '''[http://www.thegeekstuff.com/2008/06/the-ultimate-guide-for-creating-strong-passwords/ ][http://www.thegeekstuff.com/2008/06/the-ultimate-guide-for-creating-strong-passwords/ these best practices][http://www.thegeekstuff.com/2008/06/the-ultimate-guide-for-creating-strong-passwords/  ]to''' '''create a strong password''' '''for the user.


e extended


'''How to identify the default values used by useradd?'''


p primary partition (1-4)


Following are the default values that will be used when an user is created.


'''p'''


<nowiki># </nowiki>'''useradd –D'''


Partition number (1-4): '''1'''


GROUP=100


First cylinder (1-34893, default 1): Using default value 1


Last cylinder or +size or +sizeM or +sizeK (1-34893, default 34893):
HOME=/home




Using default value 34893
INACTIVE=-1




Command (m for help): '''w'''
EXPIRE=




The partition table has been altered!
SHELL=/bin/bash




Calling ioctl() to re-read partition table.
SKEL=/etc/skel




Syncing disks.
=='''Hack 71. Create a new group and assign to an user'''==




'''Verify that the partition got created successfully'''
'''Create a new developer group.'''




<nowiki># </nowiki>'''fdisk /dev/sda'''
<nowiki># </nowiki>groupadd developers




The number of cylinders for this disk is set to 34893. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with:81'''Linux 101 Hacks'''www.thegeekstuff.com
'''Validate that the group was created successfully.'''




# software that runs at boot time (e.g., old versions of LILO)
<nowiki># grep developer /etc/group developers:x:511:</nowiki>
# booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)


'''Command (m for help): p'''


'''Add an user to an existing group.'''


Disk /dev/sda: 287.0 GB, 287005343744 bytes 255 heads, 63 sectors/track, 34893 cylinders


Units <nowiki>= </nowiki>cylinders of 16065 <nowiki>* </nowiki>512 <nowiki>= </nowiki>8225280 bytes
You cannot use useradd to modify an existing user, as you’ll get the following error message.


# useradd -G developers jsmith useradd: user jsmith exists


# usermod -g developers jsmith


{| style="border-spacing:0;"
'''Validate the users group was modified successfully.'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Device Boot
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Start</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">End</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Blocks</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Id
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| System


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sda1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">34893</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">280277991</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 83
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux


|-
<nowiki># grep jsmith /etc/passwd jsmith:x:510:511:Oracle Developer:/home/jsmith:/bin/bash</nowiki>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Command (m for help): '''q'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
'''Hack 66. Format a partition using mke2fsk'''


<nowiki># </nowiki>'''id jsmith'''


After partitioning the disks, it is still not ready for usage, as we need to format the disk. At this stage, if you try to view the disk information, it will give the following error message indicating that no valid superblock is present.


uid=510(jsmith) gid=511(developers) groups=511(developers)


<nowiki># </nowiki>'''tune2fs -l /dev/sda1'''


<nowiki># grep jsmith /etc/group jsmith:x:510: developers:x:511:jsmith</nowiki>


tune2fs 1.35 (28-Feb-2004)


=='''Hack 72. Setup SSH passwordless login in OpenSSH'''==


tune2fs: Bad magic number in super-block while trying to open /dev/sda1


You can login to a remote Linux server without entering password in 3 simple steps using ssky-keygen and ssh-copy-id as explained in this example.


Couldn't find valid filesystem superblock.


ssh-keygen creates the public and private keys. ssh-copy-id copies the local-host’s public key to the remote-host’s authorized_keys file. ssh-copy-id also assigns proper permission to the remote-host’s home, ~/.ssh, and ~/.ssh/authorized_keys.


To format the disk, use mke2fs as shown below.


'''Step 1: Create public and private keys using ssh-key-gen on local-host'''


<nowiki># </nowiki>'''mke2fs /dev/sda1'''


jsmith@local-host$ '''ssh-keygen'''


You can also pass the following optional parameter to the mke2fs.


Generating public/private rsa key pair.


82


'''Linux 101 Hacks'''www.thegeekstuff.com
<nowiki>Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]</nowiki>




# -m 0 : reserved-blocks-percentage – This indicates the percentage of the filesystem blocks reserved for the root user. Default is 5%. In the following example, it is set to 0.
<nowiki>Enter passphrase (empty for no passphrase): [Press enter key]</nowiki>


#
## -b 4096 : block-size specified in bytes. Valid values are 1024, 2048 and 4096 bytes per block.


# '''mke2fs -m 0 -b 4096 /dev/sda1 '''
<nowiki>Enter same passphrase again: [Pess enter key] Your identification has been saved in /home/jsmith/.ssh/id_rsa.</nowiki>


mke2fs 1.35 (28-Feb-2004) Filesystem label=


Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub. The key fingerprint is:


OS type: Linux


33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host


Block size=4096 (log=2) Fragment size=4096 (log=2) 205344 inodes, 70069497 blocks


'''Step 2: Copy the public key to remote-host using ssh-copy-id'''


0 blocks (0.00%) reserved for the super user First data block=0


Maximum filesystem blocks=71303168 2139 block groups
jsmith@local-host$ '''ssh-copy-id -i ~/.ssh/id_rsa.pub''' '''remote-host'''


32768 blocks per group, 32768 fragments per group


jsmith@remote-host’s password:


96 inodes per group
 
Now try logging into the machine, with “ssh ‘remote-host’”, and check in:




Superblock backups stored on blocks:
.ssh/authorized_keys to make sure we haven’t added extra keys that you weren’t expecting.




32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872
'''Note: '''ssh-copy-id appends the keys to the remote-host’s




Writing inode tables: done
.ssh/authorized_key.




Writing superblocks and filesystem accounting information: done
'''Step 3: Login to remote-host without entering the password'''




This filesystem will be automatically checked every 32 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
jsmith@local-host$ '''ssh remote-host'''
 


Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2 <nowiki>[</nowiki>'''Note''': SSH did not ask for password.] jsmith@remote-host$ <nowiki>[</nowiki>'''Note''': You are on remote-host here]


The above command will create an ext2 filesystem. To create an ext3 file system do the following:


=='''Hack 73. Use ssh-copy-id along with ssh-agent'''==


# '''mkfs.ext3 /dev/sda1 '''


# '''mke2fs –j /dev/sda1 '''
'''Using ssh-copy-id along with the ssh-add/ssh-agent'''


83'''Linux 101 Hacks'''www.thegeekstuff.com


When no value is passed for the option -i and If ~/.ssh/identity.pub is not available, ssh-copy-id will display the following error message.


'''Hack 67. Mount the partition'''


jsmith@local-host$ ssh-copy-id -i remote-host


After creating a partition and formatting, you can mount it to a mount point.


/usr/bin/ssh-copy-id: ERROR: No identities found


First create a directory where the partition should be mounted.


If you have loaded keys to the ssh-agent using the ssh-add, then ssh-copy-id will get the keys from the ssh-agent to copy to the remote-host. i.e, it copies the keys provided by ssh-add -L command to the remote-host, when you don’t pass option -i to the ssh-copy-id.


# mkdir /home/database


Mount the file system.
jsmith@local-host$ ssh-agent $SHELL




# '''mount /dev/sda1 /home/database '''
jsmith@local-host$ ssh-add -L The agent has no identities.


To automatically mount the filesystem after the reboot, add the following entry to the /etc/fstab


jsmith@local-host$ ssh-add


/dev/sdaa /home/database ext3 defaults 0 2


Identity added: /home/jsmith/.ssh/id_rsa (/home/jsmith/.ssh/id_rsa)


'''Hack 68. Fine tune the partition using tune2fs'''


jsmith@local-host$ ssh-add -L ssh-rsa


Use the tune2fs –l /dev/sda1 to view the filesystem information as shown below.


AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79D aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow== /home/jsmith/.ssh/id_rsa


<nowiki># </nowiki>'''tune2fs -l /dev/sda1''' tune2fs 1.35 (28-Feb-2004)


jsmith@local-host$ ssh-copy-id -i remote-host jsmith@remote-host’s password:


Filesystem volume name: /home/database


Now try logging into the machine, with “ssh ‘remote-host’”, and check in: .ssh/authorized_keys to make sure we haven’t added extra keys that you weren’t expecting. <nowiki>[</nowiki>'''Note''': This has added the key displayed by ssh-add -L]


Last mounted on:<nowiki><not </nowiki>available>
'''Three Minor Annoyances of ssh-copy-id'''




Filesystem UUID:f1234556-e123-1234-abcd-
Following are few minor annoyances of the ssh-copy-id.




bbbbaaaaae11
# Default public key: ssh-copy-id uses ~/.ssh/identity.pub as the default public key file (i.e when no value is passed to option -i). Instead, I wish it uses id_dsa.pub, or id_rsa.pub, or identity.pub as default keys. i.e If any one of them exist, it should copy that to the remote-host. If two or three of them exist, it should copy identity.pub as default.


# The agent has no identities: When the ssh-agent is running and the ssh-add -L returns “The agent has no identities” (i.e no keys are added to the ssh-agent), the ssh-copy-id will still copy the message “The agent has no identities” to the remote-host’s authorized_keys entry.


Filesystem magic number: 0xEF44
# Duplicate entry in authorized_keys: I wish ssh-copy-id validates duplicate entry on the remote-host’s authorized_keys. If you execute ssh-copy-id multiple times on the local-host, it will keep appending the same key on the remote-host’s authorized_keys file without checking for duplicates. Even with duplicate entries everything works as expected. But, I would like to have my authorized_keys file clutter free.


=='''Hack 74. Crontab'''==


Filesystem revision <nowiki>#:</nowiki>1 (dynamic)


Using cron you can execute a shell-script or Linux commands at a specific time and date. For example a sysadmin can schedule a backup job that can run every day.


Filesystem features:resize_inode filetype


'''How to add a job to the cron?'''


sparse_super


<nowiki># </nowiki>crontab –e


Default mount options:(none)


0 5 <nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/backup.sh
This will execute /root/bin/backup.sh at 5 a.m every day.


Filesystem state:not clean84
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">www.thegeekstuff.com</div>


|-
'''Description of Cron fields.'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Errors behavior:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Continue
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Filesystem OS type:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
Following is the format of the crontab file.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode count:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1094912
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Block count:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 140138994
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved block count:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free blocks:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16848481
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
o minute: Allowed range 0 – 59 o hour: Allowed range 0 – 23
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free inodes:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1014969
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| First block:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
o day-of-month: Allowed range 0 – 31
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Block size:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2048
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Fragment size:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2048
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
o month: Allowed range 1 – 12. 1 = January. 12 = December. o Day-of-week: Allowed range 0 – 7. Sunday is either 0 or 7.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved GDT blocks:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 512
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Blocks per group:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16384
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
'''Crontab examples'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Fragments per group:
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16384
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
# Run at 12:01 a.m. 1 minute after midnight everyday. This is a good time to run backup when the system is not under load.  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inodes per group:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 128
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
#
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode blocks per group:
## 0 <nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/backup.sh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
# Run backup every weekday (Mon – Fri) at 11:59 p.m.  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Filesystem created:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Tue Jul
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1 00:06:03 2008</div>


|-
#
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Last mount time:
## 11 <nowiki>* </nowiki><nowiki>* </nowiki>1,2,3,4,5 /root/bin/backup.sh
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Thu Aug 21 05:58:25 2008


|-
Following will also do the same.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Last write time:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Fri Jan
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2 15:40:36 2009</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mount count:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
59 11 <nowiki>* </nowiki><nowiki>* </nowiki>1-5 /root/bin/backup.sh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Maximum mount count:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 20
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Last checked:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Tue Jul
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1 00:06:03 2008</div>


|-
# Execute the command every 5 minutes.  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Check interval:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 15552000 (6 months)


|-
<nowiki>*/5 </nowiki><nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/check-status.sh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Next check after:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sat Dec 27 23:06:03 2008


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved blocks uid:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 (user root)


|-
# Execute at 1:10 p.m on 1st of every month
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved blocks gid:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 (group root)


|-
#
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| First inode:
## 13 1 <nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/full-backup.sh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 11
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
# Execute 11 p.m on weekdays.  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode size:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 128
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
#
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Default directory hash:
## 23 <nowiki>* </nowiki><nowiki>* </nowiki>1-5 /root/bin/incremental-backup.sh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tea
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
'''Crontab Options '''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Directory Hash Seed:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12345829-1236-4123-9aaa-


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ccccc123292b
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
Following are the available options with crontab:
You can also use the tune2fs to tune the ex2/ext3 filesystem parameter. For example, if you want to change the Filesystem volume name, you can do it as shown below.




# '''tune2fs -l /dev/sda1 | grep volume '''
# crontab –e : Edit the crontab file. This will create a crontab, if it doesn’t exist


Filesystem volume name: /home/database
o crontab –l : Display the crontab file. o crontab -r : Remove the crontab file.




# '''tune2fs -L database-home /dev/emcpowera1 '''tune2fs 1.35 (28-Feb-2004)
o crontab -ir : This will prompt user before deleting a crontab.


85'''Linux 101 Hacks'''www.thegeekstuff.com


=='''Hack 75. Safe Reboot Of Linux Using Magic SysRq Key'''==


<nowiki># </nowiki>'''tune2fs -l /dev/sda1 | grep volume'''


The magic SysRq key is a key combination in the Linux kernel which allows the user to perform various low level commands regardless of the system’s state.


Filesystem volume name: database-home


It is often used to recover from freezes, or to reboot a computer without corrupting the filesystem. The key combination consists of


'''Hack 69. Create a swap file system.'''
Alt+SysRq+commandkey. In many systems the SysRq key is the printscreen key.




Create a file for swap usage as shown below.
First, you need to enable the SysRq key, as shown below.




# '''dd if=/dev/zero of=/home/swap-fs bs=1M count=512 '''
echo "1" > /proc/sys/kernel/sysrq


512+0 records in


'''List of SysRq Command Keys'''


512+0 records out


Following are the command keys available for Alt+SysRq+commandkey. o ‘k’ – Kills all the process running on the current virtual console. o ’s’ – This will attempt to sync all the mounted file system.


# ls -l /home/swap-fs


-rw-r--r-- 1 root root 536870912 Jan 2 23:13 /home/swap-fs
# ‘b’ - Immediately reboot the system, without unmounting partitions or syncing.


o ‘e’ – Sends SIGTERM to all process except init.


Use mkswap to setup a Linux swap area in the /home/swap-fs file that was created above.


o ‘m’ – Output current memory information to the console. o ‘i’ - Send the SIGKILL signal to all processes except init


<nowiki># </nowiki>'''mkswap /home/swap-fs'''


# ‘r’ - Switch the keyboard from raw mode (the mode used by programs such as X11), to XLATE mode.


Setting up swapspace version 1, size <nowiki>= </nowiki>536866 kB
o ’s’ – sync all mounted file system.




Once the file is created and has been setup for Linux swap area, it is time to enable the swap using swapon as shown below.
# ‘t’ - Output a list of current tasks and their information to the console.  


o ‘u’ - Remount all mounted filesystems in readonly mode. o ‘o’ – Shutdown the system immediately.


<nowiki># </nowiki>'''swapon /home/swap-fs'''


o ‘p’ – Print the current registers and flags to the console.


Add the following line to /etc/fstab and reboot the system for the swap to take into effect.


# ‘0-9′ - Sets the console log level, controlling which kernel messages will be printed to your console.


/home/swap-fs swap swap defaults 0 086'''Linux 101 Hacks'''www.thegeekstuff.com




'''Hack 70. Create a new user'''
o ‘f’ - Will call oom_kill to kill process which takes more memory.




'''Add a new user Basic method'''
# ‘h’ Used to display the help. But any other keys than the above listed will print help.


We can also do this by echoing the keys to the /proc/sysrq-trigger file. For example, to re-boot a system you can perform the following.


Specify only the user name.


echo "b" > /proc/sysrq-trigger


<nowiki># </nowiki>useradd jsmith


'''Perform a Safe reboot of Linux using Magic SysRq Key'''


'''Add a new user with additional Parameter'''


To perform a safe reboot of a Linux computer which hangs up, do the following. This will avoid the fsck during the next re-booting. i.e Press Alt+SysRq+letter highlighted below.


You can also specify the following parameter to the useradd o -c : Description about the user.


o un'''R'''aw (take control of keyboard back from X11,


#
## -e : expiry date of the user in mm/dd/yy format


# '''adduser -c "John Smith - Oracle Developer" -e 12/31/09 jsmith '''
# t'''E'''rminate (send SIGTERM to all processes, allowing them to terminate gracefully),


Verify that the user got added successfully.
# '''k'''Ill (send SIGILL to all processes, forcing them to terminate''' '''immediately),


o '''S'''ync (flush data to disk),


<nowiki># grep jsmith /etc/passwd jsmith:x:510:510:John Smith - Oracle Developer:/home/jsmith:/bin/bash</nowiki>


o '''U'''nmount (remount all filesystems read-only), o re'''B'''oot.


'''Change the user password.'''
='''Chapter 10: Apachectl and Httpd'''=




<nowiki># </nowiki>'''passwd jsmith'''
'''Examples'''




Changing password for user jsmith. New UNIX password:
After you have installed Apache2, if you want to use apachectl and httpd to it’s maximum potential, you should go beyond using start, stop and restart. The 9 practical examples provided in this chapter will help you to use apachectl and httpd very effectively.




BAD PASSWORD: it is based on a dictionary word Retype new UNIX password:
Apachectl acts as SysV init script, taking arguments like start, stop, restart and status. It also acts as front-end to httpd command, by simply passing the command line arguments to httpd. So, all the commands you execute using apachectl, can also be executed directly by calling httpd.


passwd: all authentication tokens updated successfully.87'''Linux 101 Hacks'''www.thegeekstuff.com


If you don’t have Apache, refer to the tutorials: [http://www.thegeekstuff.com/2008/07/install-apache-2-from-source-on-linux/ ][http://www.thegeekstuff.com/2008/07/install-apache-2-from-source-on-linux/ install apache from source][http://www.thegeekstuff.com/2008/07/install-apache-2-from-source-on-linux/  ]or [http://www.thegeekstuff.com/2008/09/how-to-install-or-upgrade-lamp-apache-mysql-and-php-stack-on-linux-using-yum/ ][http://www.thegeekstuff.com/2008/09/how-to-install-or-upgrade-lamp-apache-mysql-and-php-stack-on-linux-using-yum/ install LAMP stack using yum].


'''Note: '''Make sure to follow''' '''[http://www.thegeekstuff.com/2008/06/the-ultimate-guide-for-creating-strong-passwords/ ][http://www.thegeekstuff.com/2008/06/the-ultimate-guide-for-creating-strong-passwords/ these best practices][http://www.thegeekstuff.com/2008/06/the-ultimate-guide-for-creating-strong-passwords/  ]to''' '''create a strong password''' '''for the user.


=='''Hack 76. Pass different httpd.conf filename to apachectl'''==


'''How to identify the default values used by useradd?'''


Typically you’ll modify the original httpd.conf to try out different Apache directives. If something doesn’t work out, you’ll revert back the changes. Instead of playing around with the original httpd.conf, copy it to a new httpd.conf.debug and use this new httpd.conf.debug file with Apache for testing purpose as shown below using option -f.


Following are the default values that will be used when an user is created.


# '''apachectl -f conf/httpd.conf.debug '''


<nowiki># </nowiki>'''useradd –D'''
# '''httpd -k start -f conf/httpd.conf.debug '''


<nowiki>[</nowiki>'''Note''': you can use either apachectl or httpd as shown above]


GROUP=100


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>ps
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">-ef | grep http</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


HOME=/home
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25080</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 23:26 00:00:00 /usr/sbin/httpd -f


|}


INACTIVE=-1




EXPIRE=
conf/httpd.conf.debug




SHELL=/bin/bash
apache 25099 25080 0 23:28 00:00:00 /usr/sbin/httpd -f conf/httpd.conf.debug




SKEL=/etc/skel
<nowiki>[</nowiki>'''Note''': ps shows the httpd running with httpd.conf.debug file]




'''Hack 71. Create a new group and assign to an user'''
Once you are satisfied with the changes and Apache runs without any problem with httpd.conf.debug, you can copy the changes to httpd.conf and start the Apache normally as shown below.




'''Create a new developer group.'''
# cp httpd.conf.debug httpd.conf


# apachectl stop


<nowiki># </nowiki>groupadd developers
# apachectl start


# ps -ef | grep httpd


'''Validate that the group was created successfully.'''


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25114</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 23:28 00:00:00 /usr/sbin/httpd


<nowiki># grep developer /etc/group developers:x:511:</nowiki>
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -k start
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| daemon
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25115 25114</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 23:28 00:00:00 /usr/sbin/httpd


'''Add an user to an existing group.'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -k start
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
<nowiki>[</nowiki>'''Note''': ps indicates that the httpd is running using the default config file]


You cannot use useradd to modify an existing user, as you’ll get the following error message.88'''Linux 101 Hacks'''www.thegeekstuff.com


=='''Hack 77. Use a temporary DocumentRoot without modifying httpd.conf'''==


# useradd -G developers jsmith useradd: user jsmith exists


# usermod -g developers jsmith
This is very helpful, when you are trying out different layout for your website and don’t want to modify the original files under the default DocumentRoot.


'''Validate the users group was modified successfully.'''


Take a copy of your original DocumentRoot directory (/var/www/html) to a new temporary DocumentRoot directory (/var/www/html_debug). Make all your changes under this temporary DocumentRoot directory (/var/www/html_debug) and start the Apache with this temporary directory as shown below using option -c.


<nowiki># grep jsmith /etc/passwd jsmith:x:510:511:Oracle Developer:/home/jsmith:/bin/bash</nowiki>


<nowiki># </nowiki>'''httpd -k start -c “DocumentRoot /var/www/html_debug/”'''


<nowiki># </nowiki>'''id jsmith'''


If you want to go back to original configuration using the default DocumentRoot (/var/www/html), simply restart the Apache as shown below.


uid=510(jsmith) gid=511(developers) groups=511(developers)


# httpd -k stop


<nowiki># grep jsmith /etc/group jsmith:x:510: developers:x:511:jsmith</nowiki>
# apachectl start


=='''Hack 78. Increase the Log Level temporarily'''==


'''Hack 72. Setup SSH passwordless login in OpenSSH'''


While you are debugging an issue, you can change the LogLevel of the Apache temporarily, without modifying the LogLevel directive in the httpd.conf as shown below using option -e. In this example, the LogLevel is set to debug.


You can login to a remote Linux server without entering password in 3 simple steps using ssky-keygen and ssh-copy-id as explained in this example.


<nowiki># </nowiki>'''httpd -k start -e debug'''


ssh-keygen creates the public and private keys. ssh-copy-id copies the local-host’s public key to the remote-host’s authorized_keys file. ssh-copy-id also assigns proper permission to the remote-host’s home, ~/.ssh, and ~/.ssh/authorized_keys.


<nowiki>[Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_basic_module</nowiki>


'''Step 1: Create public and private keys using ssh-key-gen on local-host'''


<nowiki>[Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_digest_module</nowiki>


jsmith@local-host$ '''ssh-keygen'''


Possible values you can pass to option -e are:


Generating public/private rsa key pair.89'''Linux 101 Hacks'''www.thegeekstuff.com


o debug


<nowiki>Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]</nowiki>


o info


<nowiki>Enter passphrase (empty for no passphrase): [Press enter key]</nowiki>


o notice


<nowiki>Enter same passphrase again: [Pess enter key] Your identification has been saved in /home/jsmith/.ssh/id_rsa.</nowiki>


o warn


Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub. The key fingerprint is:


o error


33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host


o crit


'''Step 2: Copy the public key to remote-host using ssh-copy-id'''


o alert


jsmith@local-host$ '''ssh-copy-id -i ~/.ssh/id_rsa.pub''' '''remote-host'''


o emerg


jsmith@remote-host’s password:


=='''Hack 79. Display the modules inside Apache'''==


Now try logging into the machine, with “ssh ‘remote-host’”, and check in:


'''Display the modules compiled inside Apache'''


.ssh/authorized_keys to make sure we haven’t added extra keys that you weren’t expecting.


<nowiki># </nowiki>'''httpd -l'''


'''Note: '''ssh-copy-id appends the keys to the remote-host’s


Compiled in modules: core.c


.ssh/authorized_key.


prefork.c http_core.c mod_so.c


'''Step 3: Login to remote-host without entering the password'''


'''Display both static and dynamic module loaded by Apache'''


jsmith@local-host$ '''ssh remote-host'''


When you pass option -l, to httpd, it will display only the static modules. Passing option -M, will display both static and shared modules as shown below.


Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2 <nowiki>[</nowiki>'''Note''': SSH did not ask for password.] jsmith@remote-host$ <nowiki>[</nowiki>'''Note''': You are on remote-host here]90'''Linux 101 Hacks'''www.thegeekstuff.com


<nowiki># </nowiki>'''httpd –M'''


'''Hack 73. Use ssh-copy-id along with ssh-agent'''


Loaded Modules:


'''Using ssh-copy-id along with the ssh-add/ssh-agent'''


core_module (static)


When no value is passed for the option -i and If ~/.ssh/identity.pub is not available, ssh-copy-id will display the following error message.


mpm_prefork_module (static)


jsmith@local-host$ ssh-copy-id -i remote-host


http_module (static)


/usr/bin/ssh-copy-id: ERROR: No identities found


so_module (static)


If you have loaded keys to the ssh-agent using the ssh-add, then ssh-copy-id will get the keys from the ssh-agent to copy to the remote-host. i.e, it copies the keys provided by ssh-add -L command to the remote-host, when you don’t pass option -i to the ssh-copy-id.


auth_basic_module (shared)


jsmith@local-host$ ssh-agent $SHELL


auth_digest_module (shared)


jsmith@local-host$ ssh-add -L The agent has no identities.


authn_file_module (shared)


jsmith@local-host$ ssh-add


authn_alias_module (shared)


Identity added: /home/jsmith/.ssh/id_rsa (/home/jsmith/.ssh/id_rsa)


Syntax OK


jsmith@local-host$ ssh-add -L ssh-rsa


=='''Hack 80. Show all accepted directives inside httpd.conf'''==


AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79D aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow== /home/jsmith/.ssh/id_rsa


This is like an extended help for httpd, which will display all the httpd.conf directives and the places where they are valid. For a specific directive, it tells all the possible values and where it can be used inside the httpd.conf. This can be very helpful, when you want to quickly know about a particular Apache directive.


jsmith@local-host$ ssh-copy-id -i remote-host jsmith@remote-host’s password:


<nowiki># </nowiki>'''httpd –L'''


Now try logging into the machine, with “ssh ‘remote-host’”, and check in: .ssh/authorized_keys to make sure we haven’t added extra keys that you weren’t expecting. <nowiki>[</nowiki>'''Note''': This has added the key displayed by ssh-add -L]91'''Linux 101 Hacks'''www.thegeekstuff.com


HostnameLookups (core.c)


'''Three Minor Annoyances of ssh-copy-id'''


“on” to enable, “off” to disable reverse DNS lookups, or “double” to enable double-reverse DNS lookups Allowed in *.conf anywhere


Following are few minor annoyances of the ssh-copy-id.


ServerLimit (prefork.c)


# Default public key: ssh-copy-id uses ~/.ssh/identity.pub as the default public key file (i.e when no value is passed to option -i). Instead, I wish it uses id_dsa.pub, or id_rsa.pub, or identity.pub as default keys. i.e If any one of them exist, it should copy that to the remote-host. If two or three of them exist, it should copy identity.pub as default.


# The agent has no identities: When the ssh-agent is running and the ssh-add -L returns “The agent has no identities” (i.e no keys are added to the ssh-agent), the ssh-copy-id will still copy the message “The agent has no identities” to the remote-host’s authorized_keys entry.
<nowiki>Maximum value of MaxClients for this run of Apache Allowed in *.conf only outside <Directory>, <Files> or <Location></nowiki>


# Duplicate entry in authorized_keys: I wish ssh-copy-id validates duplicate entry on the remote-host’s authorized_keys. If you execute ssh-copy-id multiple times on the local-host, it will keep appending the same key on the remote-host’s authorized_keys file without checking for duplicates. Even with duplicate entries everything works as expected. But, I would like to have my authorized_keys file clutter free.


'''Hack 74. Crontab'''
KeepAlive (http_core.c)




Using cron you can execute a shell-script or Linux commands at a specific time and date. For example a sysadmin can schedule a backup job that can run every day.
<nowiki>Whether persistent connections should be On or Off Allowed in *.conf only outside <Directory>, <Files> or <Location></nowiki>




'''How to add a job to the cron?'''
LoadModule (mod_so.c)




<nowiki># </nowiki>crontab –e
a module name and the name of a shared object file to load it from


<nowiki>Allowed in *.conf only outside <Directory>, <Files> or <Location></nowiki>


0 5 <nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/backup.sh92'''Linux 101 Hacks'''www.thegeekstuff.com


=='''Hack 81. Validate the httpd.conf after making changes'''==


This will execute /root/bin/backup.sh at 5 a.m every day.


Use option -t to validate whether there are any issues with a specific Apache configuration file. In the example shown below, it displays that there is a


'''Description of Cron fields.'''


problem at line 148 in the httpd.conf.debug. mod_auth_basicso is missing a . (period) before the so.


Following is the format of the crontab file.


<nowiki># </nowiki>'''httpd -t -f conf/httpd.conf.debug'''


{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}


httpd: Syntax error on line 148 of /etc/httpd/conf/httpd.conf.debug:


o minute: Allowed range 0 – 59 o hour: Allowed range 0 – 23


Cannot load /etc/httpd/modules/mod_auth_basicso into server:


o day-of-month: Allowed range 0 – 31


/etc/httpd/modules/mod_auth_basicso: cannot open shared object file: No such file or directory


o month: Allowed range 1 – 12. 1 = January. 12 = December. o Day-of-week: Allowed range 0 – 7. Sunday is either 0 or 7.


Once you fix the issue, it will display Syntax OK.


'''Crontab examples'''


<nowiki># </nowiki>'''httpd -t -f conf/httpd.conf.debug'''


# Run at 12:01 a.m. 1 minute after midnight everyday. This is a good time to run backup when the system is not under load.


#
Syntax OK
## 0 <nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/backup.sh


# Run backup every weekday (Mon – Fri) at 11:59 p.m.


#
=='''Hack 82. Display the httpd build parameters'''==
## 11 <nowiki>* </nowiki><nowiki>* </nowiki>1,2,3,4,5 /root/bin/backup.sh


Following will also do the same.


Use option -V (upper-case V), to display Apache version number and all the parameters that are used while building the Apache.


59 11 <nowiki>* </nowiki><nowiki>* </nowiki>1-5 /root/bin/backup.sh93'''Linux 101 Hacks'''www.thegeekstuff.com


<nowiki># </nowiki>'''httpd –V'''


# Execute the command every 5 minutes.


<nowiki>*/5 </nowiki><nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/check-status.sh
Server version: Apache/2.2.9 (Unix) Server built: Jul 14 2008 15:36:56 Server’s Module Magic Number: 20051115:15


Server loaded: APR 1.2.12, APR-Util 1.2.12 Compiled using: APR 1.2.12, APR-Util 1.2.12 Architecture: 32-bit


# Execute at 1:10 p.m on 1st of every month
Server MPM: Prefork threaded: no


#
forked: yes (variable process count) Server compiled with….
## 13 1 <nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/full-backup.sh


# Execute 11 p.m on weekdays.
-D APACHE_MPM_DIR=”server/mpm/prefork” -D APR_HAS_SENDFILE


#
## 23 <nowiki>* </nowiki><nowiki>* </nowiki>1-5 /root/bin/incremental-backup.sh


'''Crontab Options '''
-D APR_HAS_MMAP




Following are the available options with crontab:
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE




# crontab –e : Edit the crontab file. This will create a crontab, if it doesn’t exist
-D APR_USE_PTHREAD_SERIALIZE


o crontab –l : Display the crontab file. o crontab -r : Remove the crontab file.


-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD


o crontab -ir : This will prompt user before deleting a crontab.


-D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=128 -D HTTPD_ROOT=”/etc/httpd”


'''Hack 75. Safe Reboot Of Linux Using Magic SysRq Key'''


-D SUEXEC_BIN=”/usr/sbin/suexec”


The magic SysRq key is a key combination in the Linux kernel which allows the user to perform various low level commands regardless of the system’s state.


-D DEFAULT_PIDLOG=”logs/httpd.pid”


It is often used to recover from freezes, or to reboot a computer without corrupting the filesystem. The key combination consists of94'''Linux 101 Hacks'''www.thegeekstuff.com


-D DEFAULT_SCOREBOARD=”logs/apache_runtime_status” -D DEFAULT_LOCKFILE=”logs/accept.lock”


Alt+SysRq+commandkey. In many systems the SysRq key is the printscreen key.


-D DEFAULT_ERRORLOG=”logs/error_log”


First, you need to enable the SysRq key, as shown below.


-D AP_TYPES_CONFIG_FILE=”conf/mime.types” -D SERVER_CONFIG_FILE=”conf/httpd.conf”


echo "1" > /proc/sys/kernel/sysrq


If you want display only the Apache version number, use the option -v (lower-case v) as shown below.


'''List of SysRq Command Keys'''


<nowiki># </nowiki>'''httpd -v'''


Following are the command keys available for Alt+SysRq+commandkey. o ‘k’ – Kills all the process running on the current virtual console. o ’s’ – This will attempt to sync all the mounted file system.


Server version: Apache/2.2.9 (Unix)


# ‘b’ - Immediately reboot the system, without unmounting partitions or syncing.


o ‘e’ – Sends SIGTERM to all process except init.
Server built: Jul 14 2008 15:36:56




o ‘m’ – Output current memory information to the console. o ‘i’ - Send the SIGKILL signal to all processes except init
=='''Hack 83. Load a specific module only on demand'''==




# ‘r’ - Switch the keyboard from raw mode (the mode used by programs such as X11), to XLATE mode.  
Sometimes you may not want to load all the modules in the Apache. For e.g. You may want to load ldap related modules to Apache, only when you are testing LDAP. This can be achieved as shown below.


o ’s’ – sync all mounted file system.


Modify the httpd.conf and add IfDefine directive called load-ldap (you can name this anything you want).


# ‘t’ - Output a list of current tasks and their information to the console.


o ‘u’ - Remount all mounted filesystems in readonly mode. o ‘o’ – Shutdown the system immediately.
<nowiki><IfDefine </nowiki>load-ldap>




o ‘p’ – Print the current registers and flags to the console.
LoadModule ldap_module modules/mod_ldap.so LoadModule authnz_ldap_module




# ‘0-9′ - Sets the console log level, controlling which kernel messages will be printed to your console.  
<nowiki>modules/mod_authnz_ldap.so </IfDefine></nowiki>


95'''Linux 101 Hacks'''www.thegeekstuff.com


When you are testing ldap and would like to Load the ldap related modules, pass the load-ldap to Option -D, as shown below:


o ‘f’ - Will call oom_kill to kill process which takes more memory.


<nowiki># httpd -k start -e debug -Dload-ldap -f /etc/httpd/conf/httpd.conf.debug</nowiki>


# ‘h’ – Used to display the help. But any other keys than the above listed will print help.


We can also do this by echoing the keys to the /proc/sysrq-trigger file. For example, to re-boot a system you can perform the following.
<nowiki>[Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded module ldap_module</nowiki>




echo "b" > /proc/sysrq-trigger
<nowiki>[Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded module authnz_ldap_module</nowiki>


<nowiki>[Note: Pass -Dload-ldap, to load the ldap modules into Apache]</nowiki>


'''Perform a Safe reboot of Linux using Magic SysRq Key'''


<nowiki># </nowiki>apachectl start


To perform a safe reboot of a Linux computer which hangs up, do the following. This will avoid the fsck during the next re-booting. i.e Press Alt+SysRq+letter highlighted below.


<nowiki>[</nowiki>'''Note''': Start the Apache normally, if you don't want to load the ldap modules.104'''Linux 101 Hacks'''www.thegeekstuff.com


o un'''R'''aw (take control of keyboard back from X11,


='''Chapter 11: Bash Scripting'''=


# t'''E'''rminate (send SIGTERM to all processes, allowing them to terminate gracefully),


# '''k'''Ill (send SIGILL to all processes, forcing them to terminate''' '''immediately),
=='''Hack 84. Execution Sequence of .bash_* files'''==


o '''S'''ync (flush data to disk),


What is the sequence in which the following files are executed?


o '''U'''nmount (remount all filesystems read-only), o re'''B'''oot.96'''Linux 101 Hacks'''www.thegeekstuff.com


o /etc/profile


'''Chapter 10: Apachectl and Httpd'''


o ~/.bash_profile


'''Examples'''


o ~/.bashrc


After you have installed Apache2, if you want to use apachectl and httpd to it’s maximum potential, you should go beyond using start, stop and restart. The 9 practical examples provided in this chapter will help you to use apachectl and httpd very effectively.


o ~/.bash_login


Apachectl acts as SysV init script, taking arguments like start, stop, restart and status. It also acts as front-end to httpd command, by simply passing the command line arguments to httpd. So, all the commands you execute using apachectl, can also be executed directly by calling httpd.


o ~/.profile


If you don’t have Apache, refer to the tutorials: [http://www.thegeekstuff.com/2008/07/install-apache-2-from-source-on-linux/ ][http://www.thegeekstuff.com/2008/07/install-apache-2-from-source-on-linux/ install apache from source][http://www.thegeekstuff.com/2008/07/install-apache-2-from-source-on-linux/  ]or [http://www.thegeekstuff.com/2008/09/how-to-install-or-upgrade-lamp-apache-mysql-and-php-stack-on-linux-using-yum/ ][http://www.thegeekstuff.com/2008/09/how-to-install-or-upgrade-lamp-apache-mysql-and-php-stack-on-linux-using-yum/ install LAMP stack using yum].


o ~/.bash_logout


'''Hack 76. Pass different httpd.conf filename to apachectl'''


'''Execution sequence for interactive login shell'''


Typically you’ll modify the original httpd.conf to try out different Apache directives. If something doesn’t work out, you’ll revert back the changes. Instead of playing around with the original httpd.conf, copy it to a new httpd.conf.debug and use this new httpd.conf.debug file with Apache for testing purpose as shown below using option -f.


Following pseudo code explains the sequence of execution of these files.


# '''apachectl -f conf/httpd.conf.debug '''


# '''httpd -k start -f conf/httpd.conf.debug '''
execute /etc/profile


<nowiki>[</nowiki>'''Note''': you can use either apachectl or httpd as shown above]


IF ~/.bash_profile exists THEN execute ~/.bash_profile


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>ps
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">-ef | grep http</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
ELSE
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25080</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 23:26 00:00:00 /usr/sbin/httpd -f


|}
97'''Linux 101 Hacks'''www.thegeekstuff.com


IF ~/.bash_login exist THEN execute ~/.bash_login


conf/httpd.conf.debug


ELSE


apache 25099 25080 0 23:28 00:00:00 /usr/sbin/httpd -f conf/httpd.conf.debug


IF ~/.profile exist THEN execute ~/.profile


<nowiki>[</nowiki>'''Note''': ps shows the httpd running with httpd.conf.debug file]


END IF END IF


Once you are satisfied with the changes and Apache runs without any problem with httpd.conf.debug, you can copy the changes to httpd.conf and start the Apache normally as shown below.
END IF




# cp httpd.conf.debug httpd.conf
When you logout of the interactive shell, following is the sequence of execution:


# apachectl stop


# apachectl start
IF ~/.bash_logout exists THEN execute ~/.bash_logout


# ps -ef | grep httpd
END IF




{| style="border-spacing:0;"
Please note that /etc/bashrc is executed by ~/.bashrc as shown below:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25114</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 23:28 00:00:00 /usr/sbin/httpd


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -k start
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
<nowiki># </nowiki>cat ~/.bashrc
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| daemon
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25115 25114</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 23:28 00:00:00 /usr/sbin/httpd


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -k start
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
if <nowiki>[ </nowiki>-f /etc/bashrc ]; then
<nowiki>[</nowiki>'''Note''': ps indicates that the httpd is running using the default config file]




'''Hack 77. Use a temporary DocumentRoot without modifying httpd.conf'''
. /etc/bashrc Fi




This is very helpful, when you are trying out different layout for your website and don’t want to modify the original files under the default DocumentRoot.
'''Execution sequence for interactive non-login shell'''




Take a copy of your original DocumentRoot directory (/var/www/html) to a new temporary DocumentRoot directory (/var/www/html_debug). Make all your changes under this temporary DocumentRoot directory (/var/www/html_debug) and start the Apache with this temporary directory as shown below using option -c.98'''Linux 101 Hacks'''www.thegeekstuff.com
While launching a non-login interactive shell, following is the sequence of execution:




<nowiki># </nowiki>'''httpd -k start -c “DocumentRoot /var/www/html_debug/”'''
IF ~/.bashrc exists THEN execute ~/.bashrc




If you want to go back to original configuration using the default DocumentRoot (/var/www/html), simply restart the Apache as shown below.
END IF




# httpd -k stop
'''Note: '''When a non-interactive shell starts up, it looks for ENV environment''' '''variable, and executes the file-name value mentioned in the ENV variable.


# apachectl start


'''Hack 78. Increase the Log Level temporarily'''
=='''Hack 85. How to generate random number in bash shell'''==




While you are debugging an issue, you can change the LogLevel of the Apache temporarily, without modifying the LogLevel directive in the httpd.conf as shown below using option -e. In this example, the LogLevel is set to debug.
Use the $RANDOM bash built-in function to generate random number between 0 – 32767 as shown below.




<nowiki># </nowiki>'''httpd -k start -e debug'''
$ echo $RANDOM


22543


<nowiki>[Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_basic_module</nowiki>


$ echo $RANDOM 25387


<nowiki>[Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_digest_module</nowiki>


$ echo $RANDOM 647


Possible values you can pass to option -e are:


=='''Hack 86. Debug a shell script'''==


o debug


To debug a shell script use set –xv inside the shell script at the top.


o info


'''Shell script with no debug command:'''


o notice


$ cat filesize.sh #!/bin/bash


o warn
for filesize in $(ls -l . | grep "^-" | awk '{print $5}')




o error
do




o crit
let totalsize=$totalsize+$filesize done




o alert
echo "Total file size in current directory: $totalsize"




o emerg99'''Linux 101 Hacks'''www.thegeekstuff.com
'''Output of Shell script with no debug command:'''




'''Hack 79. Display the modules inside Apache'''
$ ./filesize.sh




'''Display the modules compiled inside Apache'''
Total file size in current directory: 652




<nowiki># </nowiki>'''httpd -l'''
'''Shell script with Debug command inside:'''




Compiled in modules: core.c
Add set –xv inside the shell script now to debug the output as shown below.




prefork.c http_core.c mod_so.c
$ cat filesize.sh #!/bin/bash


set -xv


'''Display both static and dynamic module loaded by Apache'''


for filesize in $(ls -l . | grep "^-" | awk '{print


When you pass option -l, to httpd, it will display only the static modules. Passing option -M, will display both static and shared modules as shown below.


$5}') do


<nowiki># </nowiki>'''httpd –M'''
let totalsize=$totalsize+$filesize done


echo "Total file size in current directory: $totalsize"


Loaded Modules:


'''Output of Shell script with Debug command inside:'''


core_module (static)


$ ./fs.sh


mpm_prefork_module (static)


# ls -l .


http_module (static)
# grep '^-'


# awk '{print $5}'


so_module (static)
# for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')'
# let totalsize=+178


# for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')'
# let totalsize=178+285


auth_basic_module (shared)
# for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')'
# let totalsize=463+189


# echo 'Total file size in current directory: 652' Total file size in current directory: 652


auth_digest_module (shared)
'''Execute Shell script with debug option:'''




authn_file_module (shared)
Instead of giving the set –xv inside the shell script, you can also provide that while executing the shell script as shown below.




authn_alias_module (shared)
$ bash -xv filesize.sh




Syntax OK100'''Linux 101 Hacks'''www.thegeekstuff.com
=='''Hack 87. Quoting'''==




'''Hack 80. Show all accepted directives inside httpd.conf'''
echo statement without any special character.




This is like an extended help for httpd, which will display all the httpd.conf directives and the places where they are valid. For a specific directive, it tells all the possible values and where it can be used inside the httpd.conf. This can be very helpful, when you want to quickly know about a particular Apache directive.
$ echo The Geek Stuff




<nowiki># </nowiki>'''httpd –L'''
The Geek Stuff




HostnameLookups (core.c)
Echo statement with a special character ; . semi-colon is a command terminator in bash. In the following example, “The Geek” works for the echo and “Stuff” is treated as a separate Linux command and gives command not found.




“on” to enable, “off” to disable reverse DNS lookups, or “double” to enable double-reverse DNS lookups Allowed in *.conf anywhere
$ echo The Geek; Stuff The Geek




ServerLimit (prefork.c)
-bash: Stuff: command not found




<nowiki>Maximum value of MaxClients for this run of Apache Allowed in *.conf only outside <Directory>, <Files> or <Location></nowiki>
To avoid this you can add a \ in front of semi-colon, which will remove the special meaning of semi-colon and just print it as shown below.




KeepAlive (http_core.c)
$ echo The Geek\; Stuff The Geek; Stuff




<nowiki>Whether persistent connections should be On or Off Allowed in *.conf only outside <Directory>, <Files> or <Location></nowiki>
'''Single Quote'''




LoadModule (mod_so.c)
Use single quote when you want to literally print everything inside the single quote. Even the special variables such as $HOSTNAME will be print as $HOSTNAME instead of printing the name of the Linux host.




a module name and the name of a shared object file to load it from
$ echo 'Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD'


<nowiki>Allowed in *.conf only outside <Directory>, <Files> or <Location></nowiki>


Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD


'''Hack 81. Validate the httpd.conf after making changes'''


'''Double Quote'''


Use option -t to validate whether there are any issues with a specific Apache configuration file. In the example shown below, it displays that there is a101'''Linux 101 Hacks'''www.thegeekstuff.com


Use double quotes when you want to display the real meaning of special variables.


problem at line 148 in the httpd.conf.debug. mod_auth_basicso is missing a . (period) before the so.


$ echo "Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD"


<nowiki># </nowiki>'''httpd -t -f conf/httpd.conf.debug'''


Hostname=dev-db <nowiki>; </nowiki>Current User=ramesh <nowiki>; </nowiki>Message=$ is


httpd: Syntax error on line 148 of /etc/httpd/conf/httpd.conf.debug:


USD


Cannot load /etc/httpd/modules/mod_auth_basicso into server:


Double quotes will remove the special meaning of all characters except the following:


/etc/httpd/modules/mod_auth_basicso: cannot open shared object file: No such file or directory


o $ Parameter Substitution.


Once you fix the issue, it will display Syntax OK.


o ` Backquotes


<nowiki># </nowiki>'''httpd -t -f conf/httpd.conf.debug'''


o \$ Literal Dollar Sign. o \´ Literal Backquote.


Syntax OK


o \" Embedded Doublequote. o \\ Embedded Backslashes.


'''Hack 82. Display the httpd build parameters'''


=='''Hack 88. Read data file fields inside a shell script'''==


Use option -V (upper-case V), to display Apache version number and all the parameters that are used while building the Apache.


This example shows how to read a particular field from a data-file and manipulate it inside a shell-script. For example, let us assume the employees.txt file is in the format of {employee-name}:{employee-id}:{department-name}, with colon delimited file as shown below.


<nowiki># </nowiki>'''httpd –V'''


$ cat employees.txt


Server version: Apache/2.2.9 (Unix) Server built: Jul 14 2008 15:36:56 Server’s Module Magic Number: 20051115:15


Server loaded: APR 1.2.12, APR-Util 1.2.12 Compiled using: APR 1.2.12, APR-Util 1.2.12 Architecture: 32-bit
Emma Thomas:100:Marketing Alex Jason:200:Sales


Server MPM: Prefork threaded: no


forked: yes (variable process count) Server compiled with….
Madison Randy:300:Product Development Sanjay Gupta:400:Support


-D APACHE_MPM_DIR=”server/mpm/prefork” -D APR_HAS_SENDFILE
Nisha Singh:500:Sales




-D APR_HAS_MMAP102'''Linux 101 Hacks'''www.thegeekstuff.com
The following shell script explains how to read specific fields from this employee.txt file.




-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE
$ vi read-employees.sh #!/bin/bash




-D APR_USE_PTHREAD_SERIALIZE
IFS=:




-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD
echo "Employee Names:" echo "---------------" while read name empid dept




-D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=128 -D HTTPD_ROOT=”/etc/httpd”
do




-D SUEXEC_BIN=”/usr/sbin/suexec”
<nowiki>echo "$name is part of $dept department" done < ~/employees.txt</nowiki>




-D DEFAULT_PIDLOG=”logs/httpd.pid”
Assign execute privilege to the shell script and execute it.




-D DEFAULT_SCOREBOARD=”logs/apache_runtime_status” -D DEFAULT_LOCKFILE=”logs/accept.lock”
$ chmod u+x read-employees.sh




-D DEFAULT_ERRORLOG=”logs/error_log”
$ ./read-employees.sh Employee Names:


<nowiki>---------------</nowiki>


-D AP_TYPES_CONFIG_FILE=”conf/mime.types” -D SERVER_CONFIG_FILE=”conf/httpd.conf”


Emma Thomas is part of Marketing department Alex Jason is part of Sales department


If you want display only the Apache version number, use the option -v (lower-case v) as shown below.


Madison Randy is part of Product Development department Sanjay Gupta is part of Support department


<nowiki># </nowiki>'''httpd -v'''
Nisha Singh is part of Sales department


='''Chapter 12: System Monitoring and'''=


Server version: Apache/2.2.9 (Unix)


'''Performance'''


Server built: Jul 14 2008 15:36:56


=='''Hack 89. Free command'''==


'''Hack 83. Load a specific module only on demand'''


free command displays all the necessary information about system physical (RAM) and swap memory.


Sometimes you may not want to load all the modules in the Apache. For e.g. You may want to load ldap related modules to Apache, only when you are testing LDAP. This can be achieved as shown below.


Syntax: free <nowiki>[options]</nowiki>


Modify the httpd.conf and add IfDefine directive called load-ldap (you can name this anything you want).


'''What is the total RAM on my system?'''


<nowiki><IfDefine </nowiki>load-ldap>


In the example below, the total physical memory on this system is 1GB. The values displayed below are in KB.


LoadModule ldap_module modules/mod_ldap.so LoadModule authnz_ldap_module103'''Linux 101 Hacks'''www.thegeekstuff.com




<nowiki>modules/mod_authnz_ldap.so </IfDefine></nowiki>
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>free
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| used
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| free
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| shared
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| buffers
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| cached
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


When you are testing ldap and would like to Load the ldap related modules, pass the load-ldap to Option -D, as shown below:
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mem: 1034624
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1006696
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 27928
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 174136
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 615892
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -/+ buffers/cache:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 216668
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">817956</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


<nowiki># httpd -k start -e debug -Dload-ldap -f /etc/httpd/conf/httpd.conf.debug</nowiki>
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Swap:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2031608</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2031608</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
'''What is the total memory on my system including RAM and Swap?'''


<nowiki>[Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded module ldap_module</nowiki>


In the following command:


<nowiki>[Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded module authnz_ldap_module</nowiki>


<nowiki>[Note: Pass -Dload-ldap, to load the ldap modules into Apache]</nowiki>
o option m displays the values in MB




<nowiki># </nowiki>apachectl start
# option t displays the “Total” line, which is sum of physical and swap memory values


# option o is to hide the buffers/cache line from the above example.


<nowiki>[</nowiki>'''Note''': Start the Apache normally, if you don't want to load the ldap modules.104'''Linux 101 Hacks'''www.thegeekstuff.com
#
## '''free –mto '''


112
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''Linux 101 Hacks'''</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">www.thegeekstuff.com</div>


'''Chapter 11: Bash Scripting'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">total</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">used</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">free</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">shared</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">buffers</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">cached</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mem:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1010</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">983</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">27</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">170</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">601</div>


'''Hack 84. Execution Sequence of .bash_* files'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Swap:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1983</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1983</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Total:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2994</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">983</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2011</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


What is the sequence in which the following files are executed?
|}


=='''Hack 90. Top Command'''==


o /etc/profile


top command displays real time information about various performance metrics of the system such as CPU Load, Memory Usage, Processes list etc.


o ~/.bash_profile


Syntax: top <nowiki>[options]</nowiki>


o ~/.bashrc


'''How to view my current system status including CPU usage?'''


o ~/.bash_login


Execute top without any option from the command line, which will display the output shown below. The top command output will keep displaying the real-time values, until you press “Control + c” or q to exit from the command output.


o ~/.profile


<nowiki># </nowiki>'''top'''


o ~/.bash_logout


top - 13:10:13 up 171 days, 20:21, 3 users, load average: 0.01, 0.05, 0.00


'''Execution sequence for interactive login shell'''




Following pseudo code explains the sequence of execution of these files.
{| style="border-spacing:0;"
| colspan="8"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Tasks: 194 total, 1 running, 193 sleeping,
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 stopped,
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 zombie</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Cpu(s):
| colspan="9"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.6% us, 0.7% sy, 0.0% ni, 98.7% id, 0.0% wa,
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0% hi, 0.0% si


execute /etc/profile
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mem:
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1034624k total,
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1007420k used,
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 27204k free, 174540k buffers


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Swap:
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2031608k total,
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0k used, 2031608k free,
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 615904k cached


IF ~/.bash_profile exists THEN execute ~/.bash_profile
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PID USER
| colspan="7"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PR NI VIRT RES SHR S %CPU %MEM
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">TIME+ COMMAND</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 11912 apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">15</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 31828</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 13m
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3916 S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.2</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:46.35</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| httpd


ELSE
|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 19299
 
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| oracle
IF ~/.bash_login exist THEN execute ~/.bash_login
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">19</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">279m</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 18m
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 17m S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.2</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:00.03</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| oracle


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 11398 jsmith
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">107m</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 28m
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 6404 S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.4</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:03.07</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| perl


ELSE
|}




IF ~/.profile exist THEN execute ~/.profile


'''How to read the output of the top command shown above?'''


END IF END IF


END IF105'''Linux 101 Hacks'''www.thegeekstuff.com
# Line 1 “top”, indicates that the system has been up and running for 171 days.  


# Line 2 “Tasks”, displays the total number of processes along with a breakdown of running, sleeping, stopped and zombie processes count.


When you logout of the interactive shell, following is the sequence of execution:
# Line 3 “Cpu(s)” displays the current CPU utilization of the system. In this example, CPU is 98.7% idle


# Line 4 “Mem” and line 5 “Swap” provides the memory information. This is the same information from the free command.


IF ~/.bash_logout exists THEN execute ~/.bash_logout
# The rest of the lines display all the active processes on the system, sorted default by CPU usage (%CPU column). i.e the most CPU intensive processes will be displayed on the top by default.  


END IF
There are several command line options and interactive options available for top commands. Let us review couple of essential options for top command.




Please note that /etc/bashrc is executed by ~/.bashrc as shown below:
'''How to identify the most memory intensive processes?'''




<nowiki># </nowiki>cat ~/.bashrc
While the output of the top command displayed, press F, which will display the following message and show all fields available for sorting, press n (which is for sorting the processes by Memory) and press enter. This will display the processes in the top output sorted by memory usage.




if <nowiki>[ </nowiki>-f /etc/bashrc ]; then
Current Sort Field: K for window 1:Def




. /etc/bashrc Fi
Select sort field via field letter, type any other key to return




'''Execution sequence for interactive non-login shell'''
'''How to add additional fields (for e.g. CPU Time) to the top output?'''




While launching a non-login interactive shell, following is the sequence of execution:
While the top command is running, press f, which will display the following message and show all fields available for display, press l, which will add the CPU Time to the display columns in the top output.




IF ~/.bashrc exists THEN execute ~/.bashrc
Current Fields: AEHIOQTWKNMbcdfgjplrsuvyzX for window 1:Def




END IF
Toggle fields via field letter, type any other key to return




'''Note: '''When a non-interactive shell starts up, it looks for ENV environment''' '''variable, and executes the file-name value mentioned in the ENV variable.
'''How to get the full path name and parameters of the running processes?'''




'''Hack 85. How to generate random number in bash shell'''
While the top command is running, press c, which will display full pathname of running processes as shown below in the command column. i.e Instead of httpd, it displays /usr/local/apache2/bin/httpd.




Use the $RANDOM bash built-in function to generate random number between 0 – 32767 as shown below.


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PID USER
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PR NI VIRT RES SHR S %CPU %MEM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">TIME+ COMMAND</div>


$ echo $RANDOM106'''Linux 101 Hacks'''www.thegeekstuff.com
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 11912 apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">15</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 31828</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 13m
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3916 S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.2 0:46.35</div>


|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /usr/local/apache2/bin/httpd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


22543
|}
 
'''How to view the individual CPUs in the top command?'''


$ echo $RANDOM 25387


While the top command is running, press 1 (number one), which will display the performance data of the individual CPUs on that machine as shown below.


$ echo $RANDOM 647


top - 13:10:13 up 171 days, 20:21, 3 users, load average: 0.01, 0.05, 0.00 Tasks: 194 total, 1 running, 193 sleeping, 0 stopped, 0 zombie


'''Hack 86. Debug a shell script'''


Cpu0 : 10.2% us, 2.6% sy, 0.0% ni, 86.8% id, 0.3% wa, 0.0% hi, 0.0% si Cpu1 : 9.6% us, 8.0% sy, 0.0% ni, 82.4% id, 0.0% wa, 0.0% hi, 0.0% si Cpu2 : 1.3% us, 1.3% sy, 0.0% ni, 95.0% id, 2.3% wa, 0.0% hi, 0.0% si Cpu3 : 0.0% us, 0.0% sy, 0.0% ni, 100.0% id, 0.0% wa, 0.0% hi, 0.0% si115
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


To debug a shell script use set –xv inside the shell script at the top.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mem:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1034624k total,
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1007420k used,
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 27204k free,
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 174540k buffers


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Swap:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2031608k total,
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0k used,
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2031608k free,
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 615904k cached


'''Shell script with no debug command:'''
|}


=='''Hack 91. Ps Command'''==


$ cat filesize.sh #!/bin/bash


for filesize in $(ls -l . | grep "^-" | awk '{print $5}')
ps command (process status) will display snapshot information of all active processes.




do
Syntax: ps <nowiki>[options]</nowiki>




let totalsize=$totalsize+$filesize done
'''How to display all the processes running in the system?'''




echo "Total file size in current directory: $totalsize"
Use "ps aux", as shown below.




'''Output of Shell script with no debug command:'''


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''ps aux | more'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


$ ./filesize.sh
|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| USER
Total file size in current directory: 652
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID %CPU %MEM</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">VSZ</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| RSS TTY STAT START TIME COMMAND


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2044</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">588 ?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Ss
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jun27


'''Shell script with Debug command inside:'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>init [5]</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31186</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.6</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">23736 17556 ?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26


Add set –xv inside the shell script now to debug the output as shown below.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:40</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31187</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.3</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">20640 14444 ?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26


$ cat filesize.sh #!/bin/bash
|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:37</div>
set -xv
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
You can also use "ps -ef | more", to get a similar output


for filesize in $(ls -l . | grep "^-" | awk '{print107'''Linux 101 Hacks'''www.thegeekstuff.com


'''Print the Process Tree'''


$5}') do


let totalsize=$totalsize+$filesize done
You can use either '''ps axuf''' or '''ps –ejH''' to display processes in a tree format. The tree structure will help to visualize the process and it’s parent process immediately. For clarity purpose, few columns have been cut-off in the output below.


echo "Total file size in current directory: $totalsize"


<nowiki># </nowiki>'''ps axuf'''116
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


'''Output of Shell script with Debug command inside:'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct14
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /opt/VRTSralus/bin/beremote


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /opt/VRTSralus/bin/beremote


$ ./fs.sh
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /opt/VRTSralus/bin/beremote


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /opt/VRTSralus/bin/beremote


# ls -l .  
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /opt/VRTSralus/bin/beremote


# grep '^-'
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct 14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /opt/VRTSralus/bin/beremote


# awk '{print $5}'
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec03
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01 /usr/local/sbin/sshd


# for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')'
|-
# let totalsize=+178
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec22
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1:08 /usr/local/sbin/sshd


# for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')'
|-
# let totalsize=178+285
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 23:35
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /usr/local/sbin/sshd


# for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')'
|-
# let totalsize=463+189
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 511
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 23:35
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ -bash


# echo 'Total file size in current directory: 652' Total file size in current directory: 652
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 511
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ ps axuf


'''Execute Shell script with debug option:'''
|}
Note: You can also use pstree command to display process in tree structure.




Instead of giving the set –xv inside the shell script, you can also provide that while executing the shell script as shown below.
'''View Processes Owned by a Particular User'''




$ bash -xv filesize.sh
The following command displays all the process owned by Linux user-name: oracle.




'''Hack 87. Quoting'''


{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''ps U oracle'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


echo statement without any special character.
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PID TTY
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>STAT</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| TIME COMMAND


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 5014
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01 /oracle/bin/tnslsnr


$ echo The Geek Stuff108'''Linux 101 Hacks'''www.thegeekstuff.com
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 7124 ?
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 ora_q002_med


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8206
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 ora_cjq0_med


The Geek Stuff
|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8852
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01 ora_pmon_med


Echo statement with a special character ; . semi-colon is a command terminator in bash. In the following example, “The Geek” works for the echo and “Stuff” is treated as a separate Linux command and gives command not found.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8854
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 ora_psp0_med


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8911
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:02 oraclemed (LOCAL=NO)


$ echo The Geek; Stuff The Geek
|}
'''View Processes Owned by Current User'''




-bash: Stuff: command not found
Following command displays all the process owned by the current user.




To avoid this you can add a \ in front of semi-colon, which will remove the special meaning of semi-colon and just print it as shown below.


{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''ps U $USER'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


$ echo The Geek\; Stuff The Geek; Stuff
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PID TTY
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">STAT</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| TIME COMMAND


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 10329
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ?
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">S</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 sshd: ramesh@pts/1,pts/2


'''Single Quote'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 10330
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Ss</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 -bash


|}
117
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


Use single quote when you want to literally print everything inside the single quote. Even the special variables such as $HOSTNAME will be print as $HOSTNAME instead of printing the name of the Linux host.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10354</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Ss+
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 -bash


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10530</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| R+
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 ps U ramesh


$ echo 'Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD'
|}


=='''Hack 92. Df Command'''==


Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD


df command (disk free) displays the amount of total and free disk space available on the mounted filesystems.


'''Double Quote'''


Syntax: df <nowiki>[options] </nowiki><nowiki>[name]</nowiki>


Use double quotes when you want to display the real meaning of special variables.


'''How much GB of disk space is free on my system?'''


$ echo "Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD"109'''Linux 101 Hacks'''www.thegeekstuff.com


Use df -h as shown below. Option -h displays the values in human readable format (for example: K for Kb, M for Mb and G for Gb). In the sample output below, / filesystem has 17GB of disk space available and /home/user filesystem has 70GB available.


Hostname=dev-db <nowiki>; </nowiki>Current User=ramesh <nowiki>; </nowiki>Message=$ is




USD
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''df –h'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Filesystem
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Size</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Used Avail Use% Mounted on


Double quotes will remove the special meaning of all characters except the following:
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sda1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">64G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 44G
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 17G
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">73%</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sdb1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">137G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 67G
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 70G
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">49%</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /home/user


o $ Parameter Substitution.
|}
'''What type of filesystem do I have on my system?'''




o ` Backquotes
Option -T will display the information about the filesystem Type. In this example / and /home/user filesystems are ext2. Option -a will display all the filesystems, including the 0 size special filesystem used by the system.




o \$ Literal Dollar Sign. o \´ Literal Backquote.
<nowiki># </nowiki>'''df -Tha'''




o \" Embedded Doublequote. o \\ Embedded Backslashes.
Filesystem Type Size Used Avail Use% Mounted on118
 
{| style="border-spacing:0;"
 
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
'''Hack 88. Read data file fields inside a shell script'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sda1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ext2</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>64G</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">44G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">17G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">73%</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /


This example shows how to read a particular field from a data-file and manipulate it inside a shell-script. For example, let us assume the employees.txt file is in the format of {employee-name}:{employee-id}:{department-name}, with colon delimited file as shown below.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sdb1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ext2</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>137G</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">67G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">70G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">49%</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /home/user


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| none
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">proc</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">-</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /proc


$ cat employees.txt
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| none
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">sysfs</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">-</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /sys


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| none
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">devpts</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">-</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/pts


Emma Thomas:100:Marketing Alex Jason:200:Sales
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| none
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">tmpfs</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2.0G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2.0G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0%</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/shm


|}


Madison Randy:300:Product Development Sanjay Gupta:400:Support
=='''Hack 93. Kill Command'''==


Nisha Singh:500:Sales


kill command can be used to terminate a running process. Typically this command is used to kill processes that are hanging and not responding.


The following shell script explains how to read specific fields from this employee.txt file.


Syntax: kill <nowiki>[options] </nowiki><nowiki>[pids|commands]</nowiki>


$ vi read-employees.sh #!/bin/bash


'''How to kill a hanging process?'''


IFS=:


First, identify the process id of the particular process that you would like to kill using the ps command. Once you know the process id, pass it as a parameter to the kill command. The example below shows how to kill the hanging apache httpd process. Please note that typically you should use “apachectl stop” to stop apache.


echo "Employee Names:" echo "---------------" while read name empid dept110'''Linux 101 Hacks'''www.thegeekstuff.com




do
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''ps aux | grep httpd'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| USER
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID %CPU %MEM</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">VSZ</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| RSS TTY STAT START TIME COMMAND


<nowiki>echo "$name is part of $dept department" done < ~/employees.txt</nowiki>
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31186</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.6</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">23736 17556 ?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:40</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


Assign execute privilege to the shell script and execute it.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31187</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.3</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">20640 14444 ?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:37</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


$ chmod u+x read-employees.sh
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''kill 31186 31187'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
Please note that the above command tries to terminate the process graciously by sending a signal called SIGTERM. If the process does not get terminated, you can forcefully terminate the process by passing a signal called SIGKILL, using the option -9 as shown below. You should either be the owner of the process or a privileged user to kill a process.


$ ./read-employees.sh Employee Names:


<nowiki>---------------</nowiki>
<nowiki># </nowiki>'''kill -9 31186 31187'''




Emma Thomas is part of Marketing department Alex Jason is part of Sales department
Another way to kill multiple processes easily is by adding the following two functions to the .bash_profile.




Madison Randy is part of Product Development department Sanjay Gupta is part of Support department
function psgrep ()


Nisha Singh is part of Sales department111'''Linux 101 Hacks'''www.thegeekstuff.com


{


'''Chapter 12: System Monitoring and'''


ps aux | grep "$1" | grep -v 'grep'


'''Performance'''
 
}




'''Hack 89. Free command'''
function psterm ()




free command displays all the necessary information about system physical (RAM) and swap memory.
{




Syntax: free <nowiki>[options]</nowiki>
<nowiki>[ ${#} -eq 0 ] && echo "usage: $FUNCNAME STRING" && return 0 local pid</nowiki>




'''What is the total RAM on my system?'''
pid=$(ps ax | grep "$1" | grep -v grep | awk '{ print $1 }') echo -e "terminating '$1' / process(es):\n$pid"




In the example below, the total physical memory on this system is 1GB. The values displayed below are in KB.
kill -SIGTERM $pid






{| style="border-spacing:0;"
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>free
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| }
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| used
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| free
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| shared
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| buffers
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| cached
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 5,787: Line 6,151:


|-
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mem: 1034624
| colspan="8"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Now do the following, to identify and kill all httpd processes.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1006696
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 27928
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 174136
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 615892
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -/+ buffers/cache:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''psgrep http'''
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 216668
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">817956</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Swap:
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2031608</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2031608</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|}
|-
'''What is the total memory on my system including RAM and Swap?'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| USER
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID %CPU %MEM</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| VSZ
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">RSS TTY</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">STAT START</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| TIME


 
|-
In the following command:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| COMMAND
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
o option m displays the values in MB
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
# option t displays the “Total” line, which is sum of physical and swap memory values
 
# option o is to hide the buffers/cache line from the above example.  
 
#
## '''free –mto '''
 
112
{| style="border-spacing:0;"
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''Linux 101 Hacks'''</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">www.thegeekstuff.com</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31186</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">total</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.6</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">used</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">23736</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">free</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">17556</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">shared</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">buffers</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">cached</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mem:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1010</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:40</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">983</div>
| colspan="5" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">27</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">170</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">601</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Swap:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1983</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31187</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1983</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.3</div>
| colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">20640</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">14444</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Total:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2994</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:37</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">983</div>
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2011</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
|}
'''Hack 90. Top Command'''
<nowiki># </nowiki>'''psterm httpd'''




top command displays real time information about various performance metrics of the system such as CPU Load, Memory Usage, Processes list etc.
terminating 'httpd' / process(es): 31186




Syntax: top <nowiki>[options]</nowiki>
31187
 


=='''Hack 94. Du Command'''==


'''How to view my current system status including CPU usage?'''


du command (disk usage) will print the file space usage for a particular directory and its subdirectories.


Execute top without any option from the command line, which will display the output shown below. The top command output will keep displaying the real-time values, until you press “Control + c” or q to exit from the command output.


'''How much space is taken by my home directory and all its subdirectories?'''


<nowiki># </nowiki>'''top'''


In the following example, option -s stands for summary only. i.e it displays only the total size of /home/jsmith and not the individual sizes of all the subdirectories inside the /home/jsmith. Option -h displays the information in a human readable format. i.e K for KB, M for MB and G for GB. The ~ indicates the user home directory. This command is same as “du -sh /home/jsmith”


top - 13:10:13 up 171 days, 20:21, 3 users, load average: 0.01, 0.05, 0.00


<nowiki># </nowiki>'''du -sh ~'''




{| style="border-spacing:0;"
320M/home/jsmith
| colspan="8"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Tasks: 194 total, 1 running, 193 sleeping,
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 stopped,
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 zombie</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Cpu(s):
| colspan="9"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.6% us, 0.7% sy, 0.0% ni, 98.7% id, 0.0% wa,
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0% hi, 0.0% si


|-
To get the subdirectories under /home/jsmith listed, execute the above command without the s option.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mem:
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1034624k total,
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1007420k used,
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 27204k free, 174540k buffers


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Swap:
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2031608k total,
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0k used, 2031608k free,
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 615904k cached


|-
=='''Hack 95. lsof commands.'''==
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PID USER
| colspan="7"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PR NI VIRT RES SHR S %CPU %MEM
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">TIME+ COMMAND</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 11912 apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">15</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 31828</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 13m
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3916 S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.2</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:46.35</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| httpd


|-
Lsof stands for ls open files, which will list all the open files in the system. The open files include network connection, devices and directories. The output of the lsof command will have the following columns:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 19299
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| oracle
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">19</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">279m</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 18m
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 17m S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.2</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:00.03</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| oracle


|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 11398 jsmith
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">107m</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 28m
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 6404 S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.4</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:03.07</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| perl


|}
o COMMAND process name.
113'''Linux 101 Hacks'''www.thegeekstuff.com




'''How to read the output of the top command shown above?'''
o PID process ID




# Line 1 “top”, indicates that the system has been up and running for 171 days.
o USER Username


# Line 2 “Tasks”, displays the total number of processes along with a breakdown of running, sleeping, stopped and zombie processes count.
o FD file descriptor


# Line 3 “Cpu(s)” displays the current CPU utilization of the system. In this example, CPU is 98.7% idle


# Line 4 “Mem” and line 5 “Swap” provides the memory information. This is the same information from the free command.
o TYPE node type of the file


# The rest of the lines display all the active processes on the system, sorted default by CPU usage (%CPU column). i.e the most CPU intensive processes will be displayed on the top by default.


There are several command line options and interactive options available for top commands. Let us review couple of essential options for top command.
o DEVICE device number




'''How to identify the most memory intensive processes?'''
o SIZE file size




While the output of the top command displayed, press F, which will display the following message and show all fields available for sorting, press n (which is for sorting the processes by Memory) and press enter. This will display the processes in the top output sorted by memory usage.
o NODE node number




Current Sort Field: K for window 1:Def
o NAME full path of the file name.




Select sort field via field letter, type any other key to return
'''View all open files of the system'''




'''How to add additional fields (for e.g. CPU Time) to the top output?'''114'''Linux 101 Hacks'''www.thegeekstuff.com
Execute the lsof command without any parameter as shown below.
 
 
While the top command is running, press f, which will display the following message and show all fields available for display, press l, which will add the CPU Time to the display columns in the top output.
 
 
Current Fields: AEHIOQTWKNMbcdfgjplrsuvyzX for window 1:Def
 
 
Toggle fields via field letter, type any other key to return
 
 
'''How to get the full path name and parameters of the running processes?'''
 
 
While the top command is running, press c, which will display full pathname of running processes as shown below in the command column. i.e Instead of httpd, it displays /usr/local/apache2/bin/httpd.






{| style="border-spacing:0;"
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PID USER
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''lsof | more'''
| colspan="5" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PR NI VIRT RES SHR S %CPU %MEM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">TIME+ COMMAND</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 11912 apache
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| COMMAND
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">15</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PID
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 31828</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| USER
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 13m
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| FD
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3916 S
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| TYPE
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">DEVICE</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.2 0:46.35</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SIZE
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| NODE


|-
|-
| colspan="4" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /usr/local/apache2/bin/httpd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| NAME
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|}
'''How to view the individual CPUs in the top command?'''
While the top command is running, press 1 (number one), which will display the performance data of the individual CPUs on that machine as shown below.
top - 13:10:13 up 171 days, 20:21, 3 users, load average: 0.01, 0.05, 0.00 Tasks: 194 total, 1 running, 193 sleeping, 0 stopped, 0 zombie
Cpu0 : 10.2% us, 2.6% sy, 0.0% ni, 86.8% id, 0.3% wa, 0.0% hi, 0.0% si Cpu1 : 9.6% us, 8.0% sy, 0.0% ni, 82.4% id, 0.0% wa, 0.0% hi, 0.0% si Cpu2 : 1.3% us, 1.3% sy, 0.0% ni, 95.0% id, 2.3% wa, 0.0% hi, 0.0% si Cpu3 : 0.0% us, 0.0% sy, 0.0% ni, 100.0% id, 0.0% wa, 0.0% hi, 0.0% si115
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mem:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1034624k total,
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1007420k used,
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 27204k free,
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 174540k buffers
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Swap:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2031608k total,
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0k used,
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2031608k free,
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 615904k cached
|}
'''Hack 91. Ps Command'''
ps command (process status) will display snapshot information of all active processes.
Syntax: ps <nowiki>[options]</nowiki>
'''How to display all the processes running in the system?'''
Use "ps aux", as shown below.
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''ps aux | more'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 6,095: Line 6,324:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| USER
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID %CPU %MEM</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| cwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">VSZ</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| DIR
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| RSS TTY STAT START TIME COMMAND
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4096</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2 /
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| rtd
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| DIR
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2 /</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2044</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| txt
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">588 ?</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Ss
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jun27
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 32684
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 983101 /sbin/init
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| mem
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>106397</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 166798 /lib/ld-


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2.3.4.so
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>init [5]</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 6,124: Line 6,386:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31186</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| mem
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.6</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">23736 17556 ?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>1454802</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 166799


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:40</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /lib/tls/libc-2.3.4.so
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| mem
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31187</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>53736</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">163964</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.3</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">20640 14444 ?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:37</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /lib/libsepol.so.1
| colspan="4" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
|-
You can also use "ps -ef | more", to get a similar output
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
'''Print the Process Tree'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| mem
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
You can use either '''ps axuf''' or '''ps –ejH''' to display processes in a tree format. The tree structure will help to visualize the process and it’s parent process immediately. For clarity purpose, few columns have been cut-off in the output below.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
 
 
<nowiki># </nowiki>'''ps axuf'''116
{| style="border-spacing:0;"
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>56328</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">166811</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /lib/libselinux.so.1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /opt/VRTSralus/bin/beremote
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 10u
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| FIFO
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /opt/VRTSralus/bin/beremote
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0,13</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 972 /dev/initctl


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| migration
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /opt/VRTSralus/bin/beremote
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root cwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| DIR
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4096</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2 /</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| skipped…
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /opt/VRTSralus/bin/beremote
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|}
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
The lsof command by itself without may return lot of records as output, which may not be very meaningful except to give you a rough idea about how
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /opt/VRTSralus/bin/beremote


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Oct 14
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /opt/VRTSralus/bin/beremote


|-
many files are open in the system at any given point of view as shown below.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec03
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01 /usr/local/sbin/sshd


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec22
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1:08 /usr/local/sbin/sshd


|-
<nowiki># </nowiki>'''lsof | wc -l'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 23:35
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ /usr/local/sbin/sshd
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 511
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 23:35
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ -bash


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 511
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \_ ps axuf


|}
3093
Note: You can also use pstree command to display process in tree structure.




'''View Processes Owned by a Particular User'''
'''View open files by a specific user'''




The following command displays all the process owned by Linux user-name: oracle.
Use lsof –u option to display all the files opened by a specific user.






{| style="border-spacing:0;"
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''ps U oracle'''
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''lsof –u ramesh'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PID TTY
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| vi
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>STAT</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7190</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| TIME COMMAND
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ramesh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| txt
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">REG</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">474608</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 5014
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 475196 /bin/vi
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01 /oracle/bin/tnslsnr
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 7124 ?
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| sshd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7163</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 ora_q002_med
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ramesh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3u
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">IPv6</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">15088263</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|}
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8206
TCP dev-db:ssh->abc-12-12-12-12.socal.res.rr.com:2631 (ESTABLISHED)
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 ora_cjq0_med


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8852
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01 ora_pmon_med


|-
A system administrator can use this command to get some idea on what users are executing on the system.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8854
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 ora_psp0_med


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8911
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>Ss</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:02 oraclemed (LOCAL=NO)


|}
'''List Users of a particular file'''
'''View Processes Owned by Current User'''




Following command displays all the process owned by the current user.
If you like to view all the users who are using a particular file, use lsof as shown below. In this example, it displays all users who are currently using vi.






{| style="border-spacing:0;"
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''ps U $USER'''
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>lsof /bin/vi
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PID TTY
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| COMMAND
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">STAT</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| TIME COMMAND
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| USER
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">FD</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">TYPE DEVICE</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">SIZE</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">NODE</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| NAME


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 10329
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| vi
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ?
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7258</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">S</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 sshd: ramesh@pts/1,pts/2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">txt</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">474608</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">475196</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /bin/vi


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 10330
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| vi
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7300</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Ss</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ramesh txt</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 -bash
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">474608</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">475196</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /bin/vi


|}
|}
117
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10354</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Ss+
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 -bash


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10530</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| R+
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 ps U ramesh


|}
=='''Hack 96. Sar Command'''==
'''Hack 92. Df Command'''




df command (disk free) displays the amount of total and free disk space available on the mounted filesystems.
Sar commands comes with the sysstat package. Make sure sysstat is installed. If you don’t have sar installed on your system, get it from [http://pagesperso-orange.fr/sebastien.godard/ ][http://pagesperso-orange.fr/sebastien.godard/ Sysstat project].




Syntax: df <nowiki>[options] </nowiki><nowiki>[name]</nowiki>
Sar is an excellent monitoring tool that displays performance data of pretty much every resource of the system including CPU, memory, IO, paging, networking, interrupts etc.,




'''How much GB of disk space is free on my system?'''
Sar Collects, Reports (displays) and Saves the performance data. Let us look at all the three aspects separately




Use df -h as shown below. Option -h displays the values in human readable format (for example: K for Kb, M for Mb and G for Gb). In the sample output below, / filesystem has 17GB of disk space available and /home/user filesystem has 70GB available.
'''Sadc - System activity data collector'''




<nowiki>/usr/lib/sadc (System activity data collector) command collects the system data at a specified time interval. This uses the daily activity data file that is located under /va/log/sa/sa[dd], where dd is the current day.</nowiki>


{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''df –h'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
'''Sa1 shell-script'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Filesystem
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Size</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Used Avail Use% Mounted on


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sda1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">64G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 44G
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 17G
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">73%</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /


|-
/usr/lib/sa1 in-turn calls the /usr/lib/sadcs. sa1 is invoked from the crontab as shown below. Run this every 5 minutes or 15 minutes depending on your need. I prefer to schedule it for every 5 minutes in the cron tab as shown below.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sdb1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">137G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 67G
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 70G
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">49%</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /home/user


|}
'''What type of filesystem do I have on my system?'''


<nowiki>*/5 </nowiki><nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>root /usr/lib/sa/sa1 1 1


Option -T will display the information about the filesystem Type. In this example / and /home/user filesystems are ext2. Option -a will display all the filesystems, including the 0 size special filesystem used by the system.


'''Sa2 shell-script'''


<nowiki># </nowiki>'''df -Tha'''


<nowiki>/usr/lib/sa2 is a shell script that will write a daily report in the /var/log/sa/sa[dd] file, where dd is the current day. Invoke the sa2 from the crontab once a day at midnight.</nowiki>


Filesystem Type Size Used Avail Use% Mounted on118
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


|-
<nowiki># </nowiki>59 23 <nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>root /usr/lib/sa/sa2 –A
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sda1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ext2</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>64G</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">44G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">17G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">73%</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sdb1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ext2</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>137G</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">67G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">70G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">49%</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /home/user


|-
'''Note: '''/etc/cron.d/sysstat files comes with the sysstat package that includes''' '''some default value for the sa1 and sa2, which you can change accordingly.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| none
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">proc</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">-</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /proc


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| none
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">sysfs</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">-</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /sys


|-
'''Display CPU Statistics using Sar Command'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| none
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">devpts</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">-</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/pts


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| none
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">tmpfs</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2.0G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2.0G</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0%</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/shm


|}
'''Hack 93. Kill Command'''


 
{| style="border-spacing:0;"
kill command can be used to terminate a running process. Typically this command is used to kill processes that are hanging and not responding.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''sar –u'''
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Syntax: kill <nowiki>[options] </nowiki><nowiki>[pids|commands]</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
'''How to kill a hanging process?'''
 
 
First, identify the process id of the particular process that you would like to kill using the ps command. Once you know the process id, pass it as a parameter to the kill command. The example below shows how to kill the hanging apache httpd process. Please note that typically you should use “apachectl stop” to stop apache.
 
 
 
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''ps aux | grep httpd'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux 2.6.9-42.ELsmp (dev-db)
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">01/01/2009</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| USER
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID %CPU %MEM</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">VSZ</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| RSS TTY STAT START TIME COMMAND


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:00:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| CPU
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31186</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %user
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">%nice</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.6</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">%system</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">23736 17556 ?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">%iowait</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %idle
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:40</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:05:01 AM
| colspan="4" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.70
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| colspan="0style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.85</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 95.45


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:10:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31187</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4.59
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.3</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.19</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">20640 14444 ?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.06</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 94.16
| colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:15:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.90
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.95</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.04</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 95.11
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:20:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4.06
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.01</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 94.93
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:25:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.89
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.87</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 95.23


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:37</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:30:01 AM
| colspan="4" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.89
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| colspan="0style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.87</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 95.23


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Skipped..
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''kill 31186 31187'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|}
119'''Linux 101 Hacks'''www.thegeekstuff.com


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Average: all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4.56
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.15</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 94.29


Please note that the above command tries to terminate the process graciously by sending a signal called SIGTERM. If the process does not get terminated, you can forcefully terminate the process by passing a signal called SIGKILL, using the option -9 as shown below. You should either be the owner of the process or a privileged user to kill a process.
|}
'''Note: '''If you need a break down of the performance data for the individual''' '''CPU’s, execute the following command.




<nowiki># </nowiki>'''kill -9 31186 31187'''
<nowiki># </nowiki>sar -u -P ALL




Another way to kill multiple processes easily is by adding the following two functions to the .bash_profile.
'''Display Disk IO Statistics using sar command'''
 
 
function psgrep ()
 
 
{
 
 
ps aux | grep "$1" | grep -v 'grep'
 
 
}
 
 
function psterm ()
 
 
{
 
 
<nowiki>[ ${#} -eq 0 ] && echo "usage: $FUNCNAME STRING" && return 0 local pid</nowiki>
 
 
pid=$(ps ax | grep "$1" | grep -v grep | awk '{ print $1 }') echo -e "terminating '$1' / process(es):\n$pid"
 
 
kill -SIGTERM $pid






{| style="border-spacing:0;"
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| }
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''sar –d'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux 2.6.9-42.ELsmp (dev-db)
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">01/01/2009</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| colspan="8" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Now do the following, to identify and kill all httpd processes.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:00:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| DEV
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tps
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">rd_sec/s</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| wr_sec/s


|-
|-
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''psgrep http'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:05:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev2-0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1.65
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.28</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 45.43
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:10:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev8-1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4.08</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>8.11</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 21.81
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Skipped..
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Average:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev2-0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4.66
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>120.77</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 69.45
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Average:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev8-1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.89</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>3.17</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8.02
|}
125
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">www.thegeekstuff.com</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| USER
| colspan="7"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Display networking Statistics using sar command'''
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID %CPU %MEM</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| VSZ
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">RSS TTY</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">STAT START</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| TIME


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| COMMAND
| colspan="4" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''sar -n DEV | more'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux 2.6.9-42.ELsmp (dev-db)
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31186</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">01/01/2009</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:00:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">IFACE</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">rxpck/s</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">txpck/s</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">rxbyt/s</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">txbyt/s</div>
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| rxcmp/s
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| txcmp/
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.6</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">23736</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">17556</div>
| colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| s
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:40</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">rxmcst/s</div>
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| apache
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:05:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">31187</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">lo</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.17</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.16</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.3</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25.31</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">20640</div>
| colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">23.33</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">14444</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul26
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:37</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/local/apache2/bin/httpd</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
|-
<nowiki># </nowiki>'''psterm httpd'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:10:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">eth0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">52.92</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">53.64</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10169.74</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">12178.57</div>


terminating 'httpd' / process(es): 31186120'''Linux 101 Hacks'''www.thegeekstuff.com
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


31187
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''sar -n SOCK |more'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux 2.6.9-42.ELsmp (dev-db)
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">01/01/2009</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


'''Hack 94. Du Command'''
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:00:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">totsck</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">tcpsck</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">udpsck</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">rawsck</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ip-frag</div>


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:05:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">50</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


du command (disk usage) will print the file space usage for a particular directory and its subdirectories.
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:10:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">50</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:15:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">53</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


'''How much space is taken by my home directory and all its subdirectories?'''
|}
=='''Hack 97. vmstat Command'''==




In the following example, option -s stands for summary only. i.e it displays only the total size of /home/jsmith and not the individual sizes of all the subdirectories inside the /home/jsmith. Option -h displays the information in a human readable format. i.e K for KB, M for MB and G for GB. The ~ indicates the user home directory. This command is same as “du -sh /home/jsmith”
For a typical performance monitoring all you need is only vmstat command. This display memory, swap, IO, system and cpu performance information.




<nowiki># </nowiki>'''du -sh ~'''
The following command executes vmstat every 1 second for 100 times.




320M/home/jsmith


{| style="border-spacing:0;"
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''vmstat 1 100'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


To get the subdirectories under /home/jsmith listed, execute the above command without the s option.
|-
 
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| procs
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>-----------</nowiki>
'''Hack 95. lsof commands.'''
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| memory----------
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ---
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| swap--
Lsof stands for ls open files, which will list all the open files in the system. The open files include network connection, devices and directories. The output of the lsof command will have the following columns:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>-----</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">io----</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">--system--</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>----cpu----</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| r
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| b
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| swpd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| free
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| buff cache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| si
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| so
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">bi</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">bo</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">in cs us sy id wa</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


o COMMAND process name.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 282120 134108 5797012
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 100</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 282120 134108 5797012
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 1007
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">359</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 0 100</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


o PID process ID
|}
126
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''Linux 101 Hacks'''</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">www.thegeekstuff.com</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 282120 134108 5797012</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 1117</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">577</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 100</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


o USER Username121'''Linux 101 Hacks'''www.thegeekstuff.com
|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
o FD file descriptor
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 282120 134108 5797012</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 1007</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">366</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 100</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
 
|}
'''Vmstat procs Section'''
 
 
o r field: Total number of runnable process o b field: Total number of blocked process




o TYPE node type of the file
'''Memory section'''




o DEVICE device number
o Swpd field: Used swap space o Free field: Available free RAM




o SIZE file size
o Buff field: RAM used for buffers




o NODE node number
o Cache field: RAM used for filesystem cache




o NAME full path of the file name.
'''Swap Section'''




'''View all open files of the system'''
o Si field: Amount of memory swapped from disk per second o So field: Amount of memory swapped to disk per second




Execute the lsof command without any parameter as shown below.
'''IO Section'''




o Bi field: Blocks received from disk o Bo field: Blocks sent to disk.


{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''lsof | more'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
'''System Section'''
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| COMMAND
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| PID
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| USER
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| FD
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| TYPE
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">DEVICE</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SIZE
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| NODE


|-
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| NAME
o In field: Number of interrupts per second.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
o Cs field: Number of context switches per second.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
'''CPU Section'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
o Us field: Time spend running user code. (non-kernel code)
 
 
o Sy field: Time spent running kernel code.
 
 
o Id field: Idle time.
 
 
o Wa field: Time spent waiting for the IO
 
 
=='''Hack 98. Netstat Command'''==
 
 
Netstat command displays the network related information such as network connections, routing tables, interface statistics. Following are few examples on how to use netstat command.
 
 
'''Display Active Internet Connections and domain sockets using netstat'''
 
 
<nowiki># </nowiki>'''netstat –an'''
 
 
Active Internet connections (servers and established)
 
 
 
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Proto Recv-Q Send-Q
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Local Address
| colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Foreign
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Address
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| State
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:5666
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| cwd
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:*
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| DIR
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4096</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2 /
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTEN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| rtd
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| DIR
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2 /</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:111
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| txt
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:*
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 32684
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 983101 /sbin/init


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTEN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| mem
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>106397</center>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 166798 /lib/ld-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:4086
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:*


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2.3.4.so
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTEN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| skipped..
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|}
Active UNIX domain sockets (servers and established)
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Proto
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| RefCnt Flags
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Type
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| State
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| I-Node Path
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| unix
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>[ </nowiki>ACC ]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| STREAM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTENING
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 7894
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /tmp/.font-unix/fs7100
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| unix
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>[ </nowiki>ACC ]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| STREAM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTENING
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 9662
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /tmp/.gdm_socket
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| mem
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>1454802</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 166799


|-
|-
| colspan="4" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /lib/tls/libc-2.3.4.so
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| unix
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>[ </nowiki>ACC ]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| STREAM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTENING
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 10897
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| @/tmp/fam-root-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
|}
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
 
'''Display Active Connections with Process ID and Program Name'''
 
 
This could be very helpful to identify which program has initiated a specific network connection.
 
 
<nowiki># </nowiki>'''netstat -tap'''
 
 
Active Internet connections (servers and established)
 
 
 
{| style="border-spacing:0;"
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Proto Recv-Q Send-Q Local Address
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Foreign</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Address
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| mem
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| State
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID/Program name</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>53736</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">163964</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /lib/libsepol.so.1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>*:nrpe</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>*:*</nowiki></div>
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTEN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16277/xinetd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 6,882: Line 7,313:


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| localhost.localdomain:smtp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| mem
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>*:*</nowiki></div>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTEN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 7263/sendmail: acce
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>56328</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">166811</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /lib/libselinux.so.1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">34</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| localhost.localdomain:54221
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| localhost.localdomain:4089
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| CLOSE_WAIT
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">29881/httpd</div>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3216</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev-db:ssh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">cpe-76-</div>
 
|}
94-215-154.soca:4682 ESTABLISHED 11717/sshd: ramesh
 
 
'''Display Routing Table'''
 
 
<nowiki># </nowiki>'''netstat –-route'''
 
 
 
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Kernel IP routing table
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Destination
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Gateway
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Genmask</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Flags
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">MSS</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Window
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">irtt Iface</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 6,908: Line 7,384:


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| init
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 192.168.1.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>*</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">255.255.255.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| U
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 10u
| colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| FIFO
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0,13</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 972 /dev/initctl


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| migration
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 eth0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root cwd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| DIR
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4096</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2 /</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| skipped…
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 162.244.0.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>*</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">255.255.0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| U
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 eth0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| default
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 192.168.1.1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0.0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| UG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 eth0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 6,945: Line 7,441:
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|}
|-
The lsof command by itself without may return lot of records as output, which may not be very meaningful except to give you a rough idea about how122'''Linux 101 Hacks'''www.thegeekstuff.com
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Display RAW network statistics'''
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


many files are open in the system at any given point of view as shown below.
|}
<nowiki># </nowiki>'''netstat --statistics --raw'''




<nowiki># </nowiki>'''lsof | wc -l'''
Ip:




3093
11080343 total packets received




'''View open files by a specific user'''
# forwarded


# with unknown protocol


Use lsof –u option to display all the files opened by a specific user.
0 incoming packets discarded




11037744 incoming packets delivered


{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''lsof –u ramesh'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
11199763 requests sent out Icmp:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| vi
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7190</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ramesh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| txt
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">REG</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">474608</div>


|-
577135 ICMP messages received
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 475196 /bin/vi
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| sshd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7163</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ramesh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3u
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">IPv6</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">15088263</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
64 input ICMP message failed. ICMP input histogram:
TCP dev-db:ssh->abc-12-12-12-12.socal.res.rr.com:2631 (ESTABLISHED)




A system administrator can use this command to get some idea on what users are executing on the system.
destination unreachable: 537 timeout in transit: 65 source quenches: 2


echo requests: 576476 echo replies: 12 timestamp request: 3 address mask request: 3


'''List Users of a particular file'''


581558 ICMP messages sent


If you like to view all the users who are using a particular file, use lsof as shown below. In this example, it displays all users who are currently using vi.


0 ICMP messages failed ICMP output histogram:




{| style="border-spacing:0;"
destination unreachable: 5079 echo replies: 576476 timestamp replies: 3
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>lsof /bin/vi
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| COMMAND
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| USER
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">FD</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">TYPE DEVICE</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">SIZE</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">NODE</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| NAME


|-
'''Misc Netstat Commands'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| vi
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7258</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">txt</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">474608</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">475196</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /bin/vi


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| vi
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">7300</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ramesh txt</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| REG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8,1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">474608</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">475196</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /bin/vi


|}
# '''<nowiki># netstat –-tcp –-numeric </nowiki>'''List of TCP connection to and from the''' '''machine.  
123'''Linux 101 Hacks'''www.thegeekstuff.com


# '''<nowiki># netstat --tcp --listening –-programs </nowiki>'''Display TCP port that the''' '''server is listening on along with the program that is listening on that particular port.


'''Hack 96. Sar Command'''
o '''<nowiki># netstat –rnC</nowiki>''' Display the routing cache




Sar commands comes with the sysstat package. Make sure sysstat is installed. If you don’t have sar installed on your system, get it from [http://pagesperso-orange.fr/sebastien.godard/ ][http://pagesperso-orange.fr/sebastien.godard/ Sysstat project].
=='''Hack 99. Sysctl Command'''Linux kernel parameter can be changed on the fly using sysctl command.==




Sar is an excellent monitoring tool that displays performance data of pretty much every resource of the system including CPU, memory, IO, paging, networking, interrupts etc.,




Sar Collects, Reports (displays) and Saves the performance data. Let us look at all the three aspects separately


Sysctl helps to configure the Linux kernel parameters during runtime.


'''Sadc - System activity data collector'''




<nowiki>/usr/lib/sadc (System activity data collector) command collects the system data at a specified time interval. This uses the daily activity data file that is located under /va/log/sa/sa[dd], where dd is the current day.</nowiki>
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''sysctl –a'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev.cdrom.autoclose <nowiki>= </nowiki>1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


'''Sa1 shell-script'''
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| fs.quota.writes <nowiki>= </nowiki>0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| kernel.ctrl-alt-del <nowiki>= </nowiki>0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| kernel.domainname <nowiki>= </nowiki>(none)
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


/usr/lib/sa1 in-turn calls the /usr/lib/sadcs. sa1 is invoked from the crontab as shown below. Run this every 5 minutes or 15 minutes depending on your need. I prefer to schedule it for every 5 minutes in the cron tab as shown below.
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| kernel.exec-shield <nowiki>= </nowiki>1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| net.core.somaxconn <nowiki>= </nowiki>128
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| net.ipv4.tcp_window_scaling <nowiki>= </nowiki>1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


<nowiki>*/5 </nowiki><nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>root /usr/lib/sa/sa1 1 1
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| net.ipv4.tcp_wmem <nowiki>= </nowiki>4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16384</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">131072</div>


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| net.ipv6.route.mtu_expires <nowiki>= </nowiki>600
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


'''Sa2 shell-script'''
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| sunrpc.udp_slot_table_entries <nowiki>= </nowiki>16
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| vm.block_dump <nowiki>= </nowiki>0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


<nowiki>/usr/lib/sa2 is a shell script that will write a daily report in the /var/log/sa/sa[dd] file, where dd is the current day. Invoke the sa2 from the crontab once a day at midnight.</nowiki>
|}
'''Modify Kernel parameter in /etc/sysctl.conf for permanent change'''




<nowiki># </nowiki>59 23 <nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>root /usr/lib/sa/sa2 –A124'''Linux 101 Hacks'''www.thegeekstuff.com
After modifying the kernel parameter in the /etc/sysctl.conf, execute sysctl – p to commit the changes. The changes will still be there after the reboot.




'''Note: '''/etc/cron.d/sysstat files comes with the sysstat package that includes''' '''some default value for the sa1 and sa2, which you can change accordingly.
# vi /etc/sysctl.conf


# '''sysctl –p '''


'''Display CPU Statistics using Sar Command'''
'''Modify kernel parameter temporarily'''




To temporarily modify a kernel parameter, execute the following command. Please note that after reboot these changes will be lost.


{| style="border-spacing:0;"
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''sar –u'''
<nowiki># </nowiki>sysctl –w {variable-name=value}
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
 
=='''Hack 100. Nice Command'''==
 
 
Kernel decides how much processor time is required for a process based on the nice value. Possible nice value range is: -20 to 20. A process that has a nice value of -20 is very high priority. The process that has a nice value of 20 is very low priority.
 
 
Use ps axl to display the nice value of all running process as shown below.
 
 
 
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''ps axl'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 7,107: Line 7,610:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux 2.6.9-42.ELsmp (dev-db)
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">01/01/2009</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:00:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| F
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| CPU
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">UID</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %user
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">%nice</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PPID PRI</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">%system</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| NI
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">%iowait</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">VSZ</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| %idle
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| RSS WCHAN
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| STAT TTY


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:05:01 AM
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| TIME COMMAND
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.70
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.85</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 95.45
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:10:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4.59
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.19</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.06</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 94.16
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2172</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">552</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:15:01 AM
| colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:17 init <nowiki>[5]</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.90
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.95</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.04</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 95.11
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:20:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4.06
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.01</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 94.93
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:25:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.89
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.87</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 95.23
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:30:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.89
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.87</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 95.23
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Skipped..
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 7,186: Line 7,658:


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Average: all
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4.56
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">34</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.15</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">19</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 94.29
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ksofti SN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>


|}
|-
'''Note: '''If you need a break down of the performance data for the individual''' '''CPU’s, execute the following command.
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3:18 <nowiki>[ksoftirqd/0]</nowiki>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
<nowiki># </nowiki>sar -u -P ALL
 
 
'''Display Disk IO Statistics using sar command'''
 
 
 
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''sar –d'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux 2.6.9-42.ELsmp (dev-db)
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">01/01/2009</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:00:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| DEV
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tps
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">rd_sec/s</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| wr_sec/s
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5 -10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| worker <nowiki>S<</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:05:01 AM
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01 <nowiki>[events/0]</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev2-0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1.65
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.28</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 45.43
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5145</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 10 32124 18592 -
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SNs
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:10:01 AM
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:08 /usr/bin/python
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev8-1
| colspan="7" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/bin/rhn-applet-gui --sm-client-id</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4.08</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>8.11</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 21.81


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Skipped..
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| default4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 7,247: Line 7,728:


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Average:
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev2-0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4.66
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5147</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>120.77</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5142</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 69.45
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3528</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">604</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Average:
| colspan="7" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /sbin/pam_timestamp_check -d
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev8-1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.89</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>3.17</center>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8.02
 
|}
125
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">www.thegeekstuff.com</div>


|-
|-
| colspan="7"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Display networking Statistics using sar command'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1
| colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">503 17552</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4180</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16</div>
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 14208 3920</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>


|-
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''sar -n DEV | more'''
| colspan="8"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01 /home/www/apache2/bin/httpd -f
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux 2.6.9-42.ELsmp (dev-db)
| colspan="7"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /home/www/apache2/conf/httpd.conf
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">01/01/2009</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -k start
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
|}
'''How to assign a low priority to a shell-script? (higher nice value)'''
 
 
In the example below, when I started the nice-test.sh script in the background, it took the nice value of 0.
 
 
$ ./nice-test.sh & <nowiki>[3] 13009</nowiki>
 
 
$ ps axl | grep nice-test
 
 
0 509 13009 12863 17 0 4652 972 wait S
 
pts/10:00 /bin/bash ./nice-test.sh
 
 
<nowiki>[</nowiki>'''Note''': 6<sup>th</sup> column with value 0 is the nice.]
 
 
Now, let us execute the same shell script with a different nice value as shown below.
 


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:00:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">IFACE</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">rxpck/s</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">txpck/s</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">rxbyt/s</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">txbyt/s</div>


|-
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| rxcmp/s
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''nice -10 ./nice-test.sh &'''
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| txcmp/
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 7,302: Line 7,801:


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| s
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>[1]</nowiki>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">rxmcst/s</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 13016
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 7,311: Line 7,811:


|-
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:05:01 AM
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ ps axl | grep nice-test
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">lo</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.17</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.16</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25.31</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">23.33</div>
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.00
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
Line 7,329: Line 7,819:
|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 509 13016 12863
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">30</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4236</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 968 wait
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SN
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:10:01 AM
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">eth0</div>
| colspan="5" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /bin/bash ./nice-test.sh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">52.92</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">53.64</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10169.74</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">12178.57</div>


|-
|}
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.00
<nowiki>[</nowiki>'''Note''': 6<sup>th</sup> column with value 10 is the nice value for the shell-script.]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.00</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
'''How to assign a high priority to a shell-script? (Lower nice value)'''
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''sar -n SOCK |more'''
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
In the following example, let us assign a nice value of -10 (minus 10) to the nice-test.sh shellscript.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
$ '''nice --10 ./nice-test.sh &'''


|-
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux 2.6.9-42.ELsmp (dev-db)
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">01/01/2009</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
<nowiki>[1] </nowiki>13021
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:00:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">totsck</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">tcpsck</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">udpsck</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">rawsck</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ip-frag</div>


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:05:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">50</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


|-
$ nice: cannot set priority: Permission denied
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:10:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">50</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12:15:01 AM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">53</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


|}
'''Note: '''Only root user can set a negative nice value. Login as root and try the''' '''same. Please note that there is a double dash before the 10 in the nice command below.
'''Hack 97. vmstat Command'''




For a typical performance monitoring all you need is only vmstat command. This display memory, swap, IO, system and cpu performance information.
# '''nice --10 ./nice-test.sh & '''


<nowiki>[1] </nowiki>13060


The following command executes vmstat every 1 second for 100 times.


# ps axl | grep nice-test




{| style="border-spacing:0;"
{| style="border-spacing:0;"
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''vmstat 1 100'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 13060 13024</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10 -10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5388</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 964 wait
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>S<</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /bin/bash ./nice-test.sh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|}
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| procs
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>-----------</nowiki>
 
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| memory----------
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ---
<nowiki>[Note: 6</nowiki><sup>th</sup> column with value -10 is the nice value of the shell-script.]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| swap--
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>-----</nowiki></div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">io----</div>
=='''Hack 101. Renice Command'''==
 
 
Renice alters the scheduling priority of a running process.
 
 
'''How to decrease the priority of a running process? (Increase nice)'''
 
 
In the example below, an existing shell-script is running at nice value of 10. (6<sup>th</sup> column in the ps output)
 
 
 
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $
| colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ps axl | grep nice-test</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">--system--</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>----cpu----</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| r
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| b
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">509 13245 13216</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| swpd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">30</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| free
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| buff cache
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5244</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| si
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 968 wait
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| so
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">bi</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">bo</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">in cs us sy id wa</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /bin/bash ./nice-test.sh
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 282120 134108 5797012
| colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
|}
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
To increase the nice value (thus reducing the priority), execute the renice command as shown below.
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 0</div>
$ '''renice 16 -p 13245'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 100</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
 
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
13245: old priority 10, new priority 16
 


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 282120 134108 5797012
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 1007
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">359</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 0 100</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


|}
126
{| style="border-spacing:0;"
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">'''Linux 101 Hacks'''</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ps axl | grep nice-test</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">www.thegeekstuff.com</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">509 13245 13216</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 282120 134108 5797012</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">36</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5244</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 968 wait
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 1117</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">577</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 100</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


|-
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="5" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /bin/bash ./nice-test.sh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 282120 134108 5797012</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|  
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 1007</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">366</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 100</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>


|}
|}
'''Vmstat procs Section'''
<nowiki>[</nowiki>'''Note''': Now, the 6<sup>th</sup> column of the nice-test.sh (PID 13245) shows the new nice value of 16.]




o r field: Total number of runnable process o b field: Total number of blocked process
'''How to increase the priority of a running process? (Decrease nice)'''




'''Memory section'''
In the example below, an existing shell-script is running at a nice value of 10.134
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com


|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| (6<sup>th</sup> column in the ps output)
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


o Swpd field: Used swap space o Free field: Available free RAM
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ ps axl | grep nice-test
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">509 13254 13216</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">30</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4412</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 968 wait
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SN


o Buff field: RAM used for buffers
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| colspan="6"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /bin/bash ./nice-test.sh
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
In increase the priority, give a lower nice value as shown below. However, only root can increase the priority of a running process, else you’ll get the following error message.


o Cache field: RAM used for filesystem cache


$ '''renice 5 -p 13254'''


'''Swap Section'''


renice: 13254: setpriority: Permission denied Login as root to increase the priority of a running process


o Si field: Amount of memory swapped from disk per second o So field: Amount of memory swapped to disk per second


$ su -


'''IO Section'''


<nowiki># </nowiki>'''renice 5 -p 13254'''


o Bi field: Blocks received from disk o Bo field: Blocks sent to disk.


13254: old priority 10, new priority 5


'''System Section'''




o In field: Number of interrupts per second.
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>#</nowiki>
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ps axl | grep nice-test</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">509 13254 13216</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 5
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4412</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 968 wait
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SN


o Cs field: Number of context switches per second.127'''Linux 101 Hacks'''www.thegeekstuff.com
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:00 /bin/bash</div>
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ./nice-test.sh
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|


|}
<nowiki>[</nowiki>'''Note''': The 6<sup>th</sup> column now shows a lower nice value of 5 (increased priority)]


'''CPU Section'''


'''12 Amazing and Essential Linux Books'''


o Us field: Time spend running user code. (non-kernel code)


For further reading on Linux, I recommend the following books. The 12 Linux books mentioned here by no means are comprehensive or authoritative list. But, these 12 Books are few of my favorites that I enjoyed reading over the years and I strongly believe will enhance your technical abilities on Linux, if you have not read them yet.


o Sy field: Time spent running kernel code.


# [http://www.amazon.com/gp/product/1565922255?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1565922255 ][http://www.amazon.com/gp/product/1565922255?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1565922255 Sed and Awk], by Dale Dougherty and Arnold Robbins'''. Sed and Awk''' '''have transformed the way I worked on Linux command line. This book is the only material you would ever need on Sed and Awk. Once you’ve mastered even the basics of Sed and Awk, you’ll be amazed with the amount of complex tasks you can perform very quickly and elegently. For my day-to-day quick reference of sed and awk examples, I use the Sed and Awk Pocket Reference, written by the same author.


o Id field: Idle time.
# [http://www.amazon.com/gp/product/059652983X?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=059652983X ][http://www.amazon.com/gp/product/059652983X?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=059652983X Learning the Vi and Vim Editors], by Arnold Robbins'''. I’m a''' '''command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to carry the Vi editor pocket reference with me all the times. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.  


# [http://www.amazon.com/gp/product/0596526784?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596526784 ][http://www.amazon.com/gp/product/0596526784?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596526784 Bash Cookbook], by Carl Albing, JP Vossen and Cameron Newham'''.''' '''Whether you are a sysadmin, DBA or a developer, you have to write shell script at some point. A wise sysadmin knows that once you’ve mastered the shell-scripting techniques, you can put your servers on auto-pilot mode by letting the shell-scripts do the grunt work. To get to the auto-pilot mode of sysadmin, you definitely need to master the examples provided in this cookbook. There are quiet few Bash shell books out there. But, this books tops them all by giving lot of detailed examples.


o Wa field: Time spent waiting for the IO
# [http://www.amazon.com/gp/product/0596008953?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596008953 ][http://www.amazon.com/gp/product/0596008953?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596008953 SSH, The Secure Shell], by Daniel J. Barrett, Richard E. Silverman and Robert G. Byrnes'''. This is hands-down the best book on SSH. This''' '''book explains both theoretical and practical aspects of SSH. Using
 
 
'''Hack 98. Netstat Command'''
 
 
Netstat command displays the network related information such as network connections, routing tables, interface statistics. Following are few examples on how to use netstat command.
 
 
'''Display Active Internet Connections and domain sockets using netstat'''
 
 
<nowiki># </nowiki>'''netstat –an'''
 
 
Active Internet connections (servers and established)
 
 
 
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Proto Recv-Q Send-Q
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Local Address
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Foreign
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Address
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| State
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:5666
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:*
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTEN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:111
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:*
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTEN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:4086
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0.0.0:*
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTEN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| skipped..
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|}
Active UNIX domain sockets (servers and established)
 
 
 
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Proto
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| RefCnt Flags
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Type
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| State
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| I-Node Path
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| unix
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>[ </nowiki>ACC ]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| STREAM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTENING
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 7894
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /tmp/.font-unix/fs7100
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| unix
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>[ </nowiki>ACC ]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| STREAM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTENING
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 9662
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /tmp/.gdm_socket
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| unix
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>[ </nowiki>ACC ]
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| STREAM
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTENING
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 10897
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| @/tmp/fam-root-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|}
128'''Linux 101 Hacks'''www.thegeekstuff.com
 
 
'''Display Active Connections with Process ID and Program Name'''
 
 
This could be very helpful to identify which program has initiated a specific network connection.
 
 
<nowiki># </nowiki>'''netstat -tap'''
 
 
Active Internet connections (servers and established)
 
 
 
{| style="border-spacing:0;"
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Proto Recv-Q Send-Q Local Address
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Foreign</div>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Address
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| State
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID/Program name</div>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>*:nrpe</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>*:*</nowiki></div>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTEN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16277/xinetd
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| localhost.localdomain:smtp
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>*:*</nowiki></div>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| LISTEN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 7263/sendmail: acce
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">34</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| localhost.localdomain:54221
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| localhost.localdomain:4089
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| CLOSE_WAIT
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">29881/httpd</div>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tcp
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3216</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev-db:ssh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">cpe-76-</div>
 
|}
94-215-154.soca:4682 ESTABLISHED 11717/sshd: ramesh
 
 
'''Display Routing Table'''
 
 
<nowiki># </nowiki>'''netstat –-route'''
 
 
 
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Kernel IP routing table
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Destination
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Gateway
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Genmask</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Flags
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">MSS</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Window
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">irtt Iface</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 192.168.1.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right"><nowiki>*</nowiki></div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">255.255.255.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| U
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 eth0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 162.244.0.0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>*</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">255.255.0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| U
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 eth0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| default
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 192.168.1.1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0.0.0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| UG
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 0</div>
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 eth0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Display RAW network statistics'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|}
<nowiki># </nowiki>'''netstat --statistics --raw'''
 
 
Ip:129'''Linux 101 Hacks'''www.thegeekstuff.com
 
 
11080343 total packets received
 
 
# forwarded
 
# with unknown protocol
 
0 incoming packets discarded
 
 
11037744 incoming packets delivered
 
 
11199763 requests sent out Icmp:
 
577135 ICMP messages received
 
 
64 input ICMP message failed. ICMP input histogram:
 
 
destination unreachable: 537 timeout in transit: 65 source quenches: 2
 
echo requests: 576476 echo replies: 12 timestamp request: 3 address mask request: 3
 
 
581558 ICMP messages sent
 
 
0 ICMP messages failed ICMP output histogram:
 
 
destination unreachable: 5079 echo replies: 576476 timestamp replies: 3
 
 
'''Misc Netstat Commands'''
 
 
# '''<nowiki># netstat –-tcp –-numeric </nowiki>'''List of TCP connection to and from the''' '''machine.
 
# '''<nowiki># netstat --tcp --listening –-programs </nowiki>'''Display TCP port that the''' '''server is listening on along with the program that is listening on that particular port.
 
o '''<nowiki># netstat –rnC</nowiki>''' Display the routing cache
 
 
'''Hack 99. Sysctl Command'''Linux kernel parameter can be changed on the fly using sysctl command.
 
 
130'''Linux 101 Hacks'''www.thegeekstuff.com
 
 
Sysctl helps to configure the Linux kernel parameters during runtime.
 
 
 
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''sysctl –a'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| dev.cdrom.autoclose <nowiki>= </nowiki>1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| fs.quota.writes <nowiki>= </nowiki>0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| kernel.ctrl-alt-del <nowiki>= </nowiki>0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| kernel.domainname <nowiki>= </nowiki>(none)
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| kernel.exec-shield <nowiki>= </nowiki>1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| net.core.somaxconn <nowiki>= </nowiki>128
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| net.ipv4.tcp_window_scaling <nowiki>= </nowiki>1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| net.ipv4.tcp_wmem <nowiki>= </nowiki>4096
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16384</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">131072</div>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| net.ipv6.route.mtu_expires <nowiki>= </nowiki>600
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| sunrpc.udp_slot_table_entries <nowiki>= </nowiki>16
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| vm.block_dump <nowiki>= </nowiki>0
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|}
'''Modify Kernel parameter in /etc/sysctl.conf for permanent change'''
 
 
After modifying the kernel parameter in the /etc/sysctl.conf, execute sysctl – p to commit the changes. The changes will still be there after the reboot.
 
 
# vi /etc/sysctl.conf
 
# '''sysctl –p '''
 
'''Modify kernel parameter temporarily'''
 
 
To temporarily modify a kernel parameter, execute the following command. Please note that after reboot these changes will be lost.
 
 
<nowiki># </nowiki>sysctl –w {variable-name=value}131'''Linux 101 Hacks'''www.thegeekstuff.com
 
 
'''Hack 100. Nice Command'''
 
 
Kernel decides how much processor time is required for a process based on the nice value. Possible nice value range is: -20 to 20. A process that has a nice value of -20 is very high priority. The process that has a nice value of 20 is very low priority.
 
 
Use ps axl to display the nice value of all running process as shown below.
 
 
 
{| style="border-spacing:0;"
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''ps axl'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| F
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">UID</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PID</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">PPID PRI</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| NI
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">VSZ</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| RSS WCHAN
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| STAT TTY
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| TIME COMMAND
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2172</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">552</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:17 init <nowiki>[5]</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">34</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">19</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ksofti SN
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
 
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3:18 <nowiki>[ksoftirqd/0]</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5 -10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| worker <nowiki>S<</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01 <nowiki>[events/0]</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5145</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25</div>
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 10 32124 18592 -
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SNs
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
 
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:08 /usr/bin/python
| colspan="7"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">/usr/bin/rhn-applet-gui --sm-client-id</div>
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| default4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5147</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5142</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3528</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">604</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
 
|-
| colspan="7"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /sbin/pam_timestamp_check -d
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">503 17552</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4180</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16</div>
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 14208 3920</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">?</div>
 
|-
| colspan="8"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:01 /home/www/apache2/bin/httpd -f
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="7"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /home/www/apache2/conf/httpd.conf
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| -k start
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|}
'''How to assign a low priority to a shell-script? (higher nice value)'''
 
 
In the example below, when I started the nice-test.sh script in the background, it took the nice value of 0.
 
 
$ ./nice-test.sh & <nowiki>[3] 13009</nowiki>
 
 
$ ps axl | grep nice-test
 
 
0 509 13009 12863 17 0 4652 972 wait S132'''Linux 101 Hacks'''www.thegeekstuff.com
 
 
pts/10:00 /bin/bash ./nice-test.sh
 
 
<nowiki>[</nowiki>'''Note''': 6<sup>th</sup> column with value 0 is the nice.]
 
 
Now, let us execute the same shell script with a different nice value as shown below.
 
 
 
{| style="border-spacing:0;"
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''nice -10 ./nice-test.sh &'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>[1]</nowiki>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 13016
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ ps axl | grep nice-test
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 509 13016 12863
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">30</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4236</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 968 wait
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SN
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /bin/bash ./nice-test.sh
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|}
<nowiki>[</nowiki>'''Note''': 6<sup>th</sup> column with value 10 is the nice value for the shell-script.]
 
 
'''How to assign a high priority to a shell-script? (Lower nice value)'''
 
 
In the following example, let us assign a nice value of -10 (minus 10) to the nice-test.sh shellscript.
 
 
$ '''nice --10 ./nice-test.sh &'''
 
 
<nowiki>[1] </nowiki>13021
 
 
$ nice: cannot set priority: Permission denied
 
 
'''Note: '''Only root user can set a negative nice value. Login as root and try the''' '''same. Please note that there is a double dash before the 10 in the nice command below.
 
 
# '''nice --10 ./nice-test.sh & '''
 
<nowiki>[1] </nowiki>13060
 
 
# ps axl | grep nice-test
 
 
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0 13060 13024</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10 -10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5388</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 964 wait
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>S<</nowiki>
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /bin/bash ./nice-test.sh
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|}
133'''Linux 101 Hacks'''www.thegeekstuff.com
 
 
<nowiki>[Note: 6</nowiki><sup>th</sup> column with value -10 is the nice value of the shell-script.]
 
 
'''Hack 101. Renice Command'''
 
 
Renice alters the scheduling priority of a running process.
 
 
'''How to decrease the priority of a running process? (Increase nice)'''
 
 
In the example below, an existing shell-script is running at nice value of 10. (6<sup>th</sup> column in the ps output)
 
 
 
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ps axl | grep nice-test</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">509 13245 13216</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">30</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5244</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 968 wait
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SN
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /bin/bash ./nice-test.sh
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|}
To increase the nice value (thus reducing the priority), execute the renice command as shown below.
 
 
$ '''renice 16 -p 13245'''
 
 
13245: old priority 10, new priority 16
 
 
 
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ps axl | grep nice-test</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">509 13245 13216</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">36</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">16</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5244</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 968 wait
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SN
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| colspan="5"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /bin/bash ./nice-test.sh
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|}
<nowiki>[</nowiki>'''Note''': Now, the 6<sup>th</sup> column of the nice-test.sh (PID 13245) shows the new nice value of 16.]
 
 
'''How to increase the priority of a running process? (Decrease nice)'''
 
 
In the example below, an existing shell-script is running at a nice value of 10.134
{| style="border-spacing:0;"
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks'''
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com
 
|-
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| (6<sup>th</sup> column in the ps output)
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| colspan="4"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ ps axl | grep nice-test
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">509 13254 13216</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">30</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">10</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4412</div>
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 968 wait
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SN
 
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| colspan="6"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 /bin/bash ./nice-test.sh
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
 
|}
In increase the priority, give a lower nice value as shown below. However, only root can increase the priority of a running process, else you’ll get the following error message.
 
 
$ '''renice 5 -p 13254'''
 
 
renice: 13254: setpriority: Permission denied Login as root to increase the priority of a running process




$ su -
<nowiki># </nowiki>'''renice 5 -p 13254'''
13254: old priority 10, new priority 5
{| style="border-spacing:0;"
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki>#</nowiki>
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ps axl | grep nice-test</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|-
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">509 13254 13216</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">25</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 5
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4412</div>
| style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 968 wait
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| SN
|-
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/1
| colspan="2"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0:00 /bin/bash</div>
| colspan="3"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ./nice-test.sh
| colspan="0"  style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"|
|}
<nowiki>[</nowiki>'''Note''': The 6<sup>th</sup> column now shows a lower nice value of 5 (increased priority)]135'''Linux 101 Hacks'''www.thegeekstuff.com
'''12 Amazing and Essential Linux Books'''
For further reading on Linux, I recommend the following books. The 12 Linux books mentioned here by no means are comprehensive or authoritative list. But, these 12 Books are few of my favorites that I enjoyed reading over the years and I strongly believe will enhance your technical abilities on Linux, if you have not read them yet.
# [http://www.amazon.com/gp/product/1565922255?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1565922255 ][http://www.amazon.com/gp/product/1565922255?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1565922255 Sed and Awk], by Dale Dougherty and Arnold Robbins'''. Sed and Awk''' '''have transformed the way I worked on Linux command line. This book is the only material you would ever need on Sed and Awk. Once you’ve mastered even the basics of Sed and Awk, you’ll be amazed with the amount of complex tasks you can perform very quickly and elegently. For my day-to-day quick reference of sed and awk examples, I use the Sed and Awk Pocket Reference, written by the same author.
# [http://www.amazon.com/gp/product/059652983X?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=059652983X ][http://www.amazon.com/gp/product/059652983X?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=059652983X Learning the Vi and Vim Editors], by Arnold Robbins'''. I’m a''' '''command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to carry the Vi editor pocket reference with me all the times. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.
# [http://www.amazon.com/gp/product/0596526784?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596526784 ][http://www.amazon.com/gp/product/0596526784?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596526784 Bash Cookbook], by Carl Albing, JP Vossen and Cameron Newham'''.''' '''Whether you are a sysadmin, DBA or a developer, you have to write shell script at some point. A wise sysadmin knows that once you’ve mastered the shell-scripting techniques, you can put your servers on auto-pilot mode by letting the shell-scripts do the grunt work. To get to the auto-pilot mode of sysadmin, you definitely need to master the examples provided in this cookbook. There are quiet few Bash shell books out there. But, this books tops them all by giving lot of detailed examples.
# [http://www.amazon.com/gp/product/0596008953?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596008953 ][http://www.amazon.com/gp/product/0596008953?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596008953 SSH, The Secure Shell], by Daniel J. Barrett, Richard E. Silverman and Robert G. Byrnes'''. This is hands-down the best book on SSH. This''' '''book explains both theoretical and practical aspects of SSH. Using
136'''Linux 101 Hacks'''www.thegeekstuff.com




Line 8,516: Line 8,073:
# [http://www.amazon.com/gp/product/0596006403?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596006403 ][http://www.amazon.com/gp/product/0596006403?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596006403 Linux Cookbook], by Carla Schroder'''. This book covers Linux features''' '''from both users and system administrators point of view. There are  
# [http://www.amazon.com/gp/product/0596006403?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596006403 ][http://www.amazon.com/gp/product/0596006403?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596006403 Linux Cookbook], by Carla Schroder'''. This book covers Linux features''' '''from both users and system administrators point of view. There are  


137'''Linux 101 Hacks'''www.thegeekstuff.com
 




Line 8,528: Line 8,085:
# [http://www.amazon.com/gp/product/1590599918?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1590599918 ][http://www.amazon.com/gp/product/1590599918?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1590599918 Beginning Ubuntu Linux], by Keir Thomas and Jaime Sicam'''. For''' '''those who like to transition from Windows to Linux, install Ubuntu Linux on one of your old laptop or desktop and get this book. I strongly believe in spreading the news about Linux to those who don’t use it. If you want any of your loved ones or friends to learn Linux, install Ubuntu on an old laptop and give this book as a gift to them. They’ll definitely be very thankful to you.  
# [http://www.amazon.com/gp/product/1590599918?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1590599918 ][http://www.amazon.com/gp/product/1590599918?ie=UTF8&tag=thgest-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1590599918 Beginning Ubuntu Linux], by Keir Thomas and Jaime Sicam'''. For''' '''those who like to transition from Windows to Linux, install Ubuntu Linux on one of your old laptop or desktop and get this book. I strongly believe in spreading the news about Linux to those who don’t use it. If you want any of your loved ones or friends to learn Linux, install Ubuntu on an old laptop and give this book as a gift to them. They’ll definitely be very thankful to you.  


138'''Linux 101 Hacks'''www.thegeekstuff.com




Line 8,576: Line 8,132:
# [http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ ][http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ Midnight Commander (mc) Guide: Powerful Text based File ][http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ Manager] [http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ ][http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ for Unix]  
# [http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ ][http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ Midnight Commander (mc) Guide: Powerful Text based File ][http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ Manager] [http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ ][http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ for Unix]  


139'''Linux 101 Hacks'''www.thegeekstuff.com





Latest revision as of 07:38, 24 October 2013


Linux 101 Hacks[edit]

www.thegeekstuff.com


Introduction[edit]

”There are only 10 types of people in the world — those who understand [1]binary, those who don’t, and those who understand [2]gray code


— Geek


There are total of 101 hacks in this book that will help you build a strong foundation in Linux. All the hacks in this book are explained with appropriate Linux command examples that are easy to follow.


This book contains 12 chapters. Hacks mentioned in 6 chapters are based on the articles that I’ve already posted on my blog. Hacks mentioned in rest of the 6 chapters are brand new.


I’m Ramesh Natarajan, author of [3]The Geek Stuff[4]blog and this eBook.

[[Image:]]


I have done intensive programming on several languages and C is my favorite. I have done lot of work on the infrastructure side including Linux system administration, DBA, Networking, Hardware and Storage (EMC).


I have also developed [5]Password Dragon[6]— free, easy and secure password manager that runs on Windows, Linux and Mac.


If you have any feedback about this eBook, please use this [7]contact form[8]to get in touch with me.


Copyright © 2009 – Ramesh Natarajan


Foreword[edit]

Another collection of hacks? Yes! If you have just completed your first admin course or looking for better ways to get the job done the "Linux 101 Hacks" eBook is a good point to start. These useful tips are concise, well written and easy to read.


Well done - I will recommend this eBook to my students.


--Prof. Dr. Fritz Mehner, FH Südwestfalen, Germany


(Author of several [9]Vim plugins, including [10]bash-support vim plugin)


Version[edit]

Version Date Revisions
1.0 12-Feb-2009 First Edition

Download the [11]latest version of the book here.

Chapter 1: Powerful CD Command Hacks[edit]

cd is one of the most frequently used commands during a UNIX session. The 6 cd command hacks mentioned in this chapter will boost your productivity instantly and make it easier to navigate the directory structure from command line.


Hack 1. Use CDPATH to define the base directory for cd command[edit]

If you are frequently performing cd to subdirectories of a specific parent directory, you can set the CDPATH to the parent directory and perform cd to the subdirectories without giving the parent directory path as explained below.


[ramesh@dev-db ~]# pwd /home/ramesh

[ramesh@dev-db ~]# cd mail


-bash: cd: mail: No such file or directory


[Note: This is looking for mail directory under current directory]


[ramesh@dev-db ~]# export CDPATH=/etc [ramesh@dev-db ~]# cd mail

/etc/mail


[Note: This is looking for mail under /etc and not under current directory]


[ramesh@dev-db /etc/mail]# pwd /etc/mail


To make this change permanent, add export CDPATH=/etc to your ~/.bash_profile


Similar to the PATH variable, you can add more than one directory entry in the CDPATH variable, separating them with : , as shown below.


export CDPATH=.:~:/etc:/var


This hack can be very helpful under the following situations:


  1. Oracle DBAs frequently working under $ORACLE_HOME, can set the CDPATH variable to the oracle home
  1. Unix sysadmins frequently working under /etc, can set the CDPATH variable to /etc
  1. Developers frequently working under project directory /home/projects, can set the CDPATH variable to /home/projects
  1. End-users frequently accessing the subdirectories under their home directory, can set the CDPATH variable to ~ (home directory)

Hack 2. Use cd alias to navigate up the directory effectively[edit]

When you are navigating up a very long directory structure, you may be using cd ..\..\ with multiple ..\’s depending on how many directories you want to go up as shown below.


  1. mkdir -p /tmp/very/long/directory/structure/that/is/too/deep
  2. cd /tmp/very/long/directory/structure/that/is/too/deep
  1. pwd /tmp/very/long/directory/structure/that/is/too/deep
  1. cd ../../../../


# pwd /tmp/very/long/directory/structure


Instead of executing cd ../../../.. to navigate four levels up, use one of the following three alias methods:


Method 1: Navigate up the directory using “..n”


In the example below, ..4 is used to go up 4 directory level, ..3 to go up 3 directory level, ..2 to go up 2 directory level. Add the following alias to your ~/.bash_profile and re-login.


alias ..="cd .." alias ..2="cd ../.."

alias ..3="cd ../../.." alias ..4="cd ../../../.." alias ..5="cd ../../../../.."


# cd /tmp/very/long/directory/structure/that/is/too/deep


# ..4


[Note: use ..4 to go up 4 directory level]


# pwd /tmp/very/long/directory/structure/


Method 2: Navigate up the directory using only dots


In the example below, ….. (five dots) is used to go up 4 directory level. Typing 5 dots to go up 4 directory structure is really easy to remember, as when you type the first two dots, you are thinking “going up one directory”, after that every additional dot, is to go one level up. So, use …. (four dots) to go up 3 directory level and .. (two dots) to go up 1 directory level. Add the following alias to your ~/.bash_profile and re-login for the ….. (five dots) to work properly.


alias ..="cd .."


alias ...="cd ../.." alias ....="cd ../../.."

alias .....="cd ../../../.." alias ......="cd ../../../../.."


  1. cd /tmp/very/long/directory/structure/that/is/too/deep
  1. .....

[Note: use ..... (five dots) to go up 4 directory level]


# pwd /tmp/very/long/directory/structure/


Method 3: Navigate up the directory using cd followed by consecutive dots


In the example below, cd….. (cd followed by five dots) is used to go up 4 directory level. Making it 5 dots to go up 4 directory structure is really easy to remember, as when you type the first two dots, you are thinking “going up one directory”, after that every additional dot, is to go one level up. So, use cd…. (cd followed by four dots) to go up 3 directory level and cd… (cd followed by three dots) to go up 2 directory level. Add the following alias to your ~/.bash_profile and re-login for the above cd….. (five dots) to work properly.


alias cd..="cd .." alias cd...="cd ../.."


alias cd....="cd ../../.." alias cd.....="cd ../../../.."


alias cd......="cd ../../../../.."


  1. cd /tmp/very/long/directory/structure/that/is/too/deep
  1. cd.....

[Note: use cd..... to go up 4 directory level]


# pwd /tmp/very/long/directory/structure


Method 5: Navigate up the directory using cd followed by number


In the example below, cd4 (cd followed by number 4) is used to go up 4 directory level.


alias cd1="cd .." alias cd2="cd ../.." alias cd3="cd ../../.."

alias cd4="cd ../../../.." alias cd5="cd ../../../../.."


Hack 3. Perform mkdir and cd using a single command[edit]

Sometimes when you create a new directory, you may cd to the new directory immediately to perform some work as shown below.


  1. mkdir -p /tmp/subdir1/subdir2/subdir3
  1. cd /tmp/subdir1/subdir2/subdir3
  1. pwd

/tmp/subdir1/subdir2/subdir3


Wouldn’t it be nice to combine both mkdir and cd in a single command? Add the following to the .bash_profile and re-login.


$ vi .bash_profile


function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }


Now, perform both mkdir and cd at the same time using a single command as shown below:


# mkdircd /tmp/subdir1/subdir2/subdir3


[Note: This creates the directory and cd to it automatically]

# pwd /tmp/subdir1/subdir2/subdir3


Hack 4. Use “cd -” to toggle between the last two directories[edit]

You can toggle between the last two current directories using cd - as shown below.


  1. cd /tmp/very/long/directory/structure/that/is/too/deep
  1. cd /tmp/subdir1/subdir2/subdir3
  1. cd -
  1. pwd /tmp/very/long/directory/structure/that/is/too/deep
  1. cd -
  1. pwd

/tmp/subdir1/subdir2/subdir3


# cd -


# pwd


/tmp/very/long/directory/structure/that/is/too/deep


Hack 5. Use dirs, pushd and popd to manipulate directory stack[edit]

You can use directory stack to push directories into it and later pop directory from the stack. Following three commands are used in this example.


o dirs: Display the directory stack


o pushd: Push directory into the stack


o popd: Pop directory from the stack and cd to it


Dirs will always print the current directory followed by the content of the stack. Even when the directory stack is empty, dirs command will still print only the current directory as shown below.


# popd


-bash: popd: directory stack empty


# dirs


~


# pwd /home/ramesh


How to use pushd and popd? Let us first create some temporary directories and push them to the directory stack as shown below.


  1. mkdir /tmp/dir1
  1. mkdir /tmp/dir2
  1. mkdir /tmp/dir3
  1. mkdir /tmp/dir4
  1. cd /tmp/dir1
  1. pushd .
  1. cd /tmp/dir2
  1. pushd .
  1. cd /tmp/dir3
  1. pushd .
  1. cd /tmp/dir4
  1. pushd .



# dirs


/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1


[Note: The first directory (/tmp/dir4) of the dir command output is always the current directory and not the content from the stack.]


At this stage, the directory stack contains the following directories:


/tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1


The last directory that was pushed to the stack will be at the top. When you perform popd, it will cd to the top directory entry in the stack and remove it from the stack. As shown above, the last directory that was pushed into the stack is /tmp/dir4. So, when we do a popd, it will cd to the /tmp/dir4 and remove it from the directory stack as shown below.


# popd


# pwd /tmp/dir4

[Note: After the above popd, directory Stack Contains: /tmp/dir3

/tmp/dir2 /tmp/dir1]


# popd


# pwd /tmp/dir3


[Note: After the above popd, directory Stack Contains: /tmp/dir2

/tmp/dir1]


# popd


# pwd /tmp/dir2


[Note: After the above popd, directory Stack Contains: /tmp/dir1]


# popd


# pwd /tmp/dir1

[Note: After the above popd, directory Stack is empty!]


# popd


-bash: popd: directory stack empty


Hack 6. Use “shopt -s cdspell” to automatically correct mistyped directory names on cd[edit]

Use shopt -s cdspell to correct the typos in the cd command automatically as shown below. If you are not good at typing and make lot of mistakes, this will be very helpful.


# cd /etc/mall


-bash: cd: /etc/mall: No such file or directory


  1. shopt -s cdspell
  1. cd /etc/mall
  1. pwd

/etc/mail


[Note: By mistake, when I typed mall instead of mail, cd corrected it automatically]


Chapter 2: Date Manipulation[edit]

Hack 7. Set System Date and Time[edit]

To change the system date use:


# date {mmddhhmiyyyy.ss}


o mm – Month o dd – Date


o hh – 24 hour format o mi – Minutes


o yyyy – Year o ss – seconds


For example, to set system date to Jan 31st 2008, 10:19 p.m, 53 seconds


# date 013122192009.53


You can also change system date using set argument as shown below.


  1. date 013122192009.53
  1. date +%Y%m%d -s "20090131"
  1. date -s "01/31/2009 22:19:53"
  1. date -s "31 JAN 2009 22:19:53"
  1. date set="31 JAN 2009 22:19:53"

To set the time only:


# date +%T -s "22:19:53"


# date +%T%p -s "10:19:53PM"


Hack 8. Set Hardware Date and Time[edit]

Before setting the hardware date and time, make sure the OS date and time is set appropriately as shown in the hack#7.


Set the hardware date and time based on the system date as shown below:


  1. hwclock –systohc
  1. hwclock --systohc –utc

Use hwclock without any parameter, to view the current hardware date and time:


# hwclock


Check the clock file to verify whether the system is set for UTC:


# cat /etc/sysconfig/clock


ZONE="America/Los_Angeles"


UTC=false


ARC=false


Hack 9. Display Current Date and Time in a Specific[edit]

Format


Following are different ways of displaying the current date and time in various formats:

$ date


Thu Jan 1 08:19:23 PST 2009


$ date --date="now"


Thu Jan 1 08:20:05 PST 2009


$ date --date="today"


Thu Jan 1 08:20:12 PST 2009


$ date --date='1970-01-01 00:00:01 UTC +5 hours' +%s 18001


$ date '+Current Date: %m/%d/%y%nCurrent Time:%H:%M:%S' Current Date: 01/01/09


Current Time:08:21:41


$ date +"%d-%m-%Y" 01-01-2009


$ date +"%d/%m/%Y" 01/01/2009


$ date +"%A,%B %d %Y" Thursday,January 01 2009


Following are the different format options you can pass to the date command:


o %D date (mm/dd/yy)
o %d day of month (01..31)
o %m month (01..12)
o %y last two digits of year (00..99)
o %a locale’s abbreviated weekday name (Sun..Sat)
  1. %A locale’s full weekday name, variable length (Sunday..Saturday)

o %b locale’s abbreviated month name (Jan..Dec)


  1. %B locale’s full month name, variable length (January..December)


o %H hour (00..23)
o %I hour (01..12)
o %Y year (1970…)

Hack 10. Display Past Date and Time[edit]

Following are various ways to display a past date and time:


$ date --date='3 seconds ago' Thu Jan 1 08:27:00 PST 2009


$ date --date="1 day ago" Wed Dec 31 08:27:13 PST 2008

$ date --date="1 days ago" Wed Dec 31 08:27:18 PST 2008

$ date --date="1 month ago" Mon Dec 1 08:27:23 PST 2008


$ date --date="1 year ago" Tue Jan 1 08:27:28 PST 2008


$ date --date="yesterday" Wed Dec 31 08:27:34 PST 2008


$ date --date="10 months 2 day ago" Thu Feb 28 08:27:41 PST 2008


Hack 11. Display Future Date and Time[edit]

Following examples shows how to display a future date and time.


$ date


Thu Jan 1 08:30:07 PST 2009


$ date --date='3 seconds' Thu Jan 1 08:30:12 PST 2009


$ date --date='4 hours'


Thu Jan 1 12:30:17 PST 2009


$ date --date='tomorrow' Fri Jan 2 08:30:25 PST 2009


$ date --date="1 day"


Fri Jan 2 08:30:31 PST 2009


$ date --date="1 days"


Fri Jan 2 08:30:38 PST 2009


$ date --date="2 days"


Sat Jan 3 08:30:43 PST 2009


$ date --date='1 month'


Sun Feb 1 08:30:48 PST 2009


$ date --date='1 week'


Thu Jan 8 08:30:53 PST 2009


$ date --date="2 months" Sun Mar 1 08:30:58 PST 2009


$ date --date="2 years"


Sat Jan 1 08:31:03 PST 2011


$ date --date="next day" Fri Jan 2 08:31:10 PST 2009


$ date --date="-1 days ago" Fri Jan 2 08:31:15 PST 2009


$ date --date="this Wednesday" Wed Jan 7 00:00:00 PST 2009

Chapter 3: SSH Client Commands[edit]

Hack 12. Identify SSH Client Version[edit]

Sometimes it may be necessary to identify the SSH client that you are currently running and it’s corresponding version number. Use ssh –V to identify the version number. Please note that Linux comes with OpenSSH.


The following example indicates that this particular system is using OpenSSH:


$ ssh -V


OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003


The following example indicates that this particular system is using SSH2:


$ ssh -V


ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu


Hack 13. Login to Remote Host using SSH[edit]

The First time when you login to a remotehost from a localhost, it will display the host key not found message and you can give “yes” to continue. The host key of the remote host will be added under .ssh2/hostkeys directory of your home directory, as shown below.


localhost$ ssh -l jsmith remotehost.example.com


Host key not found from database. Key fingerprint:


xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum

You can get a public key’s fingerprint by running % ssh-keygen -F publickey.pub


on the keyfile.


Are you sure you want to continue connecting (yes/no)? Yes


Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:06:50 -0700


jsmith@remotehost.example.com password:


remotehost.example.com$


The Second time when you login to the remote host from the localhost, it will prompt only for the password as the remote host key is already added to the known hosts list of the ssh client.


localhost$ ssh -l jsmith remotehost.example.com jsmith@remotehost.example.com password:


remotehost.example.com$


For some reason, if the host key of the remote host is changed after you logged in for the first time, you may get a warning message as shown below. This could be because of various reasons such as:


o Sysadmin upgraded/reinstalled the SSH server on the remote host


o Someone is doing malicious activity etc.,


The best possible action to take before saying “yes” to the message below, is to call your sysadmin and identify why you got the host key changed message and verify whether it is the correct host key or not.


localhost$ ssh -l jsmith remotehost.example.com


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: HOST IDENTIFICATION HAS CHANGED!
@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-



middle attack)!


It is also possible that the host key has just been changed. Please contact your system administrator.


Add correct host key to “/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pu b”


to get rid of this message. Received server key’s fingerprint:


xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum


You can get a public key’s fingerprint by running % ssh-keygen -F publickey.pub


on the keyfile.


Agent forwarding is disabled to avoid attacks by corrupted servers.


Are you sure you want to continue connecting (yes/no)? yes


Do you want to change the host key on disk (yes/no)? yes


Agent forwarding re-enabled. Host key saved to


/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:17:31 -0700


jsmith @remotehost.example.com’s password:


remotehost$


Hack 14. Debug SSH Client Session


Sometimes it is necessary to view debug messages to troubleshoot any SSH connection issues. pass -v (lowercase v) option to the ssh as shown below to view the ssh debug messages.


Example without SSH client debug message:


localhost$ ssh -l jsmith remotehost.example.com


warning: Connecting to remotehost.example.com failed: No address associated to the name

Example with SSH client debug message:


locaclhost$ ssh -v -l jsmith remotehost.example.com


debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext: Metaconfig parsing stopped at line 3.


debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose: Setting variable ‘VerboseMode’ to ‘FALSE’.


debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.


debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for ‘/home/jsmith/.ssh2/ssh2_config’)


debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)

warning: Connecting to remotehost.example.com failed: No address associated to the name


Hack 15. Toggle SSH Session using SSH Escape Character[edit]

When you’ve logged on to the remotehost using ssh from the localhost, you may want to come back to the localhost to perform some activity and go back to remote host again. In this case, you don’t need to disconnect the ssh session to the remote host. Instead, follow the steps below.


  1. Login to remotehost from localhost: localhost$ ssh -l jsmith remotehost



  1. Now you are connected to the remotehost: remotehost$
  1. To come back to the localhost temporarily, type the escape character ~ and Control-Z.

When you type ~ you will not see that immediately on the screen until you press <Control-Z> and press enter. So, on the remotehost in a new line enter the following key strokes for the below to work: ~<Control-Z>


remotehost$ ~^Z


[1]+ Stopped ssh -l jsmith remotehost


localhost$


4. Now you are back to the localhost and the ssh remotehost client session runs as a typical UNIX background job, which you can check as shown below:


localhost$ jobs


[1]+ Stopped ssh -l jsmith remotehost


5. You can go back to the remote host ssh without entering the password again by bringing the background ssh remotehost session job to foreground on the localhost.


localhost$ fg %1


ssh -l jsmith remotehost


remotehost$


Hack 16. SSH Session Statistics using SSH Escape Character[edit]

To get some useful statistics about the current ssh session, do the following. This works only on SSH2 client.


  1. Login to remotehost from localhost. localhost$ ssh -l jsmith remotehost
  1. On the remotehost, type ssh escape character ~ followed by s as shown below. This will display lot of useful statistics about the current SSH connection.

remotehost$ [Note: The ~s is not visible on the command line when you type.]

remote host: remotehost local host: localhost

remote version: SSH-1.99-OpenSSH_3.9p1 local version: SSH-2.0-3.2.9.1 SSH Secure


Shell (non-commercial) compressed bytes in: 1506


uncompressed bytes in: 1622 compressed bytes out: 4997 uncompressed bytes out: 5118 packets in: 15


packets out: 24 rekeys: 0 Algorithms:

Chosen key exchange algorithm: diffie-hellman-group1-sha1

Chosen host key algorithm: ssh-dss


Common host key algorithms: ssh-dss,ssh-rsa Algorithms client to server:

Cipher: aes128-cbc MAC: hmac-sha1 Compression: zlib


Algorithms server to client:


Cipher: aes128-cbc


MAC: hmac-sha1


Compression: zlib


localhost$


Additional SSH Info


On a side note, to setup SSH key based authentication, refer [12]openSSH[13]and [14]SSH2 tutorials.

Chapter 4: Essential Linux Commands[edit]

Hack 17. Grep Command[edit]

grep command is used to search files for a specific text. This is incredibly powerful command with lot of options.


Syntax: grep [options] pattern [files]


How can I find all lines matching a specific keyword on a file?


In this example, grep looks for the text John inside /etc/passwd file and displays all the matching lines.


# grep John /etc/passwd


jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash


Option -v, will display all the lines except the match. In the example below, it displays all the records from /etc/password that doesn't match John.


Note: There are several lines in the /etc/password that doesn’t contain the word John. Only the first line of the output is shown below.


# grep -v John /etc/passwd


jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash


How many lines matched the text pattern in a particular file?


In the example below, it displays the total number of lines that contains the text John in /etc/passwd file.

# grep -c John /etc/passwd 2


You can also get the total number of lines that did not match the specific pattern by passing option -cv.


# grep -cv John /etc/passwd 39


How to search a text by ignoring the case?


Pass the option -i (ignore case), which will ignore the case while searching.


# grep -i john /etc/passwd


jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash


How do I search all subdirectories for a text matching a specific pattern?


Use option -r (recursive) for this purpose. In the example below, it will search for the text "John" by ignoring the case inside all the subdirectories under /home/users.


This will display the output in the format of "filename: line that matching the pattern". You can also pass the option -l, which will display only the name of the file that matches the pattern.


# grep -ri john /home/users


/home/users/subdir1/letter.txt:John, Thanks for your contribution.

/home/users/name_list.txt:John Smith

/home/users/name_list.txt:John Doe


# grep -ril john /root


/home/users/subdir1/letter.txt /home/users/name_list.txt


Hack 18. Find Command[edit]

find is frequently used command to find files in the UNIX filesystem based on numerous conditions. Let us review some practice examples of find command.


Syntax: find [pathnames] [conditions]


How to find files containing a specific word in its name?


The following command looks for all the files under /etc directory with mail in the filename.


# find /etc -name "*mail*"


How to find all the files greater than certain size?


The following command will list all the files in the system greater than 100MB.


# find / -type f -size +100M


How to find files that are not modified in the last x number of days?


The following command will list all the files that were modified more than 60 days ago under the current directory.


# find . -mtime +60


How to find files that are modified in the last x number of days?


The following command will list all the files that were modified in the last two days under the current directory.


# find . –mtime -2


How to delete all the archive files with extension *.tar.gz and greater than 100MB?


Please be careful while executing the following command as you don’t want to delete the files by mistake. The best practice is to execute the same command with ls –l to make sure you know which files will get deleted when you execute the command with rm.


  1. find / -type f -name *.tar.gz -size +100M -exec ls -l {} \;
  1. find / -type f -name *.tar.gz -size +100M -exec rm -f {} \;

How to archive all the files that are not modified in the last x number of days?


The following command finds all the files not modified in the last 60 days under /home/jsmith directory and creates an archive files under /tmp in the format of ddmmyyyy_archive.tar.


# find /home/jsmith -type f -mtime +60 | xargs tar -cvf /tmp/`date '+%d%m%Y'_archive.tar`


On a side note, you can perform lot of file related activities (including finding files) using [15]midnight commander GUI, a powerful text based file manager for Unix.


Hack 19. Suppress Standard Output and Error[edit]

Message


Sometime while debugging a shell script, you may not want to see either the standard output or standard error message. Use /dev/null as shown below for suppressing the output.


Suppress standard output using > /dev/null


This will be very helpful when you are debugging shell scripts, where you don’t want to display the echo statement and interested in only looking at the error messages.


  1. cat file.txt > /dev/null
  1. ./shell-script.sh > /dev/null

Suppress standard error using 2> /dev/null


This is also helpful when you are interested in viewing only the standard output and don’t want to view the error messages.


  1. cat invalid-file-name.txt 2> /dev/null
  1. ./shell-script.sh 2> /dev/null

Hack 20. Join Command[edit]

Join command combines lines from two files based on a common field.


In the example below, we have two files – employee.txt and salary.txt. Both have employee-id as common field. So, we can use join command to combine

the data from these two files using employee-id as shown below.


$ cat employee.txt


100 Jason Smith


200 John Doe


300 Sanjay Gupta


400 Ashok Sharma


$ cat bonus.txt


100 $5,000


200 $500


300 $3,000


400 $1,250


$ join employee.txt bonus.txt


100 Jason Smith $5,000


200 John Doe $500


300 Sanjay Gupta $3,000


400 Ashok Sharma $1,250


Hack 21. Change the Case[edit]

Convert a file to all upper-case


$ cat employee.txt


100 Jason Smith


200 John Doe


300 Sanjay Gupta


400 Ashok Sharma


$ tr a-z A-Z < employee.txt


100 JASON SMITH


200 JOHN DOE


300 SANJAY GUPTA


400 ASHOK SHARMA


Convert a file to all lower-case


$ cat department.txt


100 FINANCE


200 MARKETING


300 PRODUCT DEVELOPMENT


400 SALES


$ tr A-Z a-z < department.txt


100 finance


200 marketing


300 product development


400 sales


Hack 22. Xargs Command[edit]

xargs is a very powerful command that takes output of a command and pass it as argument of another command. Following are some practical examples on how to use xargs effectively.


1. When you are trying to delete too many files using rm, you may get error message: /bin/rm Argument list too long – Linux. Use xargs to avoid this problem.


find ~ -name ‘*.log’ -print0 | xargs -0 rm -f


2. Get a list of all the *.conf file under /etc/. There are different ways to get the same result. Following example is only to demonstrate the use of xargs. The output of the find command in this example is passed to the ls –l one by one using xargs.


    1. find /etc -name "*.conf" | xargs ls –l
  1. If you have a file with list of URLs that you would like to download, you can use xargs as shown below.
    1. cat url-list.txt | xargs wget –c
  1. Find out all the jpg images and archive it.
    1. find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
  1. Copy all the images to an external hard-drive.
    1. ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory

Hack 23. Sort Command[edit]

Sort command sorts the lines of a text file. Following are several practical examples on how to use the sort command based on the following sample text file that has employee information in the format:


employee_name:employee_id:department_name.


$ cat names.txt


Emma Thomas:100:Marketing


Alex Jason:200:Sales


Madison Randy:300:Product Development


Sanjay Gupta:400:Support


Nisha Singh:500:Sales

Sort a text file in ascending order


$ sort names.txt


Alex Jason:200:Sales


Emma Thomas:100:Marketing


Madison Randy:300:Product Development


Nisha Singh:500:Sales


Sanjay Gupta:400:Support


Sort a text file in descending order


$ sort -r names.txt


Sanjay Gupta:400:Support


Nisha Singh:500:Sales


Madison Randy:300:Product Development


Emma Thomas:100:Marketing


Alex Jason:200:Sales


Sort a colon delimited text file on 2nd field (employee_id)


$ sort -t: -k 2 names.txt


Emma Thomas:100:Marketing


Alex Jason:200:Sales


Madison Randy:300:Product Development


Sanjay Gupta:400:Support


Nisha Singh:500:Sales


Sort a tab delimited text file on 3rd field (department_name) and suppress duplicates


$ sort -t: -u -k 3 names.txt


Emma Thomas:100:Marketing


Madison Randy:300:Product Development


Alex Jason:200:Sales


Sanjay Gupta:400:Support


Sort the passwd file by the 3rd field (numeric userid)


$ sort -t: -k 3n /etc/passwd | more


root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


Sort /etc/hosts file by ip-addres


$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts


127.0.0.1 localhost.localdomain localhost 192.168.100.101 dev-db.thegeekstuff.com dev-db 192.168.100.102 prod-db.thegeekstuff.com prod-db 192.168.101.20 dev-web.thegeekstuff.com dev-web 192.168.101.21 prod-web.thegeekstuff.com prod-web


Combine sort with other commands


o ps –ef | sort : Sort the output of process list


  1. ls -al | sort +4n : List the files in the ascending order of the file-size. i.e sorted by 5th filed and displaying smallest files first.
  1. ls -al | sort +4nr : List the files in the descending order of the file-size. i.e sorted by 5th filed and displaying largest files first.



Hack 24. Uniq Command[edit]

Uniq command is mostly used in combination with sort command, as uniq removes duplicates only from a sorted file. i.e In order for uniq to work, all the duplicate entries should be in the adjacent lines. Following are some common examples.


1. When you have an employee file with duplicate entries, you can do the following to remove duplicates.


$ sort namesd.txt | uniq


$ sort –u namesd.txt


2. If you want to know how many lines are duplicates, do the following. The first field in the following examples indicates how many duplicates where found for that particular line. So, in this example the lines beginning with Alex and Emma were found twice in the namesd.txt file.


$ sort namesd.txt | uniq –c


2 Alex Jason:200:Sales


2 Emma Thomas:100:Marketing


1 Madison Randy:300:Product Development


1 Nisha Singh:500:Sales


1 Sanjay Gupta:400:Support


  1. The following displays only the entries that are duplicates. $ sort namesd.txt | uniq –cd

2 Alex Jason:200:Sales


2 Emma Thomas:100:Marketing


Hack 25. Cut Command[edit]

Cut command can be used to display only specific columns from a text file or other command outputs.


Following are some of the examples.


Display the 1st field (employee name) from a colon delimited file


$ cut -d: -f 1 names.txt


Emma Thomas


Alex Jason


Madison Randy


Sanjay Gupta


Nisha Singh


Display 1st and 3rd field from a colon delimited file


$ cut -d: -f 1,3 names.txt


Emma Thomas:Marketing


Alex Jason:Sales


Madison Randy:Product Development


Sanjay Gupta:Support


Nisha Singh:Sales


Display only the first 8 characters of every line in a file


$ cut -c 1-8 names.txt


Emma Tho


Alex Jas


Madison


Sanjay G


Nisha Si

Misc Cut command examples


  1. cut -d: -f1 /etc/passwd Displays the unix login names for all the users in the system.
  1. free | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2 Displays the total memory available on the system.

Hack 26. Stat Command[edit]

Stat command can be used either to check the status/properties of a single file or the filesystem.


Display statistics of a file or directory.


$ stat /etc/my.cnf
File:
`/etc/my.cnf'
Size:
346 Blocks: 16 IO Block: 4096 regular file
Device:
801h/2049d Inode: 279856 Links: 1
Access:
(0644/-rw-r--r--) Uid: (
0/
root)
Gid:
(
0/
root)
Access:
2009-01-01 02:58:30.000000000 -0800
Modify:
2006-06-01 20:42:27.000000000 -0700
Change:
2007-02-02 14:17:27.000000000 -0800
$ stat /home/ramesh
File:
`/home/ramesh'
Size:
4096 Blocks: 8 IO Block:
4096
directory
Device:
803h/2051d Inode: 5521409 Links: 7
Access:
(0755/drwxr-xr-x) Uid: ( 401/ramesh)
Gid: (
401/ramesh)

Access: 2009-01-01 12:17:42.000000000 -0800


Modify: 2009-01-01 12:07:33.000000000 -0800


Change: 2009-01-09 12:07:33.000000000 -080043

Linux 101 Hacks www.thegeekstuff.com
Display the status of the filesystem using option –f
$ stat -f /
File:
"/"
ID:
0 Namelen: 255 Type: ext2/ext3
Blocks:
Total: 2579457 Free: 2008027 Available:
1876998
Size: 4096
Inodes:
Total: 1310720 Free: 1215892

Hack 27. Diff Command[edit]

diff command compares two different files and reports the difference. The output is very cryptic and not straight forward to read.


Syntax: diff [options] file1 file2


What was modified in my new file when compare to my old file?


The option -w in the diff command will ignore the white space while performing the comparison.


In the following diff output:


  1. The lines above ---, indicates the changes happened in first file in the diff command (i.e name_list.txt).
    1. The lines below ---, indicates the changes happened to the second file in the diff command (i.e name_list_new.txt). The lines that belong to the first file starts with < and the lines of second file starts with >.
  1. diff -w name_list.txt name_list_new.txt



2c2,3


< John Doe


---


  1. John M Doe
  1. Jason Bourne

Hack 28. Display total connect time of users[edit]

Ac command will display the statistics about the user’s connect time.


Connect time for the current logged in user


With the option –d, it will break down the output for the individual days. In this example, I’ve been logged in to the system for more than 6 hours today. On Dec 1st, I was logged in for about 1 hour.


$ ac –d
Dec 1 total
1.08
Dec 2 total
0.99
Dec 3 total
3.39
Dec 4 total
4.50
Today total
6.10

Connect time for all the users


To display connect time for all the users use –p as shown below. Please note that this indicates the cumulative connect time for the individual users.


$ ac -p


john 3.64
madison
0.06
sanjay 88.17
nisha 105.92


ramesh111.42


total 309.21


Connect time for a specific user


To get a connect time report for a specific user, execute the following:


$ ac -d sanjay
Jul 2 total
12.85
Aug 25 total
5.05
Sep 3 total
1.03
Sep 4 total
5.37
Dec 24 total
8.15
Dec 29 total
1.42
Today total
2.95


Chapter 5: PS1, PS2, PS3, PS4 and[edit]

PROMPT_COMMAND


Hack 29. PS1 - Default Interaction Prompt[edit]

The default interactive prompt on your Linux can be modified as shown below to something useful and informative. In the following example, the default PS1 was “\s-\v\$”, which displays the shell name and the version number. Let us change this default behavior to display the username, hostname and current working directory name as shown below.


-bash-3.2$ export PS1="\u@\h \w> "


ramesh@dev-db ~> cd /etc/mail


ramesh@dev-db /etc/mail>


[Note: Prompt changed to "username@hostname current-dir>" format]


Following PS1 codes are used in this example:


o \u – Username


o \h – Hostname


  1. \w - Full pathname of current directory. Please note that when you are in the home directory, this will display only ~ as shown above

Note that there is a space at the end in the value of PS1. Personally, I prefer a space at the end of the prompt for better readability.


Make this setting permanent by adding export PS1=”\u@\h \w> ” to either


.bash_profile (or) .bashrc as shown below.

ramesh@dev-db ~> vi ~/.bash_profile


ramesh@dev-db ~> vi ~/.bashrc


[Note: Add export PS1="\u@\h \w> " to one of the above files]


Refer to the next chapter for several practical examples of PS1 usage in detail.


Hack 30. PS2 - Continuation Interactive Prompt[edit]

A very long command can be broken down to multiple lines by giving \ at the end of the line. The default interactive prompt for a multi-line command is “> “. Let us change this default behavior to display “continue->” by using PS2 environment variable as shown below.


ramesh@dev-db ~> myisamchk --silent --force --fast -- update-state \


  1. --key_buffer_size=512M --sort_buffer_size=512M \
  1. --read_buffer_size=4M --write_buffer_size=4M \
  1. /var/lib/mysql/bugs/*.MYI

[Note: This uses the default ">" for continuation prompt]


ramesh@dev-db ~> export PS2="continue-> "


ramesh@dev-db ~> myisamchk --silent --force --fast -- update-state \


continue-> --key_buffer_size=512M -- sort_buffer_size=512M \


continue-> --read_buffer_size=4M --write_buffer_size=4M


\


continue-> /var/lib/mysql/bugs/*.MYI


[Note: This uses the modified "continue-> " for


continuation prompt]


I found it very helpful and easy to read, when I break my long commands into multiple lines using \. I have also seen others who don’t like to break-up long commands.


Hack 31. PS3 - Prompt used by “select” inside shell script[edit]

You can define a custom prompt for the select loop inside a shell script, using the PS3 environment variable, as explained below.


Shell script and output WITHOUT PS3:


ramesh@dev-db ~> cat ps3.sh select i in mon tue wed exit do


case $i in


mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;;


esac done


ramesh@dev-db ~> ./ps3.sh 1) mon

2) tue


3) wed


4) exit


#? 1


Monday


#? 4


[Note: This displays the default "#?" for select command prompt]


Shell script and output WITH PS3:


ramesh@dev-db ~> cat ps3.sh PS3="Select a day (1-4): " select i in mon tue wed exit do


case $i in


mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;;


esac done


ramesh@dev-db ~> ./ps3.sh 1) mon

2) tue


3) wed


4) exit


Select a day (1-4): 1


Monday


Select a day (1-4): 4


[Note: This displays the modified "Select a day (1-4):" for select command prompt]


Hack 32. PS4 - Used by “set -x” to prefix tracing output[edit]

The PS4 shell variable defines the prompt that gets displayed, when you execute a shell script in debug mode as shown below.


Shell script and output WITHOUT PS4:

ramesh@dev-db ~> cat ps4.sh


set -x


echo "PS4 demo script" ls -l /etc/ | wc -l du -sh ~


ramesh@dev-db ~> ./ps4.sh


++ echo 'PS4 demo script' PS4 demo script

  1. ls -l /etc/
  1. wc -l

243


++ du -sh /home/ramesh 48K /home/ramesh


[Note: This displays the default "++" while tracing the output using set -x]


Shell script and output WITH PS4:


The PS4 defined below in the ps4.sh has the following two codes: o $0 - indicates the name of script


o $LINENO - displays the current line number within the script


ramesh@dev-db ~> cat ps4.sh


export PS4='$0.$LINENO+ ' set -x


echo "PS4 demo script" ls -l /etc/ | wc -l du -sh ~


ramesh@dev-db ~> ./ps4.sh


../ps4.sh.3+ echo 'PS4 demo script' PS4 demo script

../ps4.sh.4+ ls -l /etc/


../ps4.sh.4+ wc -l


243


../ps4.sh.5+ du -sh /home/ramesh


48K/home/ramesh


[Note: This displays the modified "{script-name}.{line-number}+" while tracing the output using set -x]


Hack 33. PROMPT_COMMAND[edit]

Bash shell executes the content of the PROMPT_COMMAND just before displaying the PS1 variable.


ramesh@dev-db ~> export PROMPT_COMMAND="date +%k:%m:%S"


22:08:42 ramesh@dev-db ~>


[Note: This displays the PROMPT_COMMAND and PS1 output on different lines]


If you want to display the value of PROMPT_COMMAND in the same line as the PS1, use the echo -n as shown below.


ramesh@dev-db ~> export PROMPT_COMMAND="echo -n [$(date


+%k:%m:%S)]"


[22:08:51]ramesh@dev-db ~>


[Note: This displays the PROMPT_COMMAND and PS1 output on the same line]

Chapter 6: Colorful and Functional Shell[edit]

Prompt Using PS1


Hack 34. Display username, hostname and basename of directory in the prompt[edit]

The PS1 in this example displays following three information in the prompt:


o \u – Username o \h – Hostname


o \W – Base name of the current working directory


-bash-3.2$ export PS1="\u@\h \W> "


ramesh@dev-db ~> cd /etc/mail


ramesh@dev-db mail>


Hack 35. Display current time in the prompt[edit]

In the PS1 environment variable, you can directly execute any Linux command, by specifying in the format $(linux_command). In the following example, the command $(date) is executed to display the current time inside the prompt.


ramesh@dev-db ~> export PS1="\u@\h [\$(date


+%k:%m:%S)]> "


ramesh@dev-db [11:09:56]>


You can also use \t to display the current time in the hh:mm:ss format as shown below:


ramesh@dev-db ~> export PS1="\u@\h [\t]> "


ramesh@dev-db [12:42:55]>


You can also use \@ to display the current time in 12-hour am/pm format as shown below:


ramesh@dev-db ~> export PS1="[\@] \u@\h> "


[04:12 PM] ramesh@dev-db>


Hack 36. Display output of any command in the prompt[edit]

You can display output of any Linux command in the prompt. The following example displays three items separated by | (pipe) in the command prompt:


o \!: The history number of the command


o \h: hostname


  1. $kernel_version: The output of the uname -r command from $kernel_version variable

o \$?: Status of the last command


ramesh@dev-db ~> kernel_version=$(uname -r)


ramesh@dev-db ~> export PS1="\!|\h|$kernel_version|\$?> "


473|dev-db|2.6.25-14.fc9.i686|0>


Hack 37. Change foreground color of the prompt[edit]

Display prompt in blue color, along with username, host and current directory information


$ export PS1="\e[0;34m\u@\h \w> \e[m "


[Note: This is for light blue prompt]


$ export PS1="\e[1;34m\u@\h \w> \e[m "


[Note: This is for dark blue prompt]


o \e[ - Indicates the beginning of color prompt


  1. x;ym - Indicates color code. Use the color code values mentioned below.

o \e[m - indicates the end of color prompt


Color Code Table:


Black 0;30


Blue 0;34


Green 0;32


Cyan 0;36


Red 0;31


Purple 0;35


Brown 0;33


[Note: Replace 0 with 1 for dark color]


Make the color change permanent by adding the following lines your ~/.bash_profile or ~/.bashrc


$ vi ~/.bash_profile

STARTCOLOR='\e[0;34m'; ENDCOLOR="\e[0m"

export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"


Hack 38. Change background color of the prompt[edit]

Change the background color by specifying \e[{code}m in the PS1 prompt as shown below.


$ export PS1="\e[47m\u@\h \w> \e[m "


[Note: This is for Light Gray background]


Combination of background and foreground.


$ export PS1="\e[0;34m\e[47m\u@\h \w> \e[m "


[Note: This is for Light Blue foreground and Light Gray background]


Add the following to your ~/.bash_profile or ~/.bashrc to make the above background and foreground color permanent.


$ vi ~/.bash_profile STARTFGCOLOR='\e[0;34m'; STARTBGCOLOR="\e[47m" ENDCOLOR="\e[0m"


export PS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h \w> $ENDCOLOR"


Play around by using the following background color and choose the one that match your taste:


o \e[40m


o \e[41m


o \e[42m


o \e[43m


o \e[44m


o \e[45m


o \e[46m


o \e[47m


Hack 39. Display multiple colors in the prompt[edit]

You can also display multiple colors in the same prompt. Add the following function to your ~/.bash_profile


function prompt {


local BLUE="\[\033[0;34m\]" local DARK_BLUE="\[\033[1;34m\]” local RED=”\[\033[0;31m\]” local DARK_RED=”\[\033[1;31m\]” local NO_COLOR=”\[\033[0m\]” case $TERM in


xterm*|rxvt*) TITLEBAR=’\[\033]0;\u@\h:\w\007\]’


;;


*) TITLEBAR=”"


;;


esac


PS1=”\u@\h [\t]> ” PS1=”${TITLEBAR}\


$BLUE\u@\h $RED[\t]>$NO_COLOR ” PS2=’continue-> ‘ PS4=’$0.$LINENO+ ‘

}

You can re-login for the changes to take effect or source the .bash_profile as shown below.


$. ./.bash_profile


$ prompt


ramesh@dev-db [13:02:13]>


Hack 40. Change the prompt color using tput[edit]

You can also change color of the PS1 prompt using tput as shown below:


$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]“


tput Color Capabilities:


o tput setab [1-7] - Set a background color using ANSI escape o tput setb [1-7] - Set a background color


o tput setaf [1-7] - Set a foreground color using ANSI escape o tput setf [1-7] - Set a foreground color


tput Text Mode Capabilities:


o tput bold - Set bold mode


o tput dim - turn on half-bright mode o tput smul - begin underline mode o tput rmul - exit underline mode


o tput rev - Turn on reverse mode58

Linux 101 Hacks www.thegeekstuff.com
o tput smso - Enter standout mode (bold on rxvt)
o tput rmso - Exit standout mode
o tput sgr0 - Turn off all attributes
Color Code for tput:
o 0 – Black
o 1 – Red
o 2 – Green
o 3 – Yellow
o 4 – Blue
o 5 – Magenta
o 6 – Cyan
o 7 - White

Hack 41. Create your own prompt using the available codes for PS1 variable[edit]

Use the following codes and create your own personal PS1 Linux prompt that is functional and suites your taste.


o \a an ASCII bell character (07)


  1. \d the date in “Weekday Month Date” format (e.g., “Tue May 26″)
  1. \D{format} - the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required

o \e an ASCII escape character (033)59

Linux 101 Hacks www.thegeekstuff.com
o \h the hostname up to the first part
o \H the hostname
o \j the number of jobs currently managed by the shell
o \l the basename of the shell’s terminal device name
o \n newline
o \r carriage return
  1. \s the name of the shell, the basename of $0 (the portion following the final slash)

o \t the current time in 24-hour HH:MM:SS format o \T the current time in 12-hour HH:MM:SS format o \@ the current time in 12-hour am/pm format o \A the current time in 24-hour HH:MM format


o \u the username of the current user o \v the version of bash (e.g., 2.00)


o \V the release of bash, version + patch level (e.g., 2.00.0)


  1. \w the current working directory, with $HOME abbreviated with a tilde
  1. \W the basename of the current working directory, with $HOME abbreviated with a tilde

o \! the history number of this command


o \# the command number of this command


o \$ if the effective UID is 0, a #, otherwise a $


o \nnn the character corresponding to the octal number nnn o \\ a backslash


  1. \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt



o \] end a sequence of non-printing character


Hack 42. Use bash shell function inside PS1 variable[edit]

You can also invoke a bash shell function in the PS1 as shown below.


ramesh@dev-db ~> function httpdcount {


  1. ps aux | grep httpd | grep -v grep | wc -l
  1. }

ramesh@dev-db ~> export PS1="\u@\h [`httpdcount`]> "


ramesh@dev-db [12]>


[Note: This displays the total number of running httpd processes]


You can add the following line to your ~/.bash_profile or ~/.bashrc to make this change permanent:


$ vi .bash_profile function httpdcount {

ps aux | grep httpd | grep -v grep | wc -l


}


export PS1='\u@\h [`httpdcount`]> '


Hack 43. Use shell script inside PS1 variable[edit]

You can also invoke a shell script inside the PS1 variable. In the example below, the ~/bin/totalfilesize.sh, which calculates the total filesize of the current directory, is invoked inside the PS1 variable.


ramesh@dev-db ~> cat ~/bin/totalfilesize.sh


for filesize in $(ls -l . | grep "^-" | awk '{print $5}')


do


let totalsize=$totalsize+$filesize done


echo -n "$totalsize"


ramesh@dev-db ~> export PATH=$PATH:~/bin


ramesh@dev-db ~> export PS1="\u@\h


[\$(totalfilesize.sh) bytes]> "


ramesh@dev-db [534 bytes]> cd /etc/mail


ramesh@dev-db [167997 bytes]>


[Note: This executes the totalfilesize.sh to display the total file size of the current directory in the PS1 prompt]

Chapter 7: Archive and Compression[edit]

Hack 44. Zip command basics[edit]

How to zip multiple files?


syntax: zip {.zip file-name} {file-names}


  1. zip var-log-files.zip /var/log/* adding: var/log/acpid (deflated 81%)

adding: var/log/anaconda.log (deflated 79%) adding: var/log/anaconda.syslog (deflated 73%) adding: var/log/anaconda.xlog (deflated 82%) adding: var/log/audit/ (stored 0%)

adding: var/log/boot.log (stored 0%) adding: var/log/boot.log.1 (deflated 40%) adding: var/log/boot.log.2 (deflated 42%) adding: var/log/boot.log.3 (deflated 40%) adding: var/log/boot.log.4 (deflated 40%)


How to zip a directory and it’s files recursively?


# zip -r var-log-dir.zip /var/log/ updating: var/log/ (stored 0%)

adding: var/log/wtmp (deflated 78%)


adding: var/log/scrollkeeper.log (deflated 94%) adding: var/log/rpmpkgs.3 (deflated 68%) adding: var/log/spooler (stored 0%)


adding: var/log/cron.2 (deflated 90%) adding: var/log/spooler.1 (stored 0%) adding: var/log/spooler.4 (stored 0%) adding: var/log/httpd/ (stored 0%) adding: var/log/rpmpkgs.1 (deflated 68%)


adding: var/log/anaconda.log (deflated 79%) adding: var/log/secure.2 (deflated 93%)

How to unzip a *.zip compressed file?


# unzip var-log.zip Archive: var-log.zip


inflating: var/log/acpid inflating: var/log/anaconda.log inflating: var/log/anaconda.syslog inflating: var/log/anaconda.xlog creating: var/log/audit/


To see a detailed output during unzip pass the –v option as shown below.


# unzip -v var-log.zip
Archive: var-log.zip
Length Method
Size
Ratio
Date
Time
CRC-32
Name
--------
------
------- -----
----
----
------ ---
-
1916
Defl:N
369
81%
02-08-08 14:27
e2ffdc0c
var/log/acpid
13546
Defl:N
2900
79%
02-02-07 14:25
34cc03a1
var/log/anaconda.log
skip..
7680
Defl:N
411
95%
12-30-08 10:55
fe876ee9
var/log/wtmp.1
40981
Defl:N
7395
82%
02-08-08 14:28
6386a95e
var/log/Xorg.0.log
--------
-------
---
---
----
41406991
2809229
93%
56
files

How to list a content of zip file with uncompressing it?


# unzip -l var-log.zip
Archive:
var-log.zip
Length
Date Time Name
--------
----
---- ----
1916
02-08-08
14:27 var/log/acpid

64

Linux 101 Hacks
www.thegeekstuff.com
13546
02-02-07
14:25
var/log/anaconda.log
..skip..
40981
02-08-08
14:28
var/log/Xorg.0.log
40981
02-08-07
14:56
var/log/Xorg.0.log.old
--------
-------
41406991
56 files

Hack 45. Advanced compression using zip command.[edit]

There are 10 levels of compression provided by zip command.


  1. Level 0 is the lowest level, where it just archives the file without any compression.

o Level 1 will perform little compression. But, will be very fast.


o Level 6 is the default level of compression.


  1. Level 9 is the maximum compression. This will be slower when compared to default level. In my opinion, unless you are compressing a huge file, you should always use level 9.

In the example below, I used Level 0, default Level 6, and Level 9 compression on a same directory. See the compressed file size yourself.


  1. zip var-log-files-default.zip /var/log/*
  1. zip -0 var-log-files-0.zip /var/log/*
  1. zip -9 var-log-files-9.zip /var/log/*
  1. ls -ltr


-rw-r--r--
1
root root
2817248
Jan
1
13:05
var-log-files-default.zip
-rw-r--r--
1
root root
41415301
Jan
1
13:05
var-log-files-0.zip
-rw-r--r--
1
root root
2582610
Jan
1
13:06
var-log-files-9.zip



Hack 46. Password Protection of Zip files[edit]

Pass the option –P to the zip command to assign a password to the zip file.


# zip -P mysecurepwd var-log-protected.zip /var/log/*


The above option is good if you are using the command inside a shell-script for background jobs. However, when you are performing the compression interactively on the command-line, you don’t want the password to be visible in the history. So, use the option –e as shown below to assign the password.


# zip -e var-log-protected.zip /var/log/* Enter password:


Verify password:


updating: var/log/acpid (deflated 81%) updating: var/log/anaconda.log (deflated 79%)


When you are uncompressing a password protected file, it will ask for the password as shown below.


# unzip var-log-protected.zip Archive: var-log-protected.zip


[var-log-protected.zip] var/log/acpid password:


Hack 47. Validate a zip archive[edit]

Sometime you may want to validate a zip archive without extracting it. To test the validity of the zip file, pass option –t as shown below.


# unzip -t var-log.zip
Archive:
var-log.zip
testing:
var/log/acpid
OK
testing:
var/log/anaconda.log
OK


testing: var/log/anaconda.syslog OK


skip...


testing: var/log/wtmpOK


testing: var/log/wtmp.1OK


testing: var/log/Xorg.0.logOK


No errors detected in compressed data of var-log.zip.


Hack 48. Tar Command Basics[edit]

tar command (tape archive) is used to convert a group of files into an archive.


Syntax: tar [options] [tar-archive-name] [other-file-names]


How can I create a single backup file of all files and subdirectories under my home directory?


The following command creates a single archive backup file called my_home_directory.tar under /tmp. This archive will contain all the files and subdirectories under /home/jsmith.


o Option c, stands for create an archive.


  1. Option v stands for verbose mode, displays additional information while executing the command.
    1. Option f indicates the archive file name mentioned in the command.
  1. tar cvf /tmp/my_home_directory.tar /home/jsmith


How do I view all the files inside the tar archive?


Option t will display all the files from the tar archive.


# tar tvf /tmp/my_home_directory.tar


How do I extract all the files from a tar archive?


Option x will extract the files from the tar archive as shown below. This will extract the content to the current directory location from where the command is executed.


# tar xvf /tmp/my_home_directory.tar


How do I extract tar.gz files to a specific directory?


# tar xvfz /tmp/my_home_directory.tar.gz –C /home/ramesh


Hack 49. Combine gzip, bzip2 with tar[edit]

How to use gzip with tar?


Add option z to the tar command when dealing with tar.gz compressed file.


  1. tar cvfz /tmp/my_home_directory.tar.gz /home/jsmith
  1. tar xvfz /tmp/my_home_directory.tar.gz
  1. tar tvfz /tmp/my_home_directory.tar.gz

Note: Using gzip is faster when compared to bzip2.


How to use bzip2 with tar?


Add option j to the tar command when dealing with tar.bz2 compressed file.


  1. tar cvfj /tmp/my_home_directory.tar.bz2 /home/jsmith
  1. tar xvfj /tmp/my_home_directory.tar.bz2
  1. tar tvfj /tmp/my_home_directory.tar.bz2

Note: Using bizp2 gives higher level of compression when compared to gzip.


Chapter 8: Command Line History[edit]

When you are using Linux command line frequently, using the history effectively can be a major productivity boost. In fact, once you have mastered the 15 examples that I’ve provided here, you’ll find using command line more enjoyable and fun.


Hack 50. Display TIMESTAMP in history using HISTTIMEFORMAT[edit]

Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timestamp along with the command as shown below.


  1. export HISTTIMEFORMAT=’%F %T ‘
  1. history | more
    1. 2008-08-05 19:02:39 service network restart
    1. 2008-08-05 19:02:39 exit
    2. 2008-08-05 19:02:39 id
    3. 2008-08-05 19:02:39 cat /etc/redhat-release

Note: You can also setup the following alias to view the recent history commands.


alias h1='history 10' alias h2='history 20' alias h3='history 30'


Hack 51. Search the history using Control+R[edit]

I strongly believe that this may be your most frequently used feature of history. When you’ve already executed a very long command, you can simply


search history using a keyword and re-execute the same command without having to type it fully. Press Control+R and type the keyword.


In the following example, I searched for red, which displayed the previous command “cat /etc/redhat-release” in the history that contained the word red.


# [Note: Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt as shown below]


(reverse-i-search)`red‘: cat /etc/redhat-release


[Note: Press enter when you see your command, which will execute the command from the history]


# cat /etc/redhat-release


Fedora release 9 (Sulphur)


Sometimes you want to edit a command from history before executing it. For e.g. you can search for httpd, which will display service httpd stop from the command history, select this command and change the stop to start and re-execute it again as shown below.


# [Note: Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]

(reverse-i-search)`httpd‘: service httpd stop


[Note: Press either left arrow or right arrow key when you see your command, which will display the command for you to edit, before executing it]


# service httpd start

Hack 52. Repeat previous command quickly using 4 different methods[edit]

Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command.


  1. Use the up arrow to view the previous command and press enter to execute it.
  1. Type !! and press enter from the command line
  1. Type !-1 and press enter from the command line.
  1. Press Control+P will display the previous command, press enter to execute it

Hack 53. Execute a specific command from history[edit]

In the following example, If you want to repeat the command #4, execute !4 as shown below.


  1. history | more
    1. service network restart
    2. exit
    3. id
    4. cat /etc/redhat-release
  1. !4

cat /etc/redhat-release Fedora release 9 (Sulphur)


Hack 54. Execute previous command that starts with a specific word[edit]

Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example, typing !ps and enter, executed the previous command starting with ps, which is ‘ps aux | grep yp’.


# !ps
ps aux | grep yp
root
16947
0.0
0.1
36516
1264
?
Sl
13:10
0:00 ypbind
root
17503
0.0
0.0
4124
740
pts/0
S+
19:19
0:00 grep yp

Hack 55. Control the total number of lines in the history using HISTSIZE[edit]

Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 450 command will be stored in the bash history.


# vi ~/.bash_profile


HISTSIZE=450


HISTFILESIZE=450


Hack 56. Change the history file name using HISTFILE[edit]

By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .commandline_warrior file instead of .bash_history file. I’m yet to figure out a practical use for this. I can see this getting used when you want to track commands executed from different terminals using different history file name.


# vi ~/.bash_profile HISTFILE=/root/.commandline_warrior


Hack 57. Eliminate the continuous repeated entry from history using HISTCONTROL[edit]

In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.


  1. pwd
  1. pwd
  1. pwd
  1. history | tail -4
    1. pwd
    2. pwd
    3. pwd
    4. history | tail -4

[Note: There are three pwd commands in history, after executing pwd 3 times as shown above]


  1. export HISTCONTROL=ignoredups
  1. pwd
  1. pwd
  1. pwd
  1. history | tail -3
    1. export HISTCONTROL=ignoredups
    2. pwd
    3. history | tail -4



[Note: There is only one pwd command in the history, even after executing pwd 3 times as shown above]


Hack 58. Erase duplicates across the whole history using HISTCONTROL[edit]

The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.


  1. export HISTCONTROL=erasedups
  1. pwd
  1. service httpd stop
  1. history | tail -3
    1. pwd
    2. service httpd stop
    3. history | tail -3
  1. ls -ltr
  1. service httpd stop
  1. history | tail -6
    1. export HISTCONTROL=erasedups
    2. pwd
    3. history | tail -3
    4. ls –ltr
    5. service httpd stop
    6. history | tail -6

[Note: The previous service httpd stop after pwd got erased]


Hack 59. Force history not to remember a particular command using HISTCONTROL[edit]

When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below. I can see lot of junior sysadmins getting excited about this, as they can hide a command from the history.


It is good to understand how ignorespace works. But, as a best practice, don’t hide purposefully anything from history.


  1. export HISTCONTROL=ignorespace
  1. ls –ltr
  1. pwd
  1. service httpd stop

[Note: There is a space at the beginning of service, to ignore this command from history]


  1. history | tail -3
    1. ls –ltr
    2. pwd
    3. history | tail -3

Hack 60. Clear all the previous history using option -c[edit]

Sometime you may want to clear all the previous history. However you may still want to keep the history moving forward.


# history -c


Hack 61. Substitute words from history commands[edit]

When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.


In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.


  1. ls anaconda-ks.cfg anaconda-ks.cfg
  1. vi !!:$

vi anaconda-ks.cfg


In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).


  1. cp anaconda-ks.cfg anaconda-ks.cfg.bak anaconda-ks.cfg
  2. vi !^

vi anaconda-ks.cfg


Hack 62. Substitute a specific argument for a specific command[edit]

In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.

  1. cp ~/longname.txt /really/a/very/long/path/long-filename.txt
  2. ls -l !cp:2

ls -l /really/a/very/long/path/long-filename.txt


In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.


# ls -l !cp:$


ls -l /really/a/very/long/path/long-filename.txt


Hack 63. Disable the usage of history using HISTSIZE[edit]

If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.


  1. export HISTSIZE=0
  1. history
  1. [Note: History did not display anything]

Hack 64. Ignore specific commands from the history using HISTIGNORE[edit]

Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history.


Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.


  1. export HISTIGNORE=”pwd:ls:ls –ltr:”
  1. pwd
  1. ls
  1. ls -ltr
  1. service httpd stop
  1. history | tail -3
    1. export HISTIGNORE=”pwd:ls:ls -ltr:”
    2. service httpd stop
    3. history

[Note: History did not display pwd and ls]

Chapter 9: System Administration Tasks[edit]

Hack 65. Partition using fdisk[edit]

After you’ve installed brand new disks on your server, you have to use tools like fdisk to partition it accordingly.


Following are the 5 typical actions (commands) that you can execute inside fdisk.


o n – New Partition creation


o d – Delete an existing partition


o p - Print Partition Table


o w – Write the changes to the partition table. i.e save.


o q – Quit the fdisk utility


Create a partition


In the following example, I created a /dev/sda1 primary partition.


# fdisk /dev/sda


Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel. Changes will remain in memory only,


until you decide to write them. After that, of course, the previous content won't be recoverable.


The number of cylinders for this disk is set to 34893. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with:


  1. software that runs at boot time (e.g., old versions of LILO)
  2. booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)


Command (m for help): p


Disk /dev/sda: 287.0 GB, 287005343744 bytes 255 heads, 63 sectors/track, 34893 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes


Device Boot Start EndBlocks Id System


Command (m for help): n


Command action


e extended


p primary partition (1-4)


p


Partition number (1-4): 1


First cylinder (1-34893, default 1): Using default value 1

Last cylinder or +size or +sizeM or +sizeK (1-34893, default 34893):


Using default value 34893


Command (m for help): w


The partition table has been altered!


Calling ioctl() to re-read partition table.


Syncing disks.


Verify that the partition got created successfully


# fdisk /dev/sda


The number of cylinders for this disk is set to 34893. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with:


  1. software that runs at boot time (e.g., old versions of LILO)
  2. booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p


Disk /dev/sda: 287.0 GB, 287005343744 bytes 255 heads, 63 sectors/track, 34893 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes


Device Boot
Start
End
Blocks
Id System
/dev/sda1
1
34893
280277991
83 Linux
Command (m for help): q

Hack 66. Format a partition using mke2fsk[edit]

After partitioning the disks, it is still not ready for usage, as we need to format the disk. At this stage, if you try to view the disk information, it will give the following error message indicating that no valid superblock is present.


# tune2fs -l /dev/sda1


tune2fs 1.35 (28-Feb-2004)


tune2fs: Bad magic number in super-block while trying to open /dev/sda1


Couldn't find valid filesystem superblock.


To format the disk, use mke2fs as shown below.


# mke2fs /dev/sda1


You can also pass the following optional parameter to the mke2fs.



  1. -m 0 : reserved-blocks-percentage – This indicates the percentage of the filesystem blocks reserved for the root user. Default is 5%. In the following example, it is set to 0.
    1. -b 4096 : block-size specified in bytes. Valid values are 1024, 2048 and 4096 bytes per block.
  1. mke2fs -m 0 -b 4096 /dev/sda1

mke2fs 1.35 (28-Feb-2004) Filesystem label=


OS type: Linux


Block size=4096 (log=2) Fragment size=4096 (log=2) 205344 inodes, 70069497 blocks


0 blocks (0.00%) reserved for the super user First data block=0

Maximum filesystem blocks=71303168 2139 block groups

32768 blocks per group, 32768 fragments per group


96 inodes per group


Superblock backups stored on blocks:


32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872


Writing inode tables: done


Writing superblocks and filesystem accounting information: done


This filesystem will be automatically checked every 32 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.


The above command will create an ext2 filesystem. To create an ext3 file system do the following:


  1. mkfs.ext3 /dev/sda1
  1. mke2fs –j /dev/sda1



Hack 67. Mount the partition[edit]

After creating a partition and formatting, you can mount it to a mount point.


First create a directory where the partition should be mounted.


  1. mkdir /home/database

Mount the file system.


  1. mount /dev/sda1 /home/database

To automatically mount the filesystem after the reboot, add the following entry to the /etc/fstab


/dev/sdaa /home/database ext3 defaults 0 2


Hack 68. Fine tune the partition using tune2fs[edit]

Use the tune2fs –l /dev/sda1 to view the filesystem information as shown below.


# tune2fs -l /dev/sda1 tune2fs 1.35 (28-Feb-2004)


Filesystem volume name: /home/database


Last mounted on:<not available>


Filesystem UUID:f1234556-e123-1234-abcd-


bbbbaaaaae11


Filesystem magic number: 0xEF44


Filesystem revision #:1 (dynamic)


Filesystem features:resize_inode filetype


sparse_super


Default mount options:(none)


Filesystem state:not clean84

Linux 101 Hacks
www.thegeekstuff.com
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 1094912
Block count: 140138994
Reserved block count: 0
Free blocks: 16848481
Free inodes: 1014969
First block: 0
Block size: 2048
Fragment size: 2048
Reserved GDT blocks: 512
Blocks per group: 16384
Fragments per group: 16384
Inodes per group: 128
Inode blocks per group: 8
Filesystem created: Tue Jul
1 00:06:03 2008
Last mount time: Thu Aug 21 05:58:25 2008
Last write time: Fri Jan
2 15:40:36 2009
Mount count: 2
Maximum mount count: 20
Last checked: Tue Jul
1 00:06:03 2008
Check interval: 15552000 (6 months)
Next check after: Sat Dec 27 23:06:03 2008
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 128
Default directory hash: tea
Directory Hash Seed: 12345829-1236-4123-9aaa-
ccccc123292b

You can also use the tune2fs to tune the ex2/ext3 filesystem parameter. For example, if you want to change the Filesystem volume name, you can do it as shown below.


  1. tune2fs -l /dev/sda1 | grep volume

Filesystem volume name: /home/database


  1. tune2fs -L database-home /dev/emcpowera1 tune2fs 1.35 (28-Feb-2004)


# tune2fs -l /dev/sda1 | grep volume


Filesystem volume name: database-home


Hack 69. Create a swap file system.[edit]

Create a file for swap usage as shown below.


  1. dd if=/dev/zero of=/home/swap-fs bs=1M count=512

512+0 records in


512+0 records out


  1. ls -l /home/swap-fs

-rw-r--r-- 1 root root 536870912 Jan 2 23:13 /home/swap-fs


Use mkswap to setup a Linux swap area in the /home/swap-fs file that was created above.


# mkswap /home/swap-fs


Setting up swapspace version 1, size = 536866 kB


Once the file is created and has been setup for Linux swap area, it is time to enable the swap using swapon as shown below.


# swapon /home/swap-fs


Add the following line to /etc/fstab and reboot the system for the swap to take into effect.


/home/swap-fs swap swap defaults 0 0


Hack 70. Create a new user[edit]

Add a new user – Basic method


Specify only the user name.


# useradd jsmith


Add a new user with additional Parameter


You can also specify the following parameter to the useradd o -c : Description about the user.


    1. -e : expiry date of the user in mm/dd/yy format
  1. adduser -c "John Smith - Oracle Developer" -e 12/31/09 jsmith

Verify that the user got added successfully.


# grep jsmith /etc/passwd jsmith:x:510:510:John Smith - Oracle Developer:/home/jsmith:/bin/bash


Change the user password.


# passwd jsmith


Changing password for user jsmith. New UNIX password:


BAD PASSWORD: it is based on a dictionary word Retype new UNIX password:

passwd: all authentication tokens updated successfully.

Note: Make sure to follow [16]these best practices[17]to create a strong password for the user.


How to identify the default values used by useradd?


Following are the default values that will be used when an user is created.


# useradd –D


GROUP=100


HOME=/home


INACTIVE=-1


EXPIRE=


SHELL=/bin/bash


SKEL=/etc/skel


Hack 71. Create a new group and assign to an user[edit]

Create a new developer group.


# groupadd developers


Validate that the group was created successfully.


# grep developer /etc/group developers:x:511:


Add an user to an existing group.


You cannot use useradd to modify an existing user, as you’ll get the following error message.

  1. useradd -G developers jsmith useradd: user jsmith exists
  1. usermod -g developers jsmith

Validate the users group was modified successfully.


# grep jsmith /etc/passwd jsmith:x:510:511:Oracle Developer:/home/jsmith:/bin/bash


# id jsmith


uid=510(jsmith) gid=511(developers) groups=511(developers)


# grep jsmith /etc/group jsmith:x:510: developers:x:511:jsmith


Hack 72. Setup SSH passwordless login in OpenSSH[edit]

You can login to a remote Linux server without entering password in 3 simple steps using ssky-keygen and ssh-copy-id as explained in this example.


ssh-keygen creates the public and private keys. ssh-copy-id copies the local-host’s public key to the remote-host’s authorized_keys file. ssh-copy-id also assigns proper permission to the remote-host’s home, ~/.ssh, and ~/.ssh/authorized_keys.


Step 1: Create public and private keys using ssh-key-gen on local-host


jsmith@local-host$ ssh-keygen


Generating public/private rsa key pair.


Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]


Enter passphrase (empty for no passphrase): [Press enter key]


Enter same passphrase again: [Pess enter key] Your identification has been saved in /home/jsmith/.ssh/id_rsa.


Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub. The key fingerprint is:


33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host


Step 2: Copy the public key to remote-host using ssh-copy-id


jsmith@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host


jsmith@remote-host’s password:


Now try logging into the machine, with “ssh ‘remote-host’”, and check in:


.ssh/authorized_keys to make sure we haven’t added extra keys that you weren’t expecting.


Note: ssh-copy-id appends the keys to the remote-host’s


.ssh/authorized_key.


Step 3: Login to remote-host without entering the password


jsmith@local-host$ ssh remote-host


Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2 [Note: SSH did not ask for password.] jsmith@remote-host$ [Note: You are on remote-host here]


Hack 73. Use ssh-copy-id along with ssh-agent[edit]

Using ssh-copy-id along with the ssh-add/ssh-agent


When no value is passed for the option -i and If ~/.ssh/identity.pub is not available, ssh-copy-id will display the following error message.


jsmith@local-host$ ssh-copy-id -i remote-host


/usr/bin/ssh-copy-id: ERROR: No identities found


If you have loaded keys to the ssh-agent using the ssh-add, then ssh-copy-id will get the keys from the ssh-agent to copy to the remote-host. i.e, it copies the keys provided by ssh-add -L command to the remote-host, when you don’t pass option -i to the ssh-copy-id.


jsmith@local-host$ ssh-agent $SHELL


jsmith@local-host$ ssh-add -L The agent has no identities.


jsmith@local-host$ ssh-add


Identity added: /home/jsmith/.ssh/id_rsa (/home/jsmith/.ssh/id_rsa)


jsmith@local-host$ ssh-add -L ssh-rsa


AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79D aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow== /home/jsmith/.ssh/id_rsa


jsmith@local-host$ ssh-copy-id -i remote-host jsmith@remote-host’s password:


Now try logging into the machine, with “ssh ‘remote-host’”, and check in: .ssh/authorized_keys to make sure we haven’t added extra keys that you weren’t expecting. [Note: This has added the key displayed by ssh-add -L]

Three Minor Annoyances of ssh-copy-id


Following are few minor annoyances of the ssh-copy-id.


  1. Default public key: ssh-copy-id uses ~/.ssh/identity.pub as the default public key file (i.e when no value is passed to option -i). Instead, I wish it uses id_dsa.pub, or id_rsa.pub, or identity.pub as default keys. i.e If any one of them exist, it should copy that to the remote-host. If two or three of them exist, it should copy identity.pub as default.
  1. The agent has no identities: When the ssh-agent is running and the ssh-add -L returns “The agent has no identities” (i.e no keys are added to the ssh-agent), the ssh-copy-id will still copy the message “The agent has no identities” to the remote-host’s authorized_keys entry.
  1. Duplicate entry in authorized_keys: I wish ssh-copy-id validates duplicate entry on the remote-host’s authorized_keys. If you execute ssh-copy-id multiple times on the local-host, it will keep appending the same key on the remote-host’s authorized_keys file without checking for duplicates. Even with duplicate entries everything works as expected. But, I would like to have my authorized_keys file clutter free.

Hack 74. Crontab[edit]

Using cron you can execute a shell-script or Linux commands at a specific time and date. For example a sysadmin can schedule a backup job that can run every day.


How to add a job to the cron?


# crontab –e


0 5 * * * /root/bin/backup.sh

This will execute /root/bin/backup.sh at 5 a.m every day.


Description of Cron fields.


Following is the format of the crontab file.


{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}


o minute: Allowed range 0 – 59 o hour: Allowed range 0 – 23


o day-of-month: Allowed range 0 – 31


o month: Allowed range 1 – 12. 1 = January. 12 = December. o Day-of-week: Allowed range 0 – 7. Sunday is either 0 or 7.


Crontab examples


  1. Run at 12:01 a.m. 1 minute after midnight everyday. This is a good time to run backup when the system is not under load.
    1. 0 * * * /root/bin/backup.sh
  1. Run backup every weekday (Mon – Fri) at 11:59 p.m.
    1. 11 * * 1,2,3,4,5 /root/bin/backup.sh

Following will also do the same.


59 11 * * 1-5 /root/bin/backup.sh


  1. Execute the command every 5 minutes.

*/5 * * * * /root/bin/check-status.sh


  1. Execute at 1:10 p.m on 1st of every month
    1. 13 1 * * /root/bin/full-backup.sh
  1. Execute 11 p.m on weekdays.
    1. 23 * * 1-5 /root/bin/incremental-backup.sh

Crontab Options


Following are the available options with crontab:


  1. crontab –e : Edit the crontab file. This will create a crontab, if it doesn’t exist

o crontab –l : Display the crontab file. o crontab -r : Remove the crontab file.


o crontab -ir : This will prompt user before deleting a crontab.


Hack 75. Safe Reboot Of Linux Using Magic SysRq Key[edit]

The magic SysRq key is a key combination in the Linux kernel which allows the user to perform various low level commands regardless of the system’s state.


It is often used to recover from freezes, or to reboot a computer without corrupting the filesystem. The key combination consists of

Alt+SysRq+commandkey. In many systems the SysRq key is the printscreen key.


First, you need to enable the SysRq key, as shown below.


echo "1" > /proc/sys/kernel/sysrq


List of SysRq Command Keys


Following are the command keys available for Alt+SysRq+commandkey. o ‘k’ – Kills all the process running on the current virtual console. o ’s’ – This will attempt to sync all the mounted file system.


  1. ‘b’ - Immediately reboot the system, without unmounting partitions or syncing.

o ‘e’ – Sends SIGTERM to all process except init.


o ‘m’ – Output current memory information to the console. o ‘i’ - Send the SIGKILL signal to all processes except init


  1. ‘r’ - Switch the keyboard from raw mode (the mode used by programs such as X11), to XLATE mode.

o ’s’ – sync all mounted file system.


  1. ‘t’ - Output a list of current tasks and their information to the console.

o ‘u’ - Remount all mounted filesystems in readonly mode. o ‘o’ – Shutdown the system immediately.


o ‘p’ – Print the current registers and flags to the console.


  1. ‘0-9′ - Sets the console log level, controlling which kernel messages will be printed to your console.


o ‘f’ - Will call oom_kill to kill process which takes more memory.


  1. ‘h’ – Used to display the help. But any other keys than the above listed will print help.

We can also do this by echoing the keys to the /proc/sysrq-trigger file. For example, to re-boot a system you can perform the following.


echo "b" > /proc/sysrq-trigger


Perform a Safe reboot of Linux using Magic SysRq Key


To perform a safe reboot of a Linux computer which hangs up, do the following. This will avoid the fsck during the next re-booting. i.e Press Alt+SysRq+letter highlighted below.


o unRaw (take control of keyboard back from X11,


  1. tErminate (send SIGTERM to all processes, allowing them to terminate gracefully),
  1. kIll (send SIGILL to all processes, forcing them to terminate immediately),

o Sync (flush data to disk),


o Unmount (remount all filesystems read-only), o reBoot.

Chapter 10: Apachectl and Httpd[edit]

Examples


After you have installed Apache2, if you want to use apachectl and httpd to it’s maximum potential, you should go beyond using start, stop and restart. The 9 practical examples provided in this chapter will help you to use apachectl and httpd very effectively.


Apachectl acts as SysV init script, taking arguments like start, stop, restart and status. It also acts as front-end to httpd command, by simply passing the command line arguments to httpd. So, all the commands you execute using apachectl, can also be executed directly by calling httpd.


If you don’t have Apache, refer to the tutorials: [18]install apache from source[19]or [20]install LAMP stack using yum.


Hack 76. Pass different httpd.conf filename to apachectl[edit]

Typically you’ll modify the original httpd.conf to try out different Apache directives. If something doesn’t work out, you’ll revert back the changes. Instead of playing around with the original httpd.conf, copy it to a new httpd.conf.debug and use this new httpd.conf.debug file with Apache for testing purpose as shown below using option -f.


  1. apachectl -f conf/httpd.conf.debug
  1. httpd -k start -f conf/httpd.conf.debug

[Note: you can use either apachectl or httpd as shown above]


# ps
-ef | grep http
root
25080
1
0 23:26 00:00:00 /usr/sbin/httpd -f


conf/httpd.conf.debug


apache 25099 25080 0 23:28 00:00:00 /usr/sbin/httpd -f conf/httpd.conf.debug


[Note: ps shows the httpd running with httpd.conf.debug file]


Once you are satisfied with the changes and Apache runs without any problem with httpd.conf.debug, you can copy the changes to httpd.conf and start the Apache normally as shown below.


  1. cp httpd.conf.debug httpd.conf
  1. apachectl stop
  1. apachectl start
  1. ps -ef | grep httpd


root
25114
1
0
23:28 00:00:00 /usr/sbin/httpd
-k start
daemon
25115 25114
0
23:28 00:00:00 /usr/sbin/httpd
-k start

[Note: ps indicates that the httpd is running using the default config file]


Hack 77. Use a temporary DocumentRoot without modifying httpd.conf[edit]

This is very helpful, when you are trying out different layout for your website and don’t want to modify the original files under the default DocumentRoot.


Take a copy of your original DocumentRoot directory (/var/www/html) to a new temporary DocumentRoot directory (/var/www/html_debug). Make all your changes under this temporary DocumentRoot directory (/var/www/html_debug) and start the Apache with this temporary directory as shown below using option -c.


# httpd -k start -c “DocumentRoot /var/www/html_debug/”


If you want to go back to original configuration using the default DocumentRoot (/var/www/html), simply restart the Apache as shown below.


  1. httpd -k stop
  1. apachectl start

Hack 78. Increase the Log Level temporarily[edit]

While you are debugging an issue, you can change the LogLevel of the Apache temporarily, without modifying the LogLevel directive in the httpd.conf as shown below using option -e. In this example, the LogLevel is set to debug.


# httpd -k start -e debug


[Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_basic_module


[Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_digest_module


Possible values you can pass to option -e are:


o debug


o info


o notice


o warn


o error


o crit


o alert


o emerg


Hack 79. Display the modules inside Apache[edit]

Display the modules compiled inside Apache


# httpd -l


Compiled in modules: core.c


prefork.c http_core.c mod_so.c


Display both static and dynamic module loaded by Apache


When you pass option -l, to httpd, it will display only the static modules. Passing option -M, will display both static and shared modules as shown below.


# httpd –M


Loaded Modules:


core_module (static)


mpm_prefork_module (static)


http_module (static)


so_module (static)


auth_basic_module (shared)


auth_digest_module (shared)


authn_file_module (shared)


authn_alias_module (shared)


Syntax OK


Hack 80. Show all accepted directives inside httpd.conf[edit]

This is like an extended help for httpd, which will display all the httpd.conf directives and the places where they are valid. For a specific directive, it tells all the possible values and where it can be used inside the httpd.conf. This can be very helpful, when you want to quickly know about a particular Apache directive.


# httpd –L


HostnameLookups (core.c)


“on” to enable, “off” to disable reverse DNS lookups, or “double” to enable double-reverse DNS lookups Allowed in *.conf anywhere


ServerLimit (prefork.c)


Maximum value of MaxClients for this run of Apache Allowed in *.conf only outside <Directory>, <Files> or <Location>


KeepAlive (http_core.c)


Whether persistent connections should be On or Off Allowed in *.conf only outside <Directory>, <Files> or <Location>


LoadModule (mod_so.c)


a module name and the name of a shared object file to load it from

Allowed in *.conf only outside <Directory>, <Files> or <Location>


Hack 81. Validate the httpd.conf after making changes[edit]

Use option -t to validate whether there are any issues with a specific Apache configuration file. In the example shown below, it displays that there is a


problem at line 148 in the httpd.conf.debug. mod_auth_basicso is missing a . (period) before the so.


# httpd -t -f conf/httpd.conf.debug


httpd: Syntax error on line 148 of /etc/httpd/conf/httpd.conf.debug:


Cannot load /etc/httpd/modules/mod_auth_basicso into server:


/etc/httpd/modules/mod_auth_basicso: cannot open shared object file: No such file or directory


Once you fix the issue, it will display Syntax OK.


# httpd -t -f conf/httpd.conf.debug


Syntax OK


Hack 82. Display the httpd build parameters[edit]

Use option -V (upper-case V), to display Apache version number and all the parameters that are used while building the Apache.


# httpd –V


Server version: Apache/2.2.9 (Unix) Server built: Jul 14 2008 15:36:56 Server’s Module Magic Number: 20051115:15

Server loaded: APR 1.2.12, APR-Util 1.2.12 Compiled using: APR 1.2.12, APR-Util 1.2.12 Architecture: 32-bit

Server MPM: Prefork threaded: no

forked: yes (variable process count) Server compiled with….

-D APACHE_MPM_DIR=”server/mpm/prefork” -D APR_HAS_SENDFILE


-D APR_HAS_MMAP


-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE


-D APR_USE_PTHREAD_SERIALIZE


-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD


-D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=128 -D HTTPD_ROOT=”/etc/httpd”


-D SUEXEC_BIN=”/usr/sbin/suexec”


-D DEFAULT_PIDLOG=”logs/httpd.pid”


-D DEFAULT_SCOREBOARD=”logs/apache_runtime_status” -D DEFAULT_LOCKFILE=”logs/accept.lock”


-D DEFAULT_ERRORLOG=”logs/error_log”


-D AP_TYPES_CONFIG_FILE=”conf/mime.types” -D SERVER_CONFIG_FILE=”conf/httpd.conf”


If you want display only the Apache version number, use the option -v (lower-case v) as shown below.


# httpd -v


Server version: Apache/2.2.9 (Unix)


Server built: Jul 14 2008 15:36:56


Hack 83. Load a specific module only on demand[edit]

Sometimes you may not want to load all the modules in the Apache. For e.g. You may want to load ldap related modules to Apache, only when you are testing LDAP. This can be achieved as shown below.


Modify the httpd.conf and add IfDefine directive called load-ldap (you can name this anything you want).


<IfDefine load-ldap>


LoadModule ldap_module modules/mod_ldap.so LoadModule authnz_ldap_module


modules/mod_authnz_ldap.so </IfDefine>


When you are testing ldap and would like to Load the ldap related modules, pass the load-ldap to Option -D, as shown below:


# httpd -k start -e debug -Dload-ldap -f /etc/httpd/conf/httpd.conf.debug


[Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded module ldap_module


[Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded module authnz_ldap_module

[Note: Pass -Dload-ldap, to load the ldap modules into Apache]


# apachectl start


[Note: Start the Apache normally, if you don't want to load the ldap modules.104Linux 101 Hackswww.thegeekstuff.com


Chapter 11: Bash Scripting[edit]

Hack 84. Execution Sequence of .bash_* files[edit]

What is the sequence in which the following files are executed?


o /etc/profile


o ~/.bash_profile


o ~/.bashrc


o ~/.bash_login


o ~/.profile


o ~/.bash_logout


Execution sequence for interactive login shell


Following pseudo code explains the sequence of execution of these files.


execute /etc/profile


IF ~/.bash_profile exists THEN execute ~/.bash_profile


ELSE


IF ~/.bash_login exist THEN execute ~/.bash_login


ELSE


IF ~/.profile exist THEN execute ~/.profile


END IF END IF

END IF


When you logout of the interactive shell, following is the sequence of execution:


IF ~/.bash_logout exists THEN execute ~/.bash_logout

END IF


Please note that /etc/bashrc is executed by ~/.bashrc as shown below:


# cat ~/.bashrc


if [ -f /etc/bashrc ]; then


. /etc/bashrc Fi


Execution sequence for interactive non-login shell


While launching a non-login interactive shell, following is the sequence of execution:


IF ~/.bashrc exists THEN execute ~/.bashrc


END IF


Note: When a non-interactive shell starts up, it looks for ENV environment variable, and executes the file-name value mentioned in the ENV variable.


Hack 85. How to generate random number in bash shell[edit]

Use the $RANDOM bash built-in function to generate random number between 0 – 32767 as shown below.


$ echo $RANDOM

22543


$ echo $RANDOM 25387


$ echo $RANDOM 647


Hack 86. Debug a shell script[edit]

To debug a shell script use set –xv inside the shell script at the top.


Shell script with no debug command:


$ cat filesize.sh #!/bin/bash

for filesize in $(ls -l . | grep "^-" | awk '{print $5}')


do


let totalsize=$totalsize+$filesize done


echo "Total file size in current directory: $totalsize"


Output of Shell script with no debug command:


$ ./filesize.sh


Total file size in current directory: 652


Shell script with Debug command inside:


Add set –xv inside the shell script now to debug the output as shown below.


$ cat filesize.sh #!/bin/bash

set -xv


for filesize in $(ls -l . | grep "^-" | awk '{print


$5}') do

let totalsize=$totalsize+$filesize done

echo "Total file size in current directory: $totalsize"


Output of Shell script with Debug command inside:


$ ./fs.sh


  1. ls -l .
  1. grep '^-'
  1. awk '{print $5}'
  1. for filesize in '$(ls -l . | grep "^-" | awk '\{print $5}'\)'
  2. let totalsize=+178
  1. for filesize in '$(ls -l . | grep "^-" | awk '\{print $5}'\)'
  2. let totalsize=178+285
  1. for filesize in '$(ls -l . | grep "^-" | awk '\{print $5}'\)'
  2. let totalsize=463+189
  1. echo 'Total file size in current directory: 652' Total file size in current directory: 652

Execute Shell script with debug option:


Instead of giving the set –xv inside the shell script, you can also provide that while executing the shell script as shown below.


$ bash -xv filesize.sh


Hack 87. Quoting[edit]

echo statement without any special character.


$ echo The Geek Stuff


The Geek Stuff


Echo statement with a special character ; . semi-colon is a command terminator in bash. In the following example, “The Geek” works for the echo and “Stuff” is treated as a separate Linux command and gives command not found.


$ echo The Geek; Stuff The Geek


-bash: Stuff: command not found


To avoid this you can add a \ in front of semi-colon, which will remove the special meaning of semi-colon and just print it as shown below.


$ echo The Geek\; Stuff The Geek; Stuff


Single Quote


Use single quote when you want to literally print everything inside the single quote. Even the special variables such as $HOSTNAME will be print as $HOSTNAME instead of printing the name of the Linux host.


$ echo 'Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD'


Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD


Double Quote


Use double quotes when you want to display the real meaning of special variables.


$ echo "Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD"


Hostname=dev-db ; Current User=ramesh ; Message=$ is


USD


Double quotes will remove the special meaning of all characters except the following:


o $ Parameter Substitution.


o ` Backquotes


o \$ Literal Dollar Sign. o \´ Literal Backquote.


o \" Embedded Doublequote. o \\ Embedded Backslashes.


Hack 88. Read data file fields inside a shell script[edit]

This example shows how to read a particular field from a data-file and manipulate it inside a shell-script. For example, let us assume the employees.txt file is in the format of {employee-name}:{employee-id}:{department-name}, with colon delimited file as shown below.


$ cat employees.txt


Emma Thomas:100:Marketing Alex Jason:200:Sales


Madison Randy:300:Product Development Sanjay Gupta:400:Support

Nisha Singh:500:Sales


The following shell script explains how to read specific fields from this employee.txt file.


$ vi read-employees.sh #!/bin/bash


IFS=:


echo "Employee Names:" echo "---------------" while read name empid dept


do


echo "$name is part of $dept department" done < ~/employees.txt


Assign execute privilege to the shell script and execute it.


$ chmod u+x read-employees.sh


$ ./read-employees.sh Employee Names:

---------------


Emma Thomas is part of Marketing department Alex Jason is part of Sales department


Madison Randy is part of Product Development department Sanjay Gupta is part of Support department

Nisha Singh is part of Sales department

Chapter 12: System Monitoring and[edit]

Performance


Hack 89. Free command[edit]

free command displays all the necessary information about system physical (RAM) and swap memory.


Syntax: free [options]


What is the total RAM on my system?


In the example below, the total physical memory on this system is 1GB. The values displayed below are in KB.


# free total used free shared buffers cached
Mem: 1034624 1006696 27928 0 174136 615892
-/+ buffers/cache: 216668
817956
Swap:
2031608
0
2031608

What is the total memory on my system including RAM and Swap?


In the following command:


o option m displays the values in MB


  1. option t displays the “Total” line, which is sum of physical and swap memory values
  1. option o is to hide the buffers/cache line from the above example.
    1. free –mto

112

Linux 101 Hacks
www.thegeekstuff.com
total
used
free
shared
buffers
cached
Mem:
1010
983
27
0
170
601
Swap:
1983
0
1983
Total:
2994
983
2011

Hack 90. Top Command[edit]

top command displays real time information about various performance metrics of the system such as CPU Load, Memory Usage, Processes list etc.


Syntax: top [options]


How to view my current system status including CPU usage?


Execute top without any option from the command line, which will display the output shown below. The top command output will keep displaying the real-time values, until you press “Control + c” or q to exit from the command output.


# top


top - 13:10:13 up 171 days, 20:21, 3 users, load average: 0.01, 0.05, 0.00


Tasks: 194 total, 1 running, 193 sleeping, 0 stopped,
0 zombie
Cpu(s): 0.6% us, 0.7% sy, 0.0% ni, 98.7% id, 0.0% wa, 0.0% hi, 0.0% si
Mem: 1034624k total, 1007420k used, 27204k free, 174540k buffers
Swap: 2031608k total, 0k used, 2031608k free, 615904k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM
TIME+ COMMAND
11912 apache
15
0 31828
13m 3916 S
1
0.2
0:46.35
httpd
19299 oracle
19
0
279m
18m 17m S
1
0.2
0:00.03
oracle
11398 jsmith
16
0
107m
28m 6404 S
0
0.4
0:03.07
perl


How to read the output of the top command shown above?


  1. Line 1 “top”, indicates that the system has been up and running for 171 days.
  1. Line 2 “Tasks”, displays the total number of processes along with a breakdown of running, sleeping, stopped and zombie processes count.
  1. Line 3 “Cpu(s)” displays the current CPU utilization of the system. In this example, CPU is 98.7% idle
  1. Line 4 “Mem” and line 5 “Swap” provides the memory information. This is the same information from the free command.
  1. The rest of the lines display all the active processes on the system, sorted default by CPU usage (%CPU column). i.e the most CPU intensive processes will be displayed on the top by default.

There are several command line options and interactive options available for top commands. Let us review couple of essential options for top command.


How to identify the most memory intensive processes?


While the output of the top command displayed, press F, which will display the following message and show all fields available for sorting, press n (which is for sorting the processes by Memory) and press enter. This will display the processes in the top output sorted by memory usage.


Current Sort Field: K for window 1:Def


Select sort field via field letter, type any other key to return


How to add additional fields (for e.g. CPU Time) to the top output?


While the top command is running, press f, which will display the following message and show all fields available for display, press l, which will add the CPU Time to the display columns in the top output.


Current Fields: AEHIOQTWKNMbcdfgjplrsuvyzX for window 1:Def


Toggle fields via field letter, type any other key to return


How to get the full path name and parameters of the running processes?


While the top command is running, press c, which will display full pathname of running processes as shown below in the command column. i.e Instead of httpd, it displays /usr/local/apache2/bin/httpd.


PID USER PR NI VIRT RES SHR S %CPU %MEM
TIME+ COMMAND
11912 apache
15
0 31828
13m 3916 S
1
0.2 0:46.35
/usr/local/apache2/bin/httpd

How to view the individual CPUs in the top command?


While the top command is running, press 1 (number one), which will display the performance data of the individual CPUs on that machine as shown below.


top - 13:10:13 up 171 days, 20:21, 3 users, load average: 0.01, 0.05, 0.00 Tasks: 194 total, 1 running, 193 sleeping, 0 stopped, 0 zombie


Cpu0 : 10.2% us, 2.6% sy, 0.0% ni, 86.8% id, 0.3% wa, 0.0% hi, 0.0% si Cpu1 : 9.6% us, 8.0% sy, 0.0% ni, 82.4% id, 0.0% wa, 0.0% hi, 0.0% si Cpu2 : 1.3% us, 1.3% sy, 0.0% ni, 95.0% id, 2.3% wa, 0.0% hi, 0.0% si Cpu3 : 0.0% us, 0.0% sy, 0.0% ni, 100.0% id, 0.0% wa, 0.0% hi, 0.0% si115

Linux 101 Hacks www.thegeekstuff.com
Mem: 1034624k total, 1007420k used, 27204k free, 174540k buffers
Swap: 2031608k total, 0k used, 2031608k free, 615904k cached

Hack 91. Ps Command[edit]

ps command (process status) will display snapshot information of all active processes.


Syntax: ps [options]


How to display all the processes running in the system?


Use "ps aux", as shown below.


# ps aux | more
USER
PID %CPU %MEM
VSZ
RSS TTY STAT START TIME COMMAND
root
1
0.0
0.0
2044
588 ?
Ss Jun27
0:00
init [5]
apache
31186
0.0
1.6
23736 17556 ?
S Jul26
0:40
/usr/local/apache2/bin/httpd
apache
31187
0.0
1.3
20640 14444 ?
S Jul26
0:37
/usr/local/apache2/bin/httpd

You can also use "ps -ef | more", to get a similar output


Print the Process Tree


You can use either ps axuf or ps –ejH to display processes in a tree format. The tree structure will help to visualize the process and it’s parent process immediately. For clarity purpose, few columns have been cut-off in the output below.


# ps axuf116

Linux 101 Hacks www.thegeekstuff.com
root Oct14 0:00 /opt/VRTSralus/bin/beremote
root Oct14 0:00 \_ /opt/VRTSralus/bin/beremote
root Oct14 0:00 \_ /opt/VRTSralus/bin/beremote
root Oct14 0:00 \_ /opt/VRTSralus/bin/beremote
root Oct14 0:01 \_ /opt/VRTSralus/bin/beremote
root Oct 14 0:00 \_ /opt/VRTSralus/bin/beremote
root Dec03 0:01 /usr/local/sbin/sshd
root Dec22 1:08 /usr/local/sbin/sshd
root 23:35 0:00 \_ /usr/local/sbin/sshd
511 23:35 0:00 \_ -bash
511 \_ ps axuf

Note: You can also use pstree command to display process in tree structure.


View Processes Owned by a Particular User


The following command displays all the process owned by Linux user-name: oracle.


$ ps U oracle
PID TTY
STAT
TIME COMMAND
5014
?
Ss
0:01 /oracle/bin/tnslsnr
7124 ?
Ss
0:00 ora_q002_med
8206
?
Ss
0:00 ora_cjq0_med
8852
?
Ss
0:01 ora_pmon_med
8854
?
Ss
0:00 ora_psp0_med
8911
?
Ss
0:02 oraclemed (LOCAL=NO)

View Processes Owned by Current User


Following command displays all the process owned by the current user.


$ ps U $USER
PID TTY
STAT
TIME COMMAND
10329 ?
S
0:00 sshd: ramesh@pts/1,pts/2
10330 pts/1
Ss
0:00 -bash

117

Linux 101 Hacks www.thegeekstuff.com
10354
pts/2 Ss+ 0:00 -bash
10530
pts/1 R+ 0:00 ps U ramesh

Hack 92. Df Command[edit]

df command (disk free) displays the amount of total and free disk space available on the mounted filesystems.


Syntax: df [options] [name]


How much GB of disk space is free on my system?


Use df -h as shown below. Option -h displays the values in human readable format (for example: K for Kb, M for Mb and G for Gb). In the sample output below, / filesystem has 17GB of disk space available and /home/user filesystem has 70GB available.


# df –h
Filesystem
Size
Used Avail Use% Mounted on
/dev/sda1
64G
44G 17G
73%
/
/dev/sdb1
137G
67G 70G
49%
/home/user

What type of filesystem do I have on my system?


Option -T will display the information about the filesystem Type. In this example / and /home/user filesystems are ext2. Option -a will display all the filesystems, including the 0 size special filesystem used by the system.


# df -Tha


Filesystem Type Size Used Avail Use% Mounted on118

Linux 101 Hacks www.thegeekstuff.com
/dev/sda1
ext2
64G
44G
17G
73%
/
/dev/sdb1
ext2
137G
67G
70G
49%
/home/user
none
proc
0
0
0
-
/proc
none
sysfs
0
0
0
-
/sys
none
devpts
0
0
0
-
/dev/pts
none
tmpfs
2.0G
0
2.0G
0%
/dev/shm

Hack 93. Kill Command[edit]

kill command can be used to terminate a running process. Typically this command is used to kill processes that are hanging and not responding.


Syntax: kill [options] [pids|commands]


How to kill a hanging process?


First, identify the process id of the particular process that you would like to kill using the ps command. Once you know the process id, pass it as a parameter to the kill command. The example below shows how to kill the hanging apache httpd process. Please note that typically you should use “apachectl stop” to stop apache.


# ps aux | grep httpd
USER
PID %CPU %MEM
VSZ
RSS TTY STAT START TIME COMMAND
apache
31186
0.0
1.6
23736 17556 ?
S Jul26
0:40
/usr/local/apache2/bin/httpd
apache
31187
0.0
1.3
20640 14444 ?
S Jul26
0:37
/usr/local/apache2/bin/httpd
# kill 31186 31187


Please note that the above command tries to terminate the process graciously by sending a signal called SIGTERM. If the process does not get terminated, you can forcefully terminate the process by passing a signal called SIGKILL, using the option -9 as shown below. You should either be the owner of the process or a privileged user to kill a process.


# kill -9 31186 31187


Another way to kill multiple processes easily is by adding the following two functions to the .bash_profile.


function psgrep ()


{


ps aux | grep "$1" | grep -v 'grep'


}


function psterm ()


{


[ ${#} -eq 0 ] && echo "usage: $FUNCNAME STRING" && return 0 local pid


pid=$(ps ax | grep "$1" | grep -v grep | awk '{ print $1 }') echo -e "terminating '$1' / process(es):\n$pid"


kill -SIGTERM $pid


}
Now do the following, to identify and kill all httpd processes.
# psgrep http
USER
PID %CPU %MEM
VSZ
RSS TTY
STAT START
TIME
COMMAND
apache
31186
0.0
1.6
23736
17556
?
S
Jul26
0:40
/usr/local/apache2/bin/httpd
apache
31187
0.0
1.3
20640
14444
?
S
Jul26
0:37
/usr/local/apache2/bin/httpd

# psterm httpd


terminating 'httpd' / process(es): 31186


31187


Hack 94. Du Command[edit]

du command (disk usage) will print the file space usage for a particular directory and its subdirectories.


How much space is taken by my home directory and all its subdirectories?


In the following example, option -s stands for summary only. i.e it displays only the total size of /home/jsmith and not the individual sizes of all the subdirectories inside the /home/jsmith. Option -h displays the information in a human readable format. i.e K for KB, M for MB and G for GB. The ~ indicates the user home directory. This command is same as “du -sh /home/jsmith”


# du -sh ~


320M/home/jsmith


To get the subdirectories under /home/jsmith listed, execute the above command without the s option.


Hack 95. lsof commands.[edit]

Lsof stands for ls open files, which will list all the open files in the system. The open files include network connection, devices and directories. The output of the lsof command will have the following columns:


o COMMAND process name.


o PID process ID


o USER Username

o FD file descriptor


o TYPE node type of the file


o DEVICE device number


o SIZE file size


o NODE node number


o NAME full path of the file name.


View all open files of the system


Execute the lsof command without any parameter as shown below.


# lsof | more
COMMAND PID USER FD TYPE
DEVICE
SIZE NODE
NAME
init
1
root cwd DIR
8,1
4096
2 /
init
1
root rtd DIR
8,1
4096
2 /
init
1
root txt REG
8,1
32684 983101 /sbin/init
init
1
root mem REG
8,1
106397
166798 /lib/ld-
2.3.4.so
init
1
root mem REG
8,1
1454802
166799
/lib/tls/libc-2.3.4.so
init
1
root mem REG
8,1
53736
163964
/lib/libsepol.so.1
init
1
root mem REG
8,1
56328
166811
/lib/libselinux.so.1
init
1
root 10u FIFO
0,13
972 /dev/initctl
migration
2
root cwd DIR
8,1
4096
2 /
skipped…

The lsof command by itself without may return lot of records as output, which may not be very meaningful except to give you a rough idea about how


many files are open in the system at any given point of view as shown below.


# lsof | wc -l


3093


View open files by a specific user


Use lsof –u option to display all the files opened by a specific user.


# lsof –u ramesh
vi
7190
ramesh txt
REG
8,1
474608
475196 /bin/vi
sshd
7163
ramesh 3u
IPv6
15088263

TCP dev-db:ssh->abc-12-12-12-12.socal.res.rr.com:2631 (ESTABLISHED)


A system administrator can use this command to get some idea on what users are executing on the system.


List Users of a particular file


If you like to view all the users who are using a particular file, use lsof as shown below. In this example, it displays all users who are currently using vi.


# lsof /bin/vi
COMMAND
PID
USER
FD
TYPE DEVICE
SIZE
NODE
NAME
vi
7258
root
txt
REG
8,1
474608
475196
/bin/vi
vi
7300
ramesh txt
REG
8,1
474608
475196
/bin/vi


Hack 96. Sar Command[edit]

Sar commands comes with the sysstat package. Make sure sysstat is installed. If you don’t have sar installed on your system, get it from [21]Sysstat project.


Sar is an excellent monitoring tool that displays performance data of pretty much every resource of the system including CPU, memory, IO, paging, networking, interrupts etc.,


Sar Collects, Reports (displays) and Saves the performance data. Let us look at all the three aspects separately


Sadc - System activity data collector


/usr/lib/sadc (System activity data collector) command collects the system data at a specified time interval. This uses the daily activity data file that is located under /va/log/sa/sa[dd], where dd is the current day.


Sa1 shell-script


/usr/lib/sa1 in-turn calls the /usr/lib/sadcs. sa1 is invoked from the crontab as shown below. Run this every 5 minutes or 15 minutes depending on your need. I prefer to schedule it for every 5 minutes in the cron tab as shown below.


*/5 * * * * root /usr/lib/sa/sa1 1 1


Sa2 shell-script


/usr/lib/sa2 is a shell script that will write a daily report in the /var/log/sa/sa[dd] file, where dd is the current day. Invoke the sa2 from the crontab once a day at midnight.


# 59 23 * * * root /usr/lib/sa/sa2 –A


Note: /etc/cron.d/sysstat files comes with the sysstat package that includes some default value for the sa1 and sa2, which you can change accordingly.


Display CPU Statistics using Sar Command


# sar –u
Linux 2.6.9-42.ELsmp (dev-db)
01/01/2009
12:00:01 AM CPU %user
%nice
%system
%iowait
%idle
12:05:01 AM all 3.70
0.00
0.85
0.00
95.45
12:10:01 AM all 4.59
0.00
1.19
0.06
94.16
12:15:01 AM all 3.90
0.00
0.95
0.04
95.11
12:20:01 AM all 4.06
0.00
1.00
0.01
94.93
12:25:01 AM all 3.89
0.00
0.87
0.00
95.23
12:30:01 AM all 3.89
0.00
0.87
0.00
95.23
Skipped..
Average: all 4.56
0.00
1.00
0.15
94.29

Note: If you need a break down of the performance data for the individual CPU’s, execute the following command.


# sar -u -P ALL


Display Disk IO Statistics using sar command


# sar –d
Linux 2.6.9-42.ELsmp (dev-db)
01/01/2009
12:00:01 AM DEV tps
rd_sec/s
wr_sec/s
12:05:01 AM dev2-0 1.65
1.28
45.43
12:10:01 AM dev8-1
4.08
8.11
21.81
Skipped..
Average: dev2-0 4.66
120.77
69.45
Average: dev8-1
1.89
3.17
8.02

125

Linux 101 Hacks
www.thegeekstuff.com
Display networking Statistics using sar command
# sar -n DEV | more
Linux 2.6.9-42.ELsmp (dev-db)
01/01/2009
12:00:01 AM
IFACE
rxpck/s
txpck/s
rxbyt/s
txbyt/s
rxcmp/s txcmp/
s
rxmcst/s
12:05:01 AM
lo
0.17
0.16
25.31
23.33
0.00 0.0
0
0.00
12:10:01 AM
eth0
52.92
53.64
10169.74
12178.57
0.00 0.0
0
0.00
# sar -n SOCK |more
Linux 2.6.9-42.ELsmp (dev-db)
01/01/2009
12:00:01 AM
totsck
tcpsck
udpsck
rawsck
ip-frag
12:05:01 AM
50
13
3
0
0
12:10:01 AM
50
13
4
0
0
12:15:01 AM
53
13
5
0
0

Hack 97. vmstat Command[edit]

For a typical performance monitoring all you need is only vmstat command. This display memory, swap, IO, system and cpu performance information.


The following command executes vmstat every 1 second for 100 times.


# vmstat 1 100
procs ----------- memory---------- --- swap--
-----
io----
--system--
----cpu----
r b swpd free buff cache si so
bi
bo
in cs us sy id wa
0 0 0 282120 134108 5797012 0
0
0
2
0
0 0
0 100
0
0 0 0 282120 134108 5797012 0
0
0
0 1007
359
0 0 100
0

126

Linux 101 Hacks
www.thegeekstuff.com
0
0
0 282120 134108 5797012
0
0
0
0 1117
577
0
0 100
0
0
0
0 282120 134108 5797012
0
0
0
0 1007
366
0
0 100
0

Vmstat procs Section


o r field: Total number of runnable process o b field: Total number of blocked process


Memory section


o Swpd field: Used swap space o Free field: Available free RAM


o Buff field: RAM used for buffers


o Cache field: RAM used for filesystem cache


Swap Section


o Si field: Amount of memory swapped from disk per second o So field: Amount of memory swapped to disk per second


IO Section


o Bi field: Blocks received from disk o Bo field: Blocks sent to disk.


System Section


o In field: Number of interrupts per second.


o Cs field: Number of context switches per second.


CPU Section


o Us field: Time spend running user code. (non-kernel code)


o Sy field: Time spent running kernel code.


o Id field: Idle time.


o Wa field: Time spent waiting for the IO


Hack 98. Netstat Command[edit]

Netstat command displays the network related information such as network connections, routing tables, interface statistics. Following are few examples on how to use netstat command.


Display Active Internet Connections and domain sockets using netstat


# netstat –an


Active Internet connections (servers and established)


Proto Recv-Q Send-Q Local Address Foreign
Address State
tcp
0
0
0.0.0.0:5666 0.0.0.0:*
LISTEN
tcp
0
0
0.0.0.0:111 0.0.0.0:*
LISTEN
tcp
0
0
0.0.0.0:4086 0.0.0.0:*
LISTEN
skipped..

Active UNIX domain sockets (servers and established)


Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 7894
/tmp/.font-unix/fs7100
unix 2 [ ACC ] STREAM LISTENING 9662
/tmp/.gdm_socket
unix 2 [ ACC ] STREAM LISTENING 10897
@/tmp/fam-root-


Display Active Connections with Process ID and Program Name


This could be very helpful to identify which program has initiated a specific network connection.


# netstat -tap


Active Internet connections (servers and established)


Proto Recv-Q Send-Q Local Address
Foreign
Address State
PID/Program name
tcp
0
0
*:nrpe
*:*
LISTEN 16277/xinetd
tcp
0
0
localhost.localdomain:smtp
*:*
LISTEN 7263/sendmail: acce
tcp
34
0
localhost.localdomain:54221
localhost.localdomain:4089 CLOSE_WAIT
29881/httpd
tcp
0
3216
dev-db:ssh
cpe-76-

94-215-154.soca:4682 ESTABLISHED 11717/sshd: ramesh


Display Routing Table


# netstat –-route


Kernel IP routing table
Destination Gateway
Genmask
Flags
MSS
Window
irtt Iface
192.168.1.0
*
255.255.255.0
U
0 0
0 eth0
162.244.0.0 *
255.255.0.0
U
0
0
0 eth0
default 192.168.1.1
0.0.0.0
UG
0 0
0 eth0
Display RAW network statistics

# netstat --statistics --raw


Ip:


11080343 total packets received


  1. forwarded
  1. with unknown protocol

0 incoming packets discarded


11037744 incoming packets delivered


11199763 requests sent out Icmp:

577135 ICMP messages received


64 input ICMP message failed. ICMP input histogram:


destination unreachable: 537 timeout in transit: 65 source quenches: 2

echo requests: 576476 echo replies: 12 timestamp request: 3 address mask request: 3


581558 ICMP messages sent


0 ICMP messages failed ICMP output histogram:


destination unreachable: 5079 echo replies: 576476 timestamp replies: 3


Misc Netstat Commands


  1. # netstat –-tcp –-numeric List of TCP connection to and from the machine.
  1. # netstat --tcp --listening –-programs Display TCP port that the server is listening on along with the program that is listening on that particular port.

o # netstat –rnC Display the routing cache


Hack 99. Sysctl CommandLinux kernel parameter can be changed on the fly using sysctl command.[edit]

Sysctl helps to configure the Linux kernel parameters during runtime.


# sysctl –a
dev.cdrom.autoclose = 1
fs.quota.writes = 0
kernel.ctrl-alt-del = 0
kernel.domainname = (none)
kernel.exec-shield = 1
net.core.somaxconn = 128
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_wmem = 4096
16384
131072
net.ipv6.route.mtu_expires = 600
sunrpc.udp_slot_table_entries = 16
vm.block_dump = 0

Modify Kernel parameter in /etc/sysctl.conf for permanent change


After modifying the kernel parameter in the /etc/sysctl.conf, execute sysctl – p to commit the changes. The changes will still be there after the reboot.


  1. vi /etc/sysctl.conf
  1. sysctl –p

Modify kernel parameter temporarily


To temporarily modify a kernel parameter, execute the following command. Please note that after reboot these changes will be lost.


# sysctl –w {variable-name=value}


Hack 100. Nice Command[edit]

Kernel decides how much processor time is required for a process based on the nice value. Possible nice value range is: -20 to 20. A process that has a nice value of -20 is very high priority. The process that has a nice value of 20 is very low priority.


Use ps axl to display the nice value of all running process as shown below.


# ps axl
F
UID
PID
PPID PRI
NI
VSZ
RSS WCHAN STAT TTY
TIME COMMAND
4
0
1
0
16
0
2172
552
- S
?
0:17 init [5]
1
0
3
1
34
19
0
0
ksofti SN
?
3:18 [ksoftirqd/0]
1
0
10
1
5 -10
0
0
worker S<
?
0:01 [events/0]
4
0
5145
1
25
10 32124 18592 - SNs
?
0:08 /usr/bin/python
/usr/bin/rhn-applet-gui --sm-client-id
default4
4
0
5147
5142
16
0
3528
604
- S
?
0:00 /sbin/pam_timestamp_check -d root
1
503 17552
4180
16
0 14208 3920
- S
?
0:01 /home/www/apache2/bin/httpd -f
/home/www/apache2/conf/httpd.conf -k start

How to assign a low priority to a shell-script? (higher nice value)


In the example below, when I started the nice-test.sh script in the background, it took the nice value of 0.


$ ./nice-test.sh & [3] 13009


$ ps axl | grep nice-test


0 509 13009 12863 17 0 4652 972 wait S

pts/10:00 /bin/bash ./nice-test.sh


[Note: 6th column with value 0 is the nice.]


Now, let us execute the same shell script with a different nice value as shown below.


$ nice -10 ./nice-test.sh &
[1] 13016
$ ps axl | grep nice-test
0 509 13016 12863
30
10
4236
968 wait SN
pts/1 0:00 /bin/bash ./nice-test.sh

[Note: 6th column with value 10 is the nice value for the shell-script.]


How to assign a high priority to a shell-script? (Lower nice value)


In the following example, let us assign a nice value of -10 (minus 10) to the nice-test.sh shellscript.


$ nice --10 ./nice-test.sh &


[1] 13021


$ nice: cannot set priority: Permission denied


Note: Only root user can set a negative nice value. Login as root and try the same. Please note that there is a double dash before the 10 in the nice command below.


  1. nice --10 ./nice-test.sh &

[1] 13060


  1. ps axl | grep nice-test


4
0 13060 13024
10 -10
5388
964 wait S<
pts/1 0:00 /bin/bash ./nice-test.sh


[Note: 6th column with value -10 is the nice value of the shell-script.]


Hack 101. Renice Command[edit]

Renice alters the scheduling priority of a running process.


How to decrease the priority of a running process? (Increase nice)


In the example below, an existing shell-script is running at nice value of 10. (6th column in the ps output)


$
ps axl | grep nice-test
0
509 13245 13216
30
10
5244
968 wait SN
pts/1 0:00 /bin/bash ./nice-test.sh

To increase the nice value (thus reducing the priority), execute the renice command as shown below.


$ renice 16 -p 13245


13245: old priority 10, new priority 16


$
ps axl | grep nice-test
0
509 13245 13216
36
16
5244
968 wait SN
pts/1 0:00 /bin/bash ./nice-test.sh

[Note: Now, the 6th column of the nice-test.sh (PID 13245) shows the new nice value of 16.]


How to increase the priority of a running process? (Decrease nice)


In the example below, an existing shell-script is running at a nice value of 10.134

Linux 101 Hacks www.thegeekstuff.com
(6th column in the ps output)
$ ps axl | grep nice-test
0
509 13254 13216
30
10
4412
968 wait SN
pts/1 0:00 /bin/bash ./nice-test.sh

In increase the priority, give a lower nice value as shown below. However, only root can increase the priority of a running process, else you’ll get the following error message.


$ renice 5 -p 13254


renice: 13254: setpriority: Permission denied Login as root to increase the priority of a running process


$ su -


# renice 5 -p 13254


13254: old priority 10, new priority 5


#
ps axl | grep nice-test
0
509 13254 13216
25
5
4412
968 wait SN
pts/1
0:00 /bin/bash
./nice-test.sh

[Note: The 6th column now shows a lower nice value of 5 (increased priority)]


12 Amazing and Essential Linux Books


For further reading on Linux, I recommend the following books. The 12 Linux books mentioned here by no means are comprehensive or authoritative list. But, these 12 Books are few of my favorites that I enjoyed reading over the years and I strongly believe will enhance your technical abilities on Linux, if you have not read them yet.


  1. [22]Sed and Awk, by Dale Dougherty and Arnold Robbins. Sed and Awk have transformed the way I worked on Linux command line. This book is the only material you would ever need on Sed and Awk. Once you’ve mastered even the basics of Sed and Awk, you’ll be amazed with the amount of complex tasks you can perform very quickly and elegently. For my day-to-day quick reference of sed and awk examples, I use the Sed and Awk Pocket Reference, written by the same author.
  1. [23]Learning the Vi and Vim Editors, by Arnold Robbins. I’m a command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to carry the Vi editor pocket reference with me all the times. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.
  1. [24]Bash Cookbook, by Carl Albing, JP Vossen and Cameron Newham. Whether you are a sysadmin, DBA or a developer, you have to write shell script at some point. A wise sysadmin knows that once you’ve mastered the shell-scripting techniques, you can put your servers on auto-pilot mode by letting the shell-scripts do the grunt work. To get to the auto-pilot mode of sysadmin, you definitely need to master the examples provided in this cookbook. There are quiet few Bash shell books out there. But, this books tops them all by giving lot of detailed examples.
  1. [25]SSH, The Secure Shell, by Daniel J. Barrett, Richard E. Silverman and Robert G. Byrnes. This is hands-down the best book on SSH. This book explains both theoretical and practical aspects of SSH. Using



SSH as an end-user is fairly straight forward . But, configuring SSH as an administrator is complex and involves a detailed understanding of SSH. This is a must read for any system administrator. The examples in this book show exactly what needs to be done differently for the different flavors of SSH such as SSH1, SSH2 and OpenSSH.


  1. [26]Essential System Administration, by Æleen Frisch. This is an excellent book for those who like to become a Unix System Administrator. This book covers all the typical system administration tasks. This is a perfect companion when you are dealing with multiple flavors of Unix, as it has examples for AIX, FreeBSD, HP-UX, Linux, Solaris and Tru64. I’ve used the pocket version of this book — Essential System Administration Pocket Reference, when I was managing multiple flavors of Unix systems at the same time.
  1. [27]Linux Server Hacks, Volume One, by Rob Flickenger. 100 awesome practical hacks packed in one book. Setup a Linux test bed and try out all these hacks. These hacks are neatly grouped into different sections — Server Basics, Revision Control, Backups, Networking, Monitoring, SSH, Scripting, and Information Servers. Once you’ve mastered these hacks, you should absolutely read Linux Server Hacks, Volume Two, by William von Hagen and Brian Jones, which has 100 Linux hacks focussed on authentication, monitoring, security, performance and connectivity.
  1. [28]DNS and BIND, by Cricket Liu and Paul Albitz. Several years ago, I configured my first DNS by reading online documentation. I brought this book to understand how DNS and BIND works. I’ve already upgraded this book twice when a newer edition was released. This should definitely be in your library, if you are a serious system administrator.
  1. [29]Understanding the Linux Kernel, by Daniel Bovet and Marco Cesati.

If you are a serious developer on Linux environment or a sysadmin, this is a must read. This books explains the inner workings of the Linux Kernel 2.6 in a structured and logical way. This talks about how Kenel handles the Memory Management, Process scheduling, I/O architecture and Block devices. Overall this book is a treat for geeks who are curious to explore what is under the hood of Linux.


  1. [30]Linux Cookbook, by Carla Schroder. This book covers Linux features from both users and system administrators point of view. There are



two chapters dedicated for installing and managing software on RPM-based system and Debian. If you use RedHat, the Linux Pocket Guide, by Daniel J. Barrett is an excellent addition to your library, which covers all the essential Linux command with a sample usage.


  1. [31]Linux Firewalls, by Michael Rash. To build a secure Linux system, you must read this book. There are quiet few books out there for iptables. But, this one talks specifically about the fundamentals of how to configure an Intrusion Detection System using iptables, psad and fwsnort. If you want a comprehensive handy reference of all the things iptables can do with specific examples, Linux Iptables Pocket Reference, by Gregor N. Purdy is the best.
  1. [32]Linux Administration Handbook, by Evi Nemeth, Garth Snyder and Trent R. Hein. During my early days of system administration, I’ve referred this book frequently. This is pretty detailed book with close to 1000 pages and 30 chapters that are nicely grouped together in three high level sections — Basic Administration, Networking and Bunch O’ Stuff.
  1. [33]Beginning Ubuntu Linux, by Keir Thomas and Jaime Sicam. For those who like to transition from Windows to Linux, install Ubuntu Linux on one of your old laptop or desktop and get this book. I strongly believe in spreading the news about Linux to those who don’t use it. If you want any of your loved ones or friends to learn Linux, install Ubuntu on an old laptop and give this book as a gift to them. They’ll definitely be very thankful to you.


Extended Reading


Following are few articles from [34]The Geek Stuff[35]blog for your extended reading. Check out [36]Best Of The Blog[37]section for more articles.


o [38]Turbocharge PuTTY with 12 Powerful Add-Ons o Nagios - Enterprise Monitoring Solution


o [39]Nagios Jumpstart Guide


o [40]Monitor Window Server


o [41]Monitor Linux Server


o [42]Monitor Network Switch


o [43]Monitor VPN Device


o Perform SSH and SCP without entering password: o [44]From openSSH to openSSH


o [45]From openSSH to SSH2 o [46]From SSH2 to SSH2

o [47]Vi / Vim Tips and Tricks


o [48]Vim Macro Tutorial: How To Record and Play o How To Use Vim as [49]Perl IDE[50]and [51]C/C++ IDE o [52]Automatic Word Completion in Vim


o [53]3 Steps to Add Custom Header to a File Using Vim o [54]The Ultimate Guide for Creating Strong Passwords


  1. [55]Firefox Add-On: Hire 7 Personal Bodyguards to Browse Internet Securely

o [56]Tripwire Tutorial: Linux Host Based Intrusion Detection System


  1. [57]Midnight Commander (mc) Guide: Powerful Text based File Manager [58]for Unix


Your Feedback and Support


I hope you found Linux 101 Hacks eBook helpful. Thanks for reading. I sincerely appreciate all the support given by the regular readers of my blog. Without your tremendous support, it would’ve been difficult to find the motivation to write this eBook.


[59]Subscribe to TGS


To get Linux Tips, HowTos, Guides and Tutorials on an on-going basis, please [60]subscribe to The Geek Stuff blog. If you subscribe, you will get new articles posted on TGS website directly to your inbox or to your RSS reader.


[61]Contact TGS


Please use this [62]contact form[63]to send me your feedback, question, or clarification on any of the 101 hacks mentioned in this eBook.140PDF to Wor