Shell Tips & Tricks

pradeep's Avatar author of Shell Tips & Tricks
This is an article on Shell Tips & Tricks in Unix.
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.
BukiBv, nimesh likes this
0
shency's Avatar, Join Date: Oct 2008
Go4Expert Member
Really interesting & useful
0
adarshtp's Avatar, Join Date: Aug 2009
Newbie Member
nice one. Really helpful
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nomination for Article of the month - Aug 2009 Started.
0
nimesh's Avatar, Join Date: Apr 2009
Invasive contributor
nice tips.
Came to know some new commands.

Thanks
0
divengrabber's Avatar, Join Date: Aug 2009
Light Poster
great info carry on.
0
BukiBv's Avatar, Join Date: Mar 2009
Newbie Member
Code:
Really good thanks
0
King Falcon's Avatar, Join Date: Jul 2010
Go4Expert Member
thank you very much...
this is really a good post...