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

Go4Expert Member
18Aug2009,14:27   #2
shency's Avatar
Really interesting & useful
Newbie Member
26Aug2009,10:59   #3
adarshtp's Avatar
nice one. Really helpful
Go4Expert Founder
2Sep2009,18:56   #4
shabbir's Avatar
Nomination for Article of the month - Aug 2009 Started.
Invasive contributor
2Sep2009,19:19   #5
nimesh's Avatar
nice tips.
Came to know some new commands.

Thanks
Light Poster
21Sep2009,18:19   #6
divengrabber's Avatar
great info carry on.
Newbie Member
27Oct2009,22:24   #7
BukiBv's Avatar
Code:
Really good thanks
Go4Expert Member
18Jul2010,08:19   #8
King Falcon's Avatar
thank you very much...
this is really a good post...