Shell Tips & Tricks

Discussion in 'Unix' started by pradeep, Aug 10, 2009.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    48
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Shell Tips & Tricks

    Create A Command Alias

    Most of us use 'ls -l' so, will it not be easier to have a smaller command to show us long listing, add the following line to ~/.bashrc
    Code:
    alias ll='ls -l'
    Using it
    Code:
    [root@pradeep ~]# ll
    total 264
    -rw------- 1 root   root     1221 May 21 21:57 anaconda-ks.cfg
    -rw-r--r-- 1 root   root     2063 Jul 20 12:21 content_passlimit.txt
    drwxr-xr-x 2 root   root     4096 Jul 20 16:09 dumps
    -rw-r--r-- 1 root   root    35779 May 21 21:56 install.log
    -rw-r--r-- 1 root   root     5142 May 21 21:56 install.log.syslog
    drwxr-xr-x 7 root   root     4096 Jun 24 16:23 json-1.2.0
    -rw-r--r-- 1 root   root    13286 Mar 15  2006 json-1.2.0.tgz
    -rw-r--r-- 1 root   root       17 Aug 10 13:30 lwp_cookies.dat
    -rw------- 1 root   root   141066 May 29 17:47 mbox
    -rw-r--r-- 1 root   root    11484 Jul  6 13:01 modify_printfile.pl
    -rw-rw-r-- 1 nagios nagios   4289 Mar 15  2006 package.xml
    drwxr-xr-x 2 root   root     4096 Jul 27 19:29 test
    drwxr-xr-x 2 root   root     4096 Jul 18 12:19 test1
    
    You may create as many aliases as you want. Please share any interesting aliases you create.

    Use Previous Command's Arguments

    Running a command with the same arguments used in the previous command, use '!*' to repeat all arguments or '!:2' to use the second argument. '!$' uses the last argument. Let's see a few examples.

    Code:
    [root@pradeep ~]# mkdir -p /tmp/pradeep/test/shell
    [root@pradeep ~]# cd !$
    cd /tmp/pradeep/test/shell
    [root@pradeep shell]# mkdir -p /tmp/pradeep/test2 /tmp/pradeep/test3
    [root@pradeep shell]# chmod -R 777 !:2 !:3
    chmod -R 777 /tmp/pradeep/test2 /tmp/pradeep/test3
    [root@pradeep shell]# touch a.txt b.txt
    [root@pradeep shell]# ls !*
    ls a.txt b.txt
    a.txt  b.txt
    [root@pradeep shell]# ls -l !*
    ls -l a.txt b.txt
    -rw-r--r-- 1 root root 0 Aug 10 13:54 a.txt
    -rw-r--r-- 1 root root 0 Aug 10 13:54 b.txt
    [root@pradeep shell]#
    
    Useful Bash Keystrokes

    Ctrl+U - Cuts everything to the left
    Ctrl+W - Cuts the word to the left
    Ctrl+Y - Pastes what's in the buffer
    Ctrl+A - Go to beginning of line
    Ctrl+E - Go to end of line
    Ctrl+R - Search command history

    Changing File Extensions

    Code:
    rename 's/.html$/.html.bak/' *.html
    This will change all .html files to html.bak

    Run a second command only if the first one is successfull

    Code:
    cd /tmp/pradeep/test/shell && tar xjvf /home/pradeep/songs.tar.bz
    This is usually used like this 'make && make install' while compiling softwares.
     
    nimesh likes this.
  2. shency

    shency New Member

    Joined:
    Oct 25, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Really interesting & useful
     
  3. adarshtp

    adarshtp New Member

    Joined:
    Aug 26, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    nice one. Really helpful
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    nice tips.
    Came to know some new commands.

    Thanks
     
  6. divengrabber

    divengrabber New Member

    Joined:
    Aug 9, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    great info carry on.
     
  7. BukiBv

    BukiBv New Member

    Joined:
    Mar 29, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    Really good thanks
     
  8. King Falcon

    King Falcon New Member

    Joined:
    Jul 18, 2010
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    /root
    Home Page:
    http://kingfalcon.co.tv/
    thank you very much...
    this is really a good post...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice