1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Shell Tips & Tricks

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

  1. pradeep

    pradeep Team Leader

    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

    Really interesting & useful
     
  3. adarshtp

    adarshtp New Member

    nice one. Really helpful
     
  4. shabbir

    shabbir Administrator Staff Member

  5. nimesh

    nimesh New Member

    nice tips.
    Came to know some new commands.

    Thanks
     
  6. divengrabber

    divengrabber New Member

    great info carry on.
     
  7. BukiBv

    BukiBv New Member

    Code:
    Really good thanks
     
  8. King Falcon

    King Falcon New Member

    thank you very much...
    this is really a good post...
     

Share This Page