Tuesday, October 1, 2013

Important keyboard shortcuts in Linux Shell(BASH)

Following keyboard shortcuts are very useful while working in Bash

  1. Ctrl +u Cut the current line
  2. Ctrl+y Paste the line which has been earlier with Ctrl +u
  3. Ctrl +l clear the screen
  4. Ctrl+G get the new line and abandon the current line
  5. Ctrl+a go to beginning of the line
  6. Ctrl+e go to End of line
  7. Ctrl+k Erase from the cursor to the end of line
  8. Ctrl +r search in the history
  9. Ctrl+w cuts a word backwards
  10. Ctrl+d  Tired of typing 'exit' to close a terminal? Just hit ctrl+d on a blank line and boom!
  11. Ctrl+right - Jump one word to the right.
  12. Ctrl+left - Jump one word to the left.
  13. ALT+DEL Delete word(s) to the left of the cursor
  14. !! Repeat last command 
There are some situation when i want to read the log as well redirect it to some file for later view. 
some_command | tee output.txt 
if you want to redirect both STDERR(2) and STDOUT(1) to the same thing then we can use 2 >&1
some_command >big_blob_of_crap.txt 2>&1
The output of the ‘come_command’ is redirected to a ‘big_blob_of_crap.txt’ and the error channel (that is the ’2′ is redirected to a pointer (?) of the output (‘&1′).
So stderr goes to the stdout and that goes to the file.
some_command &>big_blob_of_crap.txt
If you want to redirect STDERR and STDOUT to a single file and at the same time, you want to view them on the screen then we can use use
some_command |tee big_blob_of_crap.txt 2>&1


Friday, March 2, 2012

Important history options to be placed in .bashrc

Configuring the command-line history options in .bash_login (or .bashrc) is really useful. The following is a cadre of settings that I use on my Macbook Pro.
Setting the following makes bash erase duplicate commands in your history
export HISTCONTROL="erasedups:ignoreboth"
I also jack my history size up pretty high too.
export HISTFILESIZE=500000 export HISTSIZE=100000
Another thing that I do is ignore some commands from my history. No need to remember the exit command.
export HISTIGNORE="&:[ ]*:exit"
You definitely want to set histappend. Otherwise, bash overwrites your history when you exit.
shopt -s histappend
Another option that I use is cmdhist. This lets you save multi-line commands to the history as one command.
shopt -s cmdhist

Tuesday, February 14, 2012

Some Vim shortcuts

Some productivity tips:
Smart movements
* and # search for the word under the cursor forward/backward. 
w to the next word 
W to the next space-separated word 
b / e to the begin/end of the current word. (B / E for space separated only) 
gg / G jump to the begin/end of the file.
%jump to the matching { .. } or ( .. ), etc.. 
{ / } jump to next paragraph.
Quick editing commands
I insert at the begin. 
A append to end. 
o / O open a new line after/before the current. 
v / V visual mode (to select text!) 
Shift+R replace text 
C change remaining part of line.
Combining commands
Most commands accept a amount and direction, for example:
cW = change till end of word 
3cW = change 3 words 
BcW = to begin of full word, 
change full word ciW = change inner word. 
ci" = change inner between ".." 
ci( = change text between ( .. ) 
4dd = delete 4 lines 
3x = delete 3 characters. 
3s = substitute 3 characters.
Useful programmer commands
r replace one character (e.g. rd replaces the current char with d).
 ~ changes case. 
J joins two lines 
Ctrl+A / Ctrl+X increments/decrements a number.
. repeat last command (a simple macro) 
= indent line (specify a range, or use visual mode)
Macro recording
Press q[key] to start recording. 
Then hit q to stop recording. 
The macro can be played with @[key].
 
VIM as a file comparator:
Use '-d' switch to compare two files in VIM. This command splits the VIM screen vertically and shows the differences.
 
vim -d chmod-restore.pl chmod-restore1.pl
  
 

Wednesday, November 16, 2011

Saving a root owned file as normal user from within Vi/Vim

This happens lot of times. I login as a normal user and start to edit root owned file in vim / vi text editor. However, I'm not able to save changes due to permission issue (all config files are owned by root). So to save file, i can do :w !sudo tee % where
Where,
:w - Write a file.
!sudo - Call shell sudo command.
tee - The output of write (vim :w) command redirected using tee. The % is nothing but current file name i.e. /etc/apache2/conf.d/mediawiki.conf. In other words tee command is run as root and it takes standard input and write it to a file represented by %. However, this will prompt to reload file again (hit L to load changes in vim itself):

Sunday, June 5, 2011

Effective usage of watch

Sometimes is useful to run over and over again the same command until something happen,
Sure you can use bash history and use up arrow and return over and over again, or perhaps write some line of bash to get an infinite loop that run the same command, but there is a smarter approach to this : watch

watch -d "ls -alrt /tmp/

Options

The most common options for watch are:

-n set the interval in seconds.
-d flag will highlight the differences between successive updates.
-b option causes the command to beep if it has a non-zero exit.
-e cause watch to exit on an error from the program running.

watch -n 1 "mysqladmin --user=root --password=mypassword processlist"

watch 'ps -eo pcpu,pid,user,args | sort -k 1 -n -r | head -10'

Friday, April 2, 2010

Using tar with ssh effectively

We know that tar could be used to take backup. We may like to transfer backup to some remote box. Also some time,it is needed to transfer file with directory structure in the remote box.
Lets say we have to transfer many files in the remote box while keeping file permissions and directory structure intact.
We may choose to tar the source directory ,compress it and scp to destination box. We will need to create a v big temp file only to be transferred to the destination box and and removed from source box. Once the file is scp'd to destination box, we will extract the tar'd file to create files in the same directory structure.

Steps :-

(In Src box)

1) Tar the directory in the source file ( tar -cvzf tarfilename.tar.gz /path/of/dir )
2) transfer file to destination box (scp tarfilename.tar.gz user@remote-host)
3) Delete the file ( rm tarfilename.tar.gz)

(In Dest Box)

4) Untar the file (tar -xvzf tarfilename.tar.gz)
5) Remote the file (rm tarfilename.tar.gz)

We can accomplish all these steps in a single command.

Tar has the great ability to send data to stdout/stdin using the "-" (dash) as a filename in the command line. So using that we can string together pipes to send the data to a remote server

tar -cvzf - /dir/to/be/transferred/ | ssh remoteuser@remotehost 'tar -C /path/of/dir -xvzf -'

IN NUTSHELL, WE HAVE REPLACED tarfilename.tar.gz WITH -(DASH)

What this does is pretty simple: it creates a compressed tar file of the /dir/to/be/transferred/ and sends it to stdout (-). We catch stdout with the pipe (|) and then call ssh to connect to a remote server where we execute the remote command tar. Using the remote tar we run a decompress extraction of stdin (-). tar -c changes directory

Great way to transfer a bunch of files securely, as well maintain ownership and permissions.
Its the example of PUSH method.
Following example will show the reverse method.

Lets say we have a backup file in remote host and we want to restore it in local box.
(PULL Method)

ssh user@remote-host "cat /path/to/backup/backupfile.tar.bz2" |tar jpxf -

In this command, we are cat'ing zipped file and output is being redirected to local tar using pipe. So local tar command will replace -(dash) with zipped file and will untar it.

Many other scenarios could be found in http://www.lamolabs.org/blog/1766/pushing-pulling-files-around-using-tar-ssh-scp-rsync/

Monday, March 29, 2010

Run the last command as ROOT

There are many privileged commands which you run as non-prev user only to get error or moy desired output. Then you think like sudo and type the same command again.
We can type sudo !! to execute the last command as PRIV user