Thursday 10 November 2011

Linux Commands


History-:
                To clear the history use the following command.

[anji@mail ~]$ history –c



         The above command will removes all the history of the user. For example the above command removes the history of the user “anji” only.

To execute a command that was typed 2 commands back, do the following

[root@mail ~]# !-2

To execute the previous command, do any one of the following

[root@mail ~]# !!            (or)

[root@mail ~]# !-1

        Press <Ctrl>-p to get the previous commands.

àTo execute a previous command with keywords use !string and !?string. For example the below command search for previous command that start with the keyword “ps” and execute it.

[root@mail ~]# !ps
ps
  PID TTY          TIME CMD
 2105 pts/0    00:00:00 ps
 6245 pts/0    00:00:00 bash
 6414 pts/0    00:00:00 su
 8246 pts/0    00:00:00 su
 8247 pts/0    00:00:00 bash
 8278 pts/0    00:00:00 su
 8925 pts/0    00:00:00 su
 8926 pts/0    00:00:00 bash

            If the ps command is used multiple times then it will execute the last used command. For example if ps aux and ps commands are executed and if ps is the last used command then ps will execute.
The following example will search for previous command that contatins the keyword “vi” and execute it.

[root@mail ~]# !?vi

vi anji.txt

àTo replace a string from the previous command use ^str1^str2^. For example first we opened a file using cat command. Later we realized that we need to edit the content of the file. Instead of typing the whole file name again, we can just replace the “cat” in the previous command with “vi” as shown below.

[root@mail ~]# cat /var/log/yum.log
Aug 29 17:50:37 Installed: libtool-ltdl-1.5.22-6.1.i386
Aug 29 17:52:19 Installed: 30:bind-9.3.6-4.P1.el5.i386

[root@mail ~]# ^cat^vi^
vi /var/log/yum.log

àTo get the 1st argument of a command use :^ , the following example gets the 1st argument from the previous command.

[root@mail ~]# ls -l !!:^
ls -l /var/log/yum.log
-rw-r--r-- 1 root root 693 Aug 29 20:24 /var/log/yum.log

[root@mail ~]# cp /etc/passwd /backup

[root@mail ~]# ls -l !cp:^
ls -l /etc/passwd
-rw-r--r-- 1 root root 1740 Aug 31 21:03 /etc/passwd

à To get the last argument of a command use :$ , the following example gets the last argument from the previous command.

[root@mail ~]# ls -l !cp:$
ls -l /backup
total 4
-rw-r--r-- 1 root root 1740 Aug 31 22:46 passwd

[root@mail ~]# ls -l !!:$
ls -l /backup
total 4
-rw-r--r-- 1 root root 1740 Aug 31 22:46 passwd

àTo get the nth argument of a commad use :n, the following example get the 2nd argument of the previously used cp command.

[root@mail ~]# cp -rf /home/anji/ /backup/

[root@mail ~]# ls -l !cp:2
ls -l /home/anji/
total 0
          In the above example second argument is /home/anji.
The following gets all the arguments from 2.

[root@mail ~]# ls -l !cp:2-$
ls -l /home/anji/ /backup/
/backup/:
total 8
drwx------ 3 root root 4096 Aug 31 23:00 anji
-rw-r--r-- 1 root root 1740 Aug 31 22:46 passwd
/home/anji/:
total 0
->!!:* gets all the arguments from the previous command
->!!:2* gets all the arguments starting from 2nd argument
->!!:2-$ same as above. gets all the arguments starting from 2nd argument
->!!:2- gets all the arguments starting from 2nd argument(except the last argument).

àTo get all the arguments from a command use :*, the following example locates the previous command in the history that starts with “cp” and gets all it’s arguments.

[root@mail ~]# ls -l !cp:*
ls -l -rf /home/anji/ /backup/
/home/anji/:
.viminfo  .emacs  ..  .bashrc  .  .bash_profile  .mozilla  .bash_logout
/backup/:
passwd  .  anji  ..

àTo remove the trailing path name from a word use :h, the following example removes the filename and gets only the full path.

[root@mail ~]# ls -l /backup/anji/.bashrc
-rw-r--r-- 1 root root 124 Aug 31 23:00 /backup/anji/.bashrc

[root@mail ~]# ls -l !!:$:h
ls -l /backup/anji
total 0

àTo remove all leading path name form a word use :t.

-->To remove the file name extension from a word use :r (for example backup.txt will be removed and print as backup)

No comments:

Post a Comment