Linux Hack
Linux Hack Ajay Kumar Tiwari Table of Contents Introduction Foreword Version Chapter 1: Powerful CD Command Hacks Use CDPATH to define the base directory for cd command Use cd alias to navigate up the directory effectively Perform mkdir and cd using a single command Use cd - to toggle between the last two directories Use dirs, pushd and popd to manipulate directory stack Use shopt -s cdspell to automatically correct mistyped directory names on cd Chapter 2: Date Manipulation Set System Date and Time Set Hardware Date and Time Display Current Date and Time in a Specific Format Display Past Date and Time Display Future Date and Time Chapter 3: SSH Client Commands Identify SSH Client Version Login to Remote Host using SSH Debug SSH Client Session Toggle SSH Session using SSH Escape Character SSH Session Statistics using SSH Escape Character Chapter 4: Essential Linux Commands Grep Command
Linux Hack Ajay Kumar Tiwari Find Command Suppress Standard Output and Error Message Join Command Change the Case Xargs Command Sort Command Uniq Command Cut Command Stat Command Diff Command Display total connect time of users Chapter 5: PS1, PS2, PS3, PS4 and PROMPT_COMMAND PS1 - Default Interaction Prompt PS2 - Continuation Interactive Prompt PS3 - Prompt used by select inside shell script PS4 - Used by set -x to prefix tracing output PROMPT_COMMAND Chapter 6: Colorful and Functional Shell Prompt Using PS1 Display username, hostname and basename of directory in the prompt Display current time in the prompt Display output of any command in the prompt Change foreground color of the prompt Change background color of the prompt Display multiple colors in the prompt Change the prompt color using tput Create your own prompt using the available codes for PS1 variable Use bash shell function inside PS1 variable Use shell script inside PS1 variable Chapter 7: Archive and Compression
Linux Hack Ajay Kumar Tiwari Zip command basics Advanced compression using zip command. Password Protection of Zip files Validate a zip archive Tar Command Basics Combine gzip, bzip2 with tar Chapter 8: Command Line History Display TIMESTAMP in history using HISTTIMEFORMAT Search the history using Control+R Repeat previous command quickly using 4 different methods Execute a specific command from history Execute previous command that starts with a specific word Control the total number of lines in the history using HISTSIZE Change the history file name using HISTFILE Eliminate the continuous repeated entry from history using HISTCONTROL Erase duplicates across the whole history using HISTCONTROL Force history not to remember a particular command using HISTCONTROL Clear all the previous history using option -c Substitute words from history commands Substitute a specific argument for a specific command Disable the usage of history using HISTSIZE Ignore specific commands from the history using HISTIGNORE Chapter 9: System Administration Tasks Partition using fdisk Format a partition using mke2fsk Mount the partition
Linux Hack Ajay Kumar Tiwari Fine tune the partition using tune2fs Create a swap file system. Create a new user Create a new group and assign to an user Setup SSH passwordless login in OpenSSH Use ssh-copy-id along with ssh-agent Crontab Safe Reboot Of Linux Using Magic SysRq Key Chapter 10: Apachectl and Httpd Examples Pass different httpd.conf filename to apachectl Use a temporary DocumentRoot without modifying httpd.conf Increase the Log Level temporarily Display the modules inside Apache Show all accepted directives inside httpd.conf Validate the httpd.conf after making changes Display the httpd build parameters Load a specific module only on demand Chapter 11: Bash Scripting Execution Sequence of .bash_* files How to generate random number in bash shell Debug a shell script Quoting Read data file fields inside a shell script Chapter 12: System Monitoring and Performance Free command Top Command Ps Command Df Command Kill Command Du Command
Linux Hack Ajay Kumar Tiwari Sar Command vmstat Command Netstat Command Sysctl Command Nice Command Renice Command lsof commands.
Linux Hack Ajay Kumar Tiwari Introduction There are only 10 types of people in the world those who understand binary, those who dont, and those who understand gray code Geek
Im Ajay Kumar Tiwari , author of this Book 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). Copyright 2015 Ajay Kumar Tiwari
Linux Hack Ajay Kumar Tiwari Foreword 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 Hack" 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 Sdwestfalen, Germany (Author of several Vim plugins, including bash-support vim plugin) Version Version 1.0 Date 25-Mar-2015 Revisions First Edition
Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently used commands during a UNIX session. Hack 1. Hack 1.
Use CDPATH to define the base directory for cd command 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
Linux Hack Ajay Kumar Tiwari 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: o 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 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) o o o Hack 2. Use cd alias to navigate up the directory effectively 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. # 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 # cd ../../../../
Next page