1
00:00:03,310 --> 00:00:08,130
Let's say we want to archive or store a user's home directory just in case we need it.

2
00:00:08,200 --> 00:00:11,290
One way to do that would be with a Taar command.

3
00:00:11,290 --> 00:00:15,170
The tar command originally stood for tape archive.

4
00:00:15,310 --> 00:00:21,060
But you can also use it to archive files to any storage device not just tapes.

5
00:00:21,250 --> 00:00:27,260
So we'll use type against TAR shows that it's a file on file system which is executable here in the

6
00:00:27,260 --> 00:00:28,490
user bin directory.

7
00:00:28,550 --> 00:00:31,580
And let's look at the man page for this command.

8
00:00:31,670 --> 00:00:36,100
I'm just going to move down here a little bit and look at these examples here.

9
00:00:36,380 --> 00:00:40,640
So these three examples are the three main uses of the tar command.

10
00:00:40,790 --> 00:00:46,070
Here you can see how to create an archive how to view the contents of an archive and how to extract

11
00:00:46,130 --> 00:00:48,130
the contents of an archive.

12
00:00:48,140 --> 00:00:54,350
So the dashi option is used to create an archive the dash f option allows you to specify the file name

13
00:00:54,380 --> 00:00:55,850
of the archive file.

14
00:00:56,030 --> 00:00:59,970
The Dashti option is to list the contents of an archive.

15
00:01:00,080 --> 00:01:05,540
The dash via option is for verbose and the dash x option is for extract.

16
00:01:05,540 --> 00:01:10,770
So let's try these commands out by the way if you want to get details on those of course.

17
00:01:10,910 --> 00:01:14,300
They are listed here and all the options in the man page.

18
00:01:14,300 --> 00:01:18,440
First let me create a directory in some files in there so we have something to play with and something

19
00:01:18,440 --> 00:01:19,820
to create an archive with.

20
00:01:19,820 --> 00:01:29,690
So let's say we have some cat videos Let's do this cat videos Admiral cat bar

21
00:01:32,360 --> 00:01:35,420
Let's call this one Tarth Paul.

22
00:01:35,610 --> 00:01:41,810
When I ask can we come up with here and let's do Luke Clocker.

23
00:01:42,310 --> 00:01:43,650
What or two more here.

24
00:01:43,650 --> 00:01:46,110
Obi Wan cat novae

25
00:01:48,980 --> 00:01:50,560
about had me.

26
00:01:50,990 --> 00:01:53,370
I'm a kitty cat him before here.

27
00:01:53,540 --> 00:01:57,980
So if we look at our cat videos director here we have some empty files that were created with the touch

28
00:01:57,980 --> 00:01:59,170
command.

29
00:01:59,180 --> 00:02:08,580
Now let's go ahead and create an archive of these cat videos Ashesi to create after specify a file will

30
00:02:08,580 --> 00:02:13,540
say cat videos dot Teej are since that is the convention.

31
00:02:13,560 --> 00:02:17,880
You don't have to do this but this is a good practice and they will give it a path to archive.

32
00:02:17,880 --> 00:02:23,390
So we're just going to archive cat videos that directory and all of its contents.

33
00:02:23,800 --> 00:02:26,930
So if I do ls here we have cat videos dot Taar.

34
00:02:27,030 --> 00:02:28,620
So the archive was created.

35
00:02:28,620 --> 00:02:30,660
Now let's look at the contents of that archive.

36
00:02:30,670 --> 00:02:37,250
Will the tar Dashti to list the contents of the archive and we have to give it a f. a file which is

37
00:02:37,260 --> 00:02:38,280
cat videos.

38
00:02:38,340 --> 00:02:39,130
They are.

39
00:02:39,330 --> 00:02:43,310
So that shows us the contents within that tar file.

40
00:02:43,470 --> 00:02:48,310
If you wanted to see the files as they were being added to the archive you can use the verbose option.

41
00:02:48,420 --> 00:02:52,060
So let's go ahead and just delete this tar file here now and do this.

42
00:02:52,080 --> 00:02:58,320
See verbose and then do cat videos TR and the cat videos directory.

43
00:02:58,320 --> 00:03:03,210
By the way you can leave off or leave on the directory separator.

44
00:03:03,210 --> 00:03:04,120
Either one is fine.

45
00:03:04,110 --> 00:03:05,710
I'm just going to leave it on for this time.

46
00:03:05,940 --> 00:03:12,030
So this time instead of silently creating our tar file the dash option allowed us to see what was being

47
00:03:12,060 --> 00:03:14,780
added to our archive as it was happening.

48
00:03:14,790 --> 00:03:17,800
Let's go ahead and restore the files from this archive somewhere.

49
00:03:17,790 --> 00:03:23,490
Let's go ahead just create a restore directory change into that directory and then what we're going

