2007-10-18-running-previous-commands-in-linux.md 3.58 KB
Newer Older
Danesh Manoharan's avatar
Danesh Manoharan committed
1
2
---
title: Running previous commands in Linux
3
author: Danesh Manoharan
Danesh Manoharan's avatar
Danesh Manoharan committed
4
5
6
7
8
9
10
date: 2007-10-17T16:38:53+00:00
pvc_views:
  - 14300
dsq_thread_id:
  - 889736617

---
11
Most non linux people tend to be taunted by the Linux command line, it's no beast nor is the user behind it a super geek just because you see him hammering away at the keyboard with lines after lines of commands. Chances are he is repeating commands or changing commands here and there to get some results. In Linux the first time takes time and practice to produce accurate results. This is what most sysadmins spend their time doing but once you get the hang of it, it became a walk in the park.
Danesh Manoharan's avatar
Danesh Manoharan committed
12

13
How often do you repeat commands while using the command line? I do this all the time but sometimes it's good to learn some shortcuts now and then to reduce repetitive tasks and improve efficiency.
Danesh Manoharan's avatar
Danesh Manoharan committed
14

15
When you type a command in the command line, it's saved into the .bash_history file within the home directory. The history file will hold the last 500 commands. To view them you can use "**_history_**" or "_**history | less**_" if the result is too long. The history file is also frequently use for user/security audits and RCA work.
Danesh Manoharan's avatar
Danesh Manoharan committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29

You will that within the output below each line is numbered, these numbers will come in handy later in this tutorial.

    danny@python:~> history
    1  uname -r
    2  man mount
    3  PS -EF
    4  ps -ef
    5  ps -ef | grep kopete
    6  kill -9 22632
    7  /sbin/ifconfig
    8  ping google.com
    9  ping 192.168.0.1

30
If you want to execute the last command you typed in again you would normally have to scroll up with the arrow key but this can also be done with "_**!!**_". See sample below.
Danesh Manoharan's avatar
Danesh Manoharan committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

    danny@python:~> ls -l
    total 40
    drwxr-xr-x 2 danny users 4096 2007-10-11 13:16 bin
    drwxr-xr-x 4 danny users 4096 2007-10-11 16:11 deluge
    drwx------ 4 danny users 4096 2007-10-17 22:38 Desktop
    drwx------ 2 danny users 4096 2007-10-11 13:16 Documents
    drwxr-xr-x 2 danny users 4096 2007-10-11 13:16 public_html
    danny@python:~> !!
    ls -l
    total 40
    drwxr-xr-x 2 danny users 4096 2007-10-11 13:16 bin
    drwxr-xr-x 4 danny users 4096 2007-10-11 16:11 deluge
    drwx------ 4 danny users 4096 2007-10-17 22:38 Desktop
    drwx------ 2 danny users 4096 2007-10-11 13:16 Documents
    drwxr-xr-x 2 danny users 4096 2007-10-11 13:16 public_html

48
You could also execute a command from the history file by referring to the corresponding line number by using "_**![##]**_". See sample below.
Danesh Manoharan's avatar
Danesh Manoharan committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

    danny@python:~> history
    1  ls -l
    2  ls -la
    3  history
    4  cd /
    5  cd -
    6  cd /nfs
    7  cd /sto
    8  ls
    9  history
    danny@python:~> !3
    history
    1  ls -l
    2  ls -la
    3  history
    4  cd /
    5  cd -
    6  cd /nfs
    7  cd /sto
    8  ls
    9  history
    10  history

73
Using a string in place of the line number is also possible. The syntax for this is "_**![string]**_". See sample below. The string value is matched against entries in the .bash_history file and the first matching entry will be excuted. For example if the "history" command existed on line 10, 100 and 450, line 450 would be returned.
Danesh Manoharan's avatar
Danesh Manoharan committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

    danny@python:~> history
    1  ls -l
    2  ls -la
    3  history
    4  cd /
    5  cd -
    6  cd /nfs
    7  cd /sto
    8  ls
    9  history
    10  history
    danny@python:~> !history
    history
    1  ls -l
    2  ls -la
    3  history
    4  cd /
    5  cd -
    6  cd /nfs
    7  cd /sto
    8  ls
    9  history
    10  history

Hope you find this helpful. Drop me a comment if you have questions or comments.