Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Unix (http://www.go4expert.com/articles/unix/)
-   -   Shell Tips & Tricks (http://www.go4expert.com/articles/shell-tips-tricks-t18967/)

pradeep 10Aug2009 13:55

Shell Tips & Tricks
 
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.

shency 18Aug2009 14:27

Re: Shell Tips & Tricks
 
Really interesting & useful

adarshtp 26Aug2009 10:59

Re: Shell Tips & Tricks
 
nice one. Really helpful

shabbir 2Sep2009 18:56

Re: Shell Tips & Tricks
 
Nomination for Article of the month - Aug 2009 Started.

nimesh 2Sep2009 19:19

Re: Shell Tips & Tricks
 
nice tips.
Came to know some new commands.

Thanks

divengrabber 21Sep2009 18:19

Re: Shell Tips & Tricks
 
great info carry on.

BukiBv 27Oct2009 22:24

Re: Shell Tips & Tricks
 
Code:

Really good thanks

King Falcon 18Jul2010 08:19

Re: Shell Tips & Tricks
 
thank you very much...
this is really a good post...


All times are GMT +5.5. The time now is 23:00.