50
00:03:23,490 --> 00:03:27,270
to do is do an x for extract and then we got to give it a file.

51
00:03:27,390 --> 00:03:33,900
And of course dot dot is a parent directory of where we were just at and then Ford slashes a directory

52
00:03:33,900 --> 00:03:38,540
separator and then cat videos dot TAR is the name of the file.

53
00:03:38,550 --> 00:03:43,680
So again here Dot Dot forward slash I'm not doing anything that's necessarily related to tar.

54
00:03:43,680 --> 00:03:51,270
This is just a Linux convention of relative pathing how to get to a file that's in the parent directory

55
00:03:51,270 --> 00:03:52,870
of your current directory.

56
00:03:53,040 --> 00:03:58,920
By the way I could have used the full path I could use home vagrant cat video star but I'm not going

57
00:03:58,920 --> 00:03:59,730
to do that.

58
00:04:00,120 --> 00:04:06,300
OK so we have a command here dash X for extract dash for the file that we're going to use as our archive

59
00:04:06,300 --> 00:04:11,550
file which is not for cat video store and going to hit enter.

60
00:04:11,550 --> 00:04:17,450
So now if we look in this new directory here we have the cat videos directory created.

61
00:04:17,540 --> 00:04:23,490
And if we list the files in that cat videos directory we get all those files that were inside that archive.

62
00:04:23,820 --> 00:04:26,150
I'm just going to go ahead and remove all this right here.

63
00:04:29,130 --> 00:04:34,500
So this time let's use the verbose option to see exactly what we're extracting and this is typically

64
00:04:34,500 --> 00:04:35,440
what I like to do.

65
00:04:39,100 --> 00:04:43,810
OK this shows that it's created the directory and then put those several files within that directory.

66
00:04:43,820 --> 00:04:48,520
OK I don't need that directory right now so I'm just going to delete it and I'm going to type CD dot

67
00:04:48,520 --> 00:04:52,960
dot to move back up to the parent directory and I'm back in my home directory.

68
00:04:53,170 --> 00:04:58,300
One of the common things people do with archive's is actually compress them to save some disk space.

69
00:04:58,300 --> 00:05:01,290
Now this can be a two step process like so.

70
00:05:01,390 --> 00:05:04,410
Let me go ahead just remove the tar file that was there.

71
00:05:04,630 --> 00:05:08,540
Let's create a cat video star file.

72
00:05:09,100 --> 00:05:15,780
And now let's zip it.

73
00:05:15,890 --> 00:05:21,950
By the way the JE's up command compresses the original file and appends a dot g the to the end of the

74
00:05:21,950 --> 00:05:22,740
file name.

75
00:05:22,760 --> 00:05:28,140
So if you see a file that ends in Tarar GZ It's a compressed archive.

76
00:05:28,310 --> 00:05:31,390
Now to decompress or uncompress a zip file.

77
00:05:31,400 --> 00:05:37,920
Use the unzip.

78
00:05:37,930 --> 00:05:40,810
Now we have our uncompressed original tar file back.

79
00:05:40,810 --> 00:05:43,270
Let's go ahead and delete that.

80
00:05:43,330 --> 00:05:46,490
You can perform the compression on the archive when you create it.

81
00:05:46,630 --> 00:05:49,250
If you use the dash the option to TAR.

82
00:05:49,270 --> 00:05:55,790
So what we can do here is to tar Dasch Z for compress and see we're going to create an archive.

83
00:05:55,980 --> 00:05:59,680
Is going to be verbose EFIS we're going to give a file name

84
00:06:03,000 --> 00:06:10,830
and then a path to what we're going to put in our archive here we can see we have our cat videos at

85
00:06:10,880 --> 00:06:12,890
Target gz file.

86
00:06:12,980 --> 00:06:17,830
And if we want to view the contents of a compressed archive we also need the dash CSY option.

87
00:06:17,840 --> 00:06:23,340
So we'll do our dash z t to list verbose Ephor file.

88
00:06:23,420 --> 00:06:24,360
You know the drill.

89
00:06:24,410 --> 00:06:29,090
We'll give it the path and then we can see the contents of that archive without actually extracting

90
00:06:29,090 --> 00:06:29,270
it.

91
00:06:29,270 --> 00:06:31,700
So let me go ahead and just delete that right now.

92
00:06:31,700 --> 00:06:37,910
Sometimes people like to use the File Extension of dot P GZ to represent a compressed archive so we

93
00:06:37,910 --> 00:06:48,090
can do this Taar the CVF cat videos Dot T G Z and then give it a path.

94
00:06:48,130 --> 00:06:55,590
So it's just something to keep in mind that target Z and Dot T G Z are really the same thing.

