1 00:00:06,630 --> 00:00:09,510 - In this video, we are going to explore history. 2 00:00:09,510 --> 00:00:10,920 Now, what is history? 3 00:00:10,920 --> 00:00:14,460 History is a bash feature that makes sure 4 00:00:14,460 --> 00:00:17,490 that all the commands that the user types is written 5 00:00:17,490 --> 00:00:20,700 to a file with the name dot bash underscore history 6 00:00:20,700 --> 00:00:22,203 in the user home directory. 7 00:00:23,310 --> 00:00:25,320 This file is persistent, and it means 8 00:00:25,320 --> 00:00:28,260 that the commands that you have typed are even kept. 9 00:00:28,260 --> 00:00:30,750 Even if you reboot your system. 10 00:00:30,750 --> 00:00:33,360 Now the history command can be used to repeat commands 11 00:00:33,360 --> 00:00:35,700 from this file or to just get an overview 12 00:00:35,700 --> 00:00:38,103 of everything that you have done. 13 00:00:39,541 --> 00:00:40,860 And there is a couple of 14 00:00:40,860 --> 00:00:43,920 of things that you can do to start with. 15 00:00:43,920 --> 00:00:47,190 You can use history minus C to clear the current history 16 00:00:47,190 --> 00:00:50,892 or history minus w to write the current history. 17 00:00:50,892 --> 00:00:53,700 Pretty convenient is history minus D 18 00:00:53,700 --> 00:00:57,330 which deletes a specific line from history. 19 00:00:57,330 --> 00:01:00,030 History minus D can be very convenient because 20 00:01:00,030 --> 00:01:02,700 at some cases you might be typing something 21 00:01:02,700 --> 00:01:06,240 in your command line that you don't want to be stored, 22 00:01:06,240 --> 00:01:09,690 because it contains security sensitive information. 23 00:01:09,690 --> 00:01:13,410 Well, if that is the case, you can look up that 24 00:01:13,410 --> 00:01:16,293 that line and use history minus D to delete it. 25 00:01:17,130 --> 00:01:18,630 Then we have Ctrl+R. 26 00:01:18,630 --> 00:01:21,480 Ctrl+R is one of the ways that you can use 27 00:01:21,480 --> 00:01:23,610 on the bash shell to work with history. 28 00:01:23,610 --> 00:01:26,040 It's a so-called to reverse-I-search. 29 00:01:26,040 --> 00:01:27,690 And in this reverse-I-search 30 00:01:27,690 --> 00:01:30,240 you can just start typing your command 31 00:01:30,240 --> 00:01:34,770 and the batch shell will do backwards lookup 32 00:01:34,770 --> 00:01:38,430 in your command history to look for any commands 33 00:01:38,430 --> 00:01:41,910 that contain the string that you have just entered. 34 00:01:41,910 --> 00:01:43,440 Not necessarily yet to begin, 35 00:01:43,440 --> 00:01:45,870 it can be totally in the middle as well. 36 00:01:45,870 --> 00:01:48,720 Also, convenient is exclamation mark. 37 00:01:48,720 --> 00:01:52,350 Exclamation mark number which will repeat a specific line 38 00:01:52,350 --> 00:01:54,303 from history. Let me show you. 39 00:01:56,310 --> 00:01:58,350 So let me start with this history. 40 00:01:58,350 --> 00:02:00,390 Here's my command history so far 41 00:02:00,390 --> 00:02:02,370 That's history since the moment that this 42 00:02:02,370 --> 00:02:04,920 system was created. The default settings 43 00:02:04,920 --> 00:02:09,920 on Linux make that there's history size set to 1000 lines. 44 00:02:10,290 --> 00:02:14,550 So you will keep the last 1000 lines in your history, 45 00:02:14,550 --> 00:02:16,560 as well as in the history file. 46 00:02:16,560 --> 00:02:17,950 LS - L. Bash_history 47 00:02:20,340 --> 00:02:21,870 is showing it's file. 48 00:02:21,870 --> 00:02:24,000 There we go, the bash history. 49 00:02:24,000 --> 00:02:28,410 And if we want to look it up, then what is going on? 50 00:02:28,410 --> 00:02:30,870 Well, you can see, oh, that is funny. 51 00:02:30,870 --> 00:02:34,080 Cat bash history just contains an exit. 52 00:02:34,080 --> 00:02:35,280 Now, what does that mean? 53 00:02:35,280 --> 00:02:40,280 That means that apparently that I never ever logged out 54 00:02:40,980 --> 00:02:44,640 allowing me to synchronize the history, which brings me 55 00:02:44,640 --> 00:02:46,530 to the last command. 56 00:02:46,530 --> 00:02:50,247 And then the last command I can see when I, 57 00:02:50,247 --> 00:02:51,633 when I logged in. 58 00:02:53,832 --> 00:02:54,665 So what do we see? 59 00:02:54,665 --> 00:02:58,590 We see one session that's open since yesterday 60 00:02:58,590 --> 00:03:00,090 At tty2 61 00:03:00,090 --> 00:03:01,980 And then we see another session that's open 62 00:03:01,980 --> 00:03:05,190 since this morning also at tty2. 63 00:03:05,190 --> 00:03:07,140 I am not really sure what is happening 64 00:03:07,140 --> 00:03:11,910 but all I am sure about is that I wanna use history-W. 65 00:03:11,910 --> 00:03:15,060 Normally if you exit a shell decently, 66 00:03:15,060 --> 00:03:16,650 history will be updated. 67 00:03:16,650 --> 00:03:19,500 If you wanna make sure that history is updated, 68 00:03:19,500 --> 00:03:22,140 use history-W. 69 00:03:22,140 --> 00:03:24,990 And now if I use my LS minus L again, look at that. 70 00:03:24,990 --> 00:03:28,110 We have a file with four kilobytes of data. 71 00:03:28,110 --> 00:03:29,973 That is more what I was looking for. 72 00:03:30,930 --> 00:03:33,240 Good. Let's forget about this file 73 00:03:33,240 --> 00:03:35,250 and let me do 74 00:03:35,250 --> 00:03:37,890 history command again. 75 00:03:37,890 --> 00:03:42,890 And I am going to remove well command number 205 before 76 00:03:43,320 --> 00:03:47,050 because honestly that was an error history 77 00:03:47,937 --> 00:03:50,220 minus minus D will delete 78 00:03:50,220 --> 00:03:52,353 command number 205. 79 00:03:53,850 --> 00:03:56,970 So now if I use my history again 80 00:03:56,970 --> 00:03:59,670 then we should see the cat command followed 81 00:03:59,670 --> 00:04:02,040 by the uptime command, but there is 82 00:04:02,040 --> 00:04:05,190 there is no longer the other command. 83 00:04:05,190 --> 00:04:08,340 So it has successfully been removed. 84 00:04:08,340 --> 00:04:10,680 I'm not going to demonstrate history minus C 85 00:04:10,680 --> 00:04:13,830 for the simple reason that I don't like wiping my history. 86 00:04:13,830 --> 00:04:17,160 But what I am going to demonstrate is Ctrl+R. 87 00:04:17,160 --> 00:04:20,850 Ctrl+R is the reverse-I-search, look at that. 88 00:04:20,850 --> 00:04:23,940 I typed an L and immediately, I see on my prompt 89 00:04:23,940 --> 00:04:28,020 the last command that has an L in there, then I type an I 90 00:04:28,020 --> 00:04:33,020 and immediately I see the last command that has L I N 91 00:04:33,090 --> 00:04:33,950 and so on. 92 00:04:33,950 --> 00:04:37,560 So if I type Linda, I get the last command as well. 93 00:04:37,560 --> 00:04:39,930 If this is not a command that I'm looking for 94 00:04:39,930 --> 00:04:44,550 I can use Ctrl+R. Ctrl+R repeats the last search. 95 00:04:44,550 --> 00:04:47,490 So it's going further backwards in history. 96 00:04:47,490 --> 00:04:51,060 And once I'm happy with the command, well 97 00:04:51,060 --> 00:04:54,843 then I can just press enter to repeat the command. 98 00:04:55,710 --> 00:04:57,000 Now, there are two other ways 99 00:04:57,000 --> 00:04:59,580 of using history that I want to show you. 100 00:04:59,580 --> 00:05:03,900 What do you think of exclamation mark H? 101 00:05:03,900 --> 00:05:06,390 This way of using history is kind of dangerous 102 00:05:06,390 --> 00:05:09,420 because you need to be pretty sure about what you are doing. 103 00:05:09,420 --> 00:05:12,450 Exclamation mark H is repeating the last command 104 00:05:12,450 --> 00:05:15,660 from history that started with an H. 105 00:05:15,660 --> 00:05:19,230 And as you can see, that's the history command itself. 106 00:05:19,230 --> 00:05:20,850 We don't really see anything. 107 00:05:20,850 --> 00:05:23,250 Let's do exclamation mark G 108 00:05:23,250 --> 00:05:24,893 for the grapple, Linda etc passwd. 109 00:05:26,190 --> 00:05:27,090 And there we go. 110 00:05:27,090 --> 00:05:30,360 You can see that it's actually repeating that command. 111 00:05:30,360 --> 00:05:34,570 What you might also like is a command like 112 00:05:35,940 --> 00:05:39,720 exclamation mark, 1 79, that allows you to 113 00:05:39,720 --> 00:05:43,380 repeat a command from history by its number. 114 00:05:43,380 --> 00:05:44,550 And there we go. 115 00:05:44,550 --> 00:05:46,893 I'm repeating command 1 79, 116 00:05:47,890 --> 00:05:49,377 as you can see 117 00:05:49,377 --> 00:05:52,590 the command itself is repeated right here. 118 00:05:52,590 --> 00:05:55,050 And the output of the command one line lower 119 00:05:55,050 --> 00:05:56,460 is printed as well. 120 00:05:56,460 --> 00:05:58,350 And that's how you can use history. 121 00:05:58,350 --> 00:06:01,300 One of the best features that is offered by the bash shell.