1 00:00:00,000 --> 00:00:02,100 [No audio] 2 00:00:02,100 --> 00:00:06,300 File Display commands. This lesson covers about 3 00:00:06,570 --> 00:00:09,570 all the commands that are used to display the 4 00:00:09,570 --> 00:00:13,500 contents of a file. You can view the file by doing 5 00:00:13,530 --> 00:00:16,410 ls -ltr, but and you could, how can you 6 00:00:16,410 --> 00:00:19,710 view inside of the file. The first command that 7 00:00:19,710 --> 00:00:24,330 we'll cover is the cat, which views the entire 8 00:00:24,330 --> 00:00:27,330 content of a file regardless whether it's one 9 00:00:27,330 --> 00:00:31,920 page or two page. more command or less command, 10 00:00:31,950 --> 00:00:36,240 both are the same. But more views the content of a 11 00:00:36,240 --> 00:00:40,320 file one page at a time, whereas less command 12 00:00:40,650 --> 00:00:45,810 views the contents of a page in reverse order, but 13 00:00:45,810 --> 00:00:49,620 it also allows you to view one line at a time. 14 00:00:50,640 --> 00:00:53,250 Then you have head. If you want to know a few 15 00:00:53,250 --> 00:00:57,600 lines, at the top of a file, let's say if a file has 16 00:00:57,600 --> 00:01:00,120 million lines, and you just wanted to view the 17 00:01:00,120 --> 00:01:02,880 first 10 lines, you could use head command, 18 00:01:03,060 --> 00:01:05,700 followed by the number of lines that you want to 19 00:01:05,700 --> 00:01:08,820 view. The same way the tail works, instead of the 20 00:01:08,820 --> 00:01:12,540 top of the file, it actually gets you the lines at 21 00:01:12,540 --> 00:01:16,237 the bottom of the file. Again, if a file has a million lines, 22 00:01:16,237 --> 00:01:17,580 and you put down minus 10, it 23 00:01:17,580 --> 00:01:22,050 will give you the last 10 lines of a file. Let's 24 00:01:22,050 --> 00:01:25,770 look at our our Linux machine and see how we could 25 00:01:25,770 --> 00:01:27,071 use these commands. 26 00:01:27,071 --> 00:01:29,824 [No audio] 27 00:01:29,824 --> 00:01:32,670 Okay, so let's clear this page 28 00:01:32,670 --> 00:01:36,960 out. Okay, the first thing I want you to do is 29 00:01:36,960 --> 00:01:47,130 copy the file called var/log/msgs, and copy it 30 00:01:47,130 --> 00:01:50,130 in your home directory. You could do slash home, 31 00:01:50,670 --> 00:01:53,550 your home directory, which would work or you could 32 00:01:53,550 --> 00:01:57,810 also do copy the name or the location of the 33 00:01:57,810 --> 00:02:02,490 entire file and followed by space and dot, dot 34 00:02:02,490 --> 00:02:06,870 means at this location where I am, you hit enter. 35 00:02:07,440 --> 00:02:11,910 And now you do ls -ltr, hit enter and you 36 00:02:11,910 --> 00:02:17,700 will see at the bottom right here, it shows that 37 00:02:17,880 --> 00:02:25,920 you have this file, which is 129338 bytes long. By 38 00:02:25,920 --> 00:02:28,800 the way, you could view, you could copy this file 39 00:02:28,800 --> 00:02:32,820 only if you are root. So when you do whoami, you 40 00:02:32,820 --> 00:02:35,850 are root. And you remember how you will become 41 00:02:35,850 --> 00:02:39,300 root. Let's say if you exit out of it, and you do 42 00:02:39,300 --> 00:02:42,270 whoami, you are yourself. And now you are going 43 00:02:42,270 --> 00:02:47,070 to copy var/log/msgs to your current location. 44 00:02:47,250 --> 00:02:49,230 It's not going to allow you so you have to be root. 45 00:02:49,230 --> 00:02:54,060 So to become root, su -, put in the 46 00:02:54,060 --> 00:03:01,410 password. And now you are in root. So go to your 47 00:03:01,410 --> 00:03:03,810 home directory, the user's home directory, which 48 00:03:03,810 --> 00:03:07,110 is your home directory. And then you'll see here 49 00:03:07,440 --> 00:03:11,260 this has this messages file that you copied from /var/log. 50 00:03:11,260 --> 00:03:13,680 And the reason you were not able to copy 51 00:03:13,680 --> 00:03:17,580 it because you see here, only root which is the user, 52 00:03:17,580 --> 00:03:20,760 the owner of the file who is root is allowed to 53 00:03:20,760 --> 00:03:25,590 read and write to this file. The rest of people 54 00:03:25,590 --> 00:03:29,670 which is group has no right for anything, and so 55 00:03:29,670 --> 00:03:33,000 on. The others have no right on read write to do 56 00:03:33,090 --> 00:03:36,420 anything. Let's go back to our commands that we 57 00:03:36,420 --> 00:03:41,520 will cover. If I wanted to view the contents of 58 00:03:41,520 --> 00:03:44,940 this file called messages, the command to use is 59 00:03:44,940 --> 00:03:50,580 cat messages. Simple as that, you hit Enter, see it 60 00:03:50,580 --> 00:03:54,090 has all those contents, messages, basically this 61 00:03:54,090 --> 00:03:57,720 messages file has all your logging information of 62 00:03:57,720 --> 00:04:00,660 your system. Anything that is going on with your 63 00:04:00,660 --> 00:04:04,830 system, any services that are running any error 64 00:04:04,830 --> 00:04:07,410 messages, warning messages, coming in the health of 65 00:04:07,410 --> 00:04:10,650 your computer, everything is stored in this file. 66 00:04:11,250 --> 00:04:13,950 We copied this file in our home directory 67 00:04:13,950 --> 00:04:18,300 just to cover the file display command lesson. So 68 00:04:18,360 --> 00:04:22,079 do clear, and then do cat and messages again, and 69 00:04:22,079 --> 00:04:25,860 you'll see it has a bunch of lines, text lines in 70 00:04:25,860 --> 00:04:28,920 it, and we could view through cat, but then we have 71 00:04:28,920 --> 00:04:32,100 to scroll up. So another way so that we don't have 72 00:04:32,100 --> 00:04:35,940 to scroll up, how do you do, you do more messages. 73 00:04:36,420 --> 00:04:40,110 You hit enter, and there you will see, you will get 74 00:04:40,350 --> 00:04:45,030 one page at a time of that file messages. Here 75 00:04:45,030 --> 00:04:47,190 you'll see it says 1% at the bottom, you hit 76 00:04:47,190 --> 00:04:50,580 spacebar to keep going down, keep going down, you're 77 00:04:50,580 --> 00:04:53,250 going one page at a time. And while you're going 78 00:04:53,250 --> 00:04:55,950 it also telling you the percentage of the file you 79 00:04:55,950 --> 00:04:59,670 have read. You keep going down, and if you want to 80 00:04:59,670 --> 00:05:02,580 get your prompt back, what do you do, you simply type Q. 81 00:05:03,420 --> 00:05:06,960 So that's how you use the more command. Now let's 82 00:05:06,960 --> 00:05:11,520 use the less command. less command space messages, 83 00:05:11,730 --> 00:05:14,820 hit enter, it does the same thing, you hit the 84 00:05:14,820 --> 00:05:17,880 spacebar, hit the spacebar one page at a time. 85 00:05:18,450 --> 00:05:22,770 Also, it will give you one line at a time to view 86 00:05:22,770 --> 00:05:27,480 it, when you hit j in your keyboard, it will come 87 00:05:27,480 --> 00:05:32,520 one line at a time, or you hit k, which is a go 88 00:05:32,520 --> 00:05:37,650 back up. You could also do up arrow key or down 89 00:05:37,650 --> 00:05:40,920 arrow key, whichever that you prefer j or k, up arrow 90 00:05:40,920 --> 00:05:43,620 key, down arrow key, whichever you prefer. So that's 91 00:05:43,620 --> 00:05:47,070 what less allows you to do. Next one, we have 92 00:05:47,070 --> 00:05:51,690 head. If you do more, and messages, you'll see the 93 00:05:51,690 --> 00:05:56,280 first page of your file messages. What if you want 94 00:05:56,280 --> 00:06:01,050 to see only the first two lines, you don't care 95 00:06:01,050 --> 00:06:04,080 what's inside the file after the first two lines. 96 00:06:04,470 --> 00:06:09,090 Then what do you do, you do head -2 space, 97 00:06:09,240 --> 00:06:12,450 the name of the file, messages, and you will see 98 00:06:12,450 --> 00:06:18,660 the first 2 lines of the file. Then you'll do 99 00:06:18,810 --> 00:06:23,070 tail -2 messages. This will give you the 100 00:06:23,070 --> 00:06:28,410 last two lines of the file, which is the last who 101 00:06:28,410 --> 00:06:34,530 is March 7th, 3:10. You could do cat again to 102 00:06:34,530 --> 00:06:38,010 messages and we'll bring you down to the last 103 00:06:38,400 --> 00:06:41,610 page, and you can verify yes, these were the last 104 00:06:41,610 --> 00:06:46,350 ones. So to wrap up, we covered cat command which 105 00:06:46,350 --> 00:06:49,050 lists the contents of your file regardless how 106 00:06:49,050 --> 00:06:51,330 many pages it has, this just displays everything 107 00:06:51,540 --> 00:06:54,900 in one shot. more gives you one page at a time. less 108 00:06:54,900 --> 00:06:57,540 give you one page at a time in reverse order but 109 00:06:57,540 --> 00:07:01,440 also lets you view one line at a time. head 110 00:07:01,440 --> 00:07:05,310 gives you the first lines depending on what you 111 00:07:05,310 --> 00:07:08,070 define, and tail gives you the last lines depending on 112 00:07:08,070 --> 00:07:13,110 what you have defined. Now exit out of root 113 00:07:13,110 --> 00:07:18,120 account, find out which directory you are in, and 114 00:07:18,120 --> 00:07:22,710 do ls -ltr. And you will see there is a file 115 00:07:22,710 --> 00:07:27,750 called jerry in there. Let's do cat and jerry. And 116 00:07:27,750 --> 00:07:31,500 you will see two lines in jerry file. The first 117 00:07:31,500 --> 00:07:33,990 line says, Jerry seinfeld is the main character, and 118 00:07:33,990 --> 00:07:37,320 the second line shows us the date and time of 119 00:07:37,530 --> 00:07:42,270 file, not the file but an output or a text 120 00:07:42,270 --> 00:07:44,970 message, whatever that is. If I just wanted to 121 00:07:44,970 --> 00:07:47,820 view the first line of the file, I don't care the 122 00:07:47,820 --> 00:07:52,740 second line, what I would do is head -1 123 00:07:53,040 --> 00:07:57,630 jerry and it will give me the first file first 124 00:07:57,630 --> 00:08:00,360 line of the file. What if I want to view the last 125 00:08:00,360 --> 00:08:02,400 line, I don't care the first one that says Jerry 126 00:08:02,400 --> 00:08:04,830 seinfeld is the main character. I just wanted to know 127 00:08:05,070 --> 00:08:13,440 this line. I will do tail -1 jerry, and it will give me 128 00:08:14,130 --> 00:08:17,066 the last line. So this is how head and tail work.