95
00:06:55,600 --> 00:06:59,930
Let's go ahead and restore this compressed archive we'll go into our restored directory here.

96
00:07:00,160 --> 00:07:02,420
And there's nothing in our folder.

97
00:07:02,560 --> 00:07:08,500
So let's use the dash z option cause we're working with a compressed file and then provide the path

98
00:07:08,500 --> 00:07:09,070
to that file.

99
00:07:09,070 --> 00:07:11,700
Actually this time I'm going to use the full path

100
00:07:15,390 --> 00:07:15,730
OK.

101
00:07:15,770 --> 00:07:22,950
It displays what was extracted and if we look sure enough we see the contents of that archive has been

102
00:07:22,950 --> 00:07:24,810
extracted on disk.

103
00:07:24,960 --> 00:07:27,560
I want to point out something that's kind of important here.

104
00:07:27,600 --> 00:07:34,480
The tar command really like most Linux commands treat you like a knowledgeable sane responsible adult.

105
00:07:34,590 --> 00:07:37,860
So it will happily override anything in its way.

106
00:07:37,860 --> 00:07:40,300
It assumes you know what the heck you're doing.

107
00:07:40,500 --> 00:07:44,800
So let's add some text to a file just to change it up a little bit.

108
00:07:44,820 --> 00:07:51,070
So let's just put some text in here and to our cat videos file of Darth paw.

109
00:07:51,450 --> 00:07:58,440
And if we kept the contents of that file we'll see it says what we piped into it by the way exclamation

110
00:07:58,440 --> 00:08:04,440
mark dollar sign takes the very last argument on the previous command line and places it on the current

111
00:08:04,500 --> 00:08:05,370
command.

112
00:08:05,370 --> 00:08:12,390
So again we're using what's called event designators in bash and you can see that exclamation mark dollar

113
00:08:12,390 --> 00:08:17,070
sign expanded to the last thing on our previous command line here.

114
00:08:17,070 --> 00:08:18,590
Cat videos Darth paw.

115
00:08:18,760 --> 00:08:24,540
And then it displays the entire command it's going to be executed and then it displays the result of

116
00:08:24,540 --> 00:08:25,110
that command.

117
00:08:25,110 --> 00:08:27,590
Just another quick tip for you there.

118
00:08:27,600 --> 00:08:29,120
Anyway back to the original point.

119
00:08:29,120 --> 00:08:32,330
Let's go ahead and extract this archive again.

120
00:08:34,980 --> 00:08:41,250
Execute the most recent command that starts with C which is cat and if we can't get videos for it unless

121
00:08:41,320 --> 00:08:43,820
Darth paw nothing is returned.

122
00:08:44,010 --> 00:08:51,030
And if we look at a long listing of that file it now has a file length of 0 and so nothing is in it

123
00:08:51,030 --> 00:08:54,120
which is the original file or the father was in the archive.

124
00:08:54,120 --> 00:08:56,630
So again Taar there just overwrote the file.

125
00:08:56,640 --> 00:09:01,280
It didn't ask for a confirmation or anything like that it didn't give you a warning.

126
00:09:01,380 --> 00:09:03,340
It just went ahead and over of the file.

127
00:09:03,480 --> 00:09:08,520
So be very careful when you're extracting archives and just be conscious of what you're doing.

128
00:09:08,520 --> 00:09:13,490
This also goes without saying but Taar relies on file permissions like any other command does.

129
00:09:13,590 --> 00:09:17,960
So if you try to create an archive of files that you don't have permissions to.

130
00:09:17,970 --> 00:09:19,980
Well that's just not going to work.

131
00:09:19,980 --> 00:09:25,140
Also if you try to extract the contents of an archive to somewhere where you don't have permissions

132
00:09:25,140 --> 00:09:27,560
to that's not going to work either.

133
00:09:27,690 --> 00:09:32,180
So if you want to be sure you have permissions you suit you or become the user first.

134
00:09:32,190 --> 00:09:38,160
For example let's say you want to backup the contents of the forward slash ETSI directory that contains

135
00:09:38,190 --> 00:09:43,140
pretty much all of your configuration files on the system you'll need root privileges to do that because

136
00:09:43,140 --> 00:09:47,930
there are some files in NC that a normal user just does not have access to.

137
00:09:47,940 --> 00:09:55,800
So let's get back to our home directory and do suruh tar dashes Z Moli velt the verbose option see Alf

138
00:09:56,130 --> 00:10:05,050
recreate the see the T GZ file and will archive the contents of forward slash ETSI here like I just

139
00:10:05,050 --> 00:10:11,000
said I didn't use the verbose option mainly because I wanted you to see this particular message.

140
00:10:11,040 --> 00:10:15,640
TAR says I'm removing the leading forward slash from member names.

141
00:10:15,810 --> 00:10:20,400
If we look at the contents of the archive we'll see that there is no leading slashy there so do this

