Linux Hack, Learn Interesting commands and Play with Linux
Trick 1: Perform mkdir and cd using a single command
Sometimes when you create a new directory, you may cd to the new directory immediately to perform some work as shown below.# mkdir -p /tmp/subdir1/subdir2/subdir3 # cd /tmp/subdir1/subdir2/subdir3 # pwd /tmp/subdir1/subdir2/subdir3Wouldn’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
Trick 2: Prompt CommandBash shell executes the content of the PROMPT_COMMAND just before displaying the PS1 variable.ramesh@dev-db ~> export PROMPT_COMMAND="date +%H:%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 +%H:%M:%S)]" [22:08:51]ramesh@dev-db ~> [Note: This displays the PROMPT_COMMAND and PS1 output on the same line]
Trick 3: Change color of prompt using tput command
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:
- tput setab [1-7] – Set a background color using ANSI escape
- tput setb [1-7] – Set a background color
- tput setaf [1-7] – Set a foreground color using ANSI escape
- tput setf [1-7] – Set a foreground color
tput Text Mode Capabilities:
- tput bold – Set bold mode
- tput dim – turn on half-bright mode
- tput smul – begin underline mode
- tput rmul – exit underline mode
- tput rev – Turn on reverse mode
- tput smso – Enter standout mode (bold on rxvt)
- tput rmso – Exit standout mode
- tput sgr0 – Turn off all attributes
Color Code for tput:
- 0 – Black
- 1 – Red
- 2 – Green
- 3 – Yellow
- 4 – Blue
- 5 – Magenta
- 6 – Cyan
- 7 – White
Trick 4: Change the history file name using HISTFILE
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
Writing a script to automate various tasks on your system that you would normally have to do manually is something that I’ve become infatuated with recently. Maybe it’s the automation engineer in me, but the idea of having a computer intelligently do what I don’t have time to do is very appealing. That is why I’ve recently started offering some tips on how you can automate your own computer with windows host scripts, typically using Visual Basic script to do things like automate telnet commands or even sending emails.