142
00:10:20,400 --> 00:10:29,580
tar ZDF at c TDC so we can see the last file there at CNN RC is not forward slash and C forward slash

143
00:10:29,610 --> 00:10:30,480
nano RC.

144
00:10:30,510 --> 00:10:33,800
It's just ETSI word slash nano RC.

145
00:10:33,840 --> 00:10:39,420
This allows you to extract the contents of the archive into a different location without overriding

146
00:10:39,450 --> 00:10:41,190
all the existing files.

147
00:10:41,190 --> 00:10:47,670
So if it preserves the leading slash forward slash and C for slice or C if you try to extract this archive

148
00:10:47,910 --> 00:10:49,490
then it would overwrite that file.

149
00:10:49,560 --> 00:10:51,750
And that may be not what you want to do.

150
00:10:51,750 --> 00:10:56,700
For example maybe you want to extract this into a different subdirectory and then compare the contents

151
00:10:56,760 --> 00:11:02,040
of that archive or that backup to what is currently on disk instead of just blowing right over and over

152
00:11:02,040 --> 00:11:03,630
writing it right away.

153
00:11:03,900 --> 00:11:09,450
So more or less here tar is going to remove the absolute pathing indicated by the leading forward slash.

154
00:11:09,450 --> 00:11:13,380
And it's just going to leave you with a relative path within your archive.

155
00:11:13,680 --> 00:11:17,280
There is one more thing I'd like to talk about with the tar command.

156
00:11:17,280 --> 00:11:21,890
Now you may have seen people leave off the leading hyphen and do things like this.

157
00:11:22,040 --> 00:11:27,370
Tarzi see CVF cat videos.

158
00:11:27,650 --> 00:11:31,090
TDC and then give a path.

159
00:11:31,130 --> 00:11:35,420
So this is an older type of syntax that is still supported to this very day.

160
00:11:35,510 --> 00:11:41,360
So I just want you to be aware of it in some documentation you might actually see mention of quote old

161
00:11:41,360 --> 00:11:42,420
options unquote.

162
00:11:42,560 --> 00:11:44,940
Well this is what they're talking about.

163
00:11:45,140 --> 00:11:51,950
If you use this old option syntax all the options must be written together as a single clomped set without

164
00:11:51,950 --> 00:11:55,530
spaces separating them or dashes preceding them.

165
00:11:55,550 --> 00:12:01,460
Also the set of letters must be the first to appear on the command line after tart's cell and then some

166
00:12:01,460 --> 00:12:02,690
whitespace.

167
00:12:02,690 --> 00:12:05,600
Old options cannot appear anywhere else.

168
00:12:05,600 --> 00:12:09,770
They must immediately followed tar with some whitespace.

169
00:12:09,770 --> 00:12:14,910
The letter of an old option is exactly the same letter as the corresponding short option.

170
00:12:14,930 --> 00:12:21,740
For example the old option t is the same as the short option Dashti when options require an argument.

171
00:12:21,740 --> 00:12:25,010
Those arguments have to be presented in the same order.

172
00:12:25,010 --> 00:12:28,800
This can get pretty confusing if you use multiple options and have an argument.

173
00:12:28,850 --> 00:12:31,560
So instead of worrying about how to do it correctly.

174
00:12:31,610 --> 00:12:35,980
Honestly my advice is to simply use a hyphen before your options.

175
00:12:35,990 --> 00:12:41,660
That way you can do things like this tar space Desch F and then provide whatever option you want to

176
00:12:41,660 --> 00:12:48,950
you after that command and then all your other options they can be Dessie dash V and so on and here's

177
00:12:48,950 --> 00:12:49,780
a path.

178
00:12:49,930 --> 00:12:55,970
And we do this and sure enough we get our archive created if we wanted to do it with the quote old style

179
00:12:55,970 --> 00:12:58,350
syntax we'd have to do this tar.

180
00:12:58,490 --> 00:13:02,740
See V.F. cat videos tar and then the path.

181
00:13:02,960 --> 00:13:04,220
So that works.

182
00:13:04,220 --> 00:13:11,720
So he couldn't do something like this for example tar Affe cat videos and then do see in the out here

183
00:13:11,960 --> 00:13:18,110
because one is going to try to do is interpret C and V as files that are to be added to the archive

184
00:13:18,350 --> 00:13:19,930
and not as options.

185
00:13:19,940 --> 00:13:24,710
So again it gets kind of confusing when this old syntax will just really treat tar like any other command

186
00:13:24,800 --> 00:13:30,620
or using dashes if some old grumpy Unix guy with suspenders and a beard yells at you for doing this

187
00:13:30,620 --> 00:13:33,960
we'll just tell him that I told you to do it and put the blame on me.
