1 00:00:07,050 --> 00:00:10,200 - In this video, we are going to explore the CP command. 2 00:00:10,200 --> 00:00:11,910 CP is for copy. 3 00:00:11,910 --> 00:00:16,910 And you can use CP of somefile to somedir to copy a file. 4 00:00:18,030 --> 00:00:19,320 This is the basic structure, 5 00:00:19,320 --> 00:00:21,780 and I will show you exactly how that is working. 6 00:00:21,780 --> 00:00:24,180 Now, the thing in this basic structure, 7 00:00:24,180 --> 00:00:26,400 is what exactly is it going to do? 8 00:00:26,400 --> 00:00:29,940 Well, it's going to look for the file somefile in /dir. 9 00:00:29,940 --> 00:00:32,760 And then it creates it to /somedir. 10 00:00:32,760 --> 00:00:36,630 Now the thing is, if /somedir does not exist, 11 00:00:36,630 --> 00:00:40,620 it will copy the file into a file with the name somedir. 12 00:00:40,620 --> 00:00:43,470 Now that might not be what you want to happen. 13 00:00:43,470 --> 00:00:45,930 So if you want the command to fill 14 00:00:45,930 --> 00:00:47,820 if somedir doesn't exist, 15 00:00:47,820 --> 00:00:52,820 then you better use CP /dir/somefile to /somedir/ 16 00:00:53,040 --> 00:00:54,810 the slash behind it is telling you 17 00:00:54,810 --> 00:00:59,040 that you really need the destination to be a directory. 18 00:00:59,040 --> 00:01:01,170 And otherwise, it fills. 19 00:01:01,170 --> 00:01:04,080 You can use CP minus A to copy all files, 20 00:01:04,080 --> 00:01:05,940 including hidden files. 21 00:01:05,940 --> 00:01:10,323 But use .* wildcard to include hidden files as well. 22 00:01:11,280 --> 00:01:13,290 So what does that mean? 23 00:01:13,290 --> 00:01:15,487 Well, that means that CP-A ~/.* to TMP. 24 00:01:19,200 --> 00:01:23,100 is how you are going to copy hidden files. 25 00:01:23,100 --> 00:01:27,420 I can tell you that copying hidden files is complicated 26 00:01:27,420 --> 00:01:30,300 and maybe you better want to use the tar utility 27 00:01:30,300 --> 00:01:35,160 which is much better utility to copy hidden files also. 28 00:01:35,160 --> 00:01:38,940 And last thing is CP minus R, which will copy recursively. 29 00:01:38,940 --> 00:01:41,130 Notice that's an uppercase R. 30 00:01:41,130 --> 00:01:42,540 And what is recursively? 31 00:01:42,540 --> 00:01:45,303 Well, that means including subdirectories. 32 00:01:47,010 --> 00:01:48,873 Let me demonstrate. 33 00:01:51,630 --> 00:01:54,990 All right, let's go have a look at some CP demo. 34 00:01:54,990 --> 00:01:58,350 I'm using CP etc passWD. 35 00:01:58,350 --> 00:01:59,790 What is passWD? 36 00:01:59,790 --> 00:02:01,560 That's the user database. 37 00:02:01,560 --> 00:02:02,550 I'm pressing enter. 38 00:02:02,550 --> 00:02:03,570 What do we get? 39 00:02:03,570 --> 00:02:05,220 We get missing destination. 40 00:02:05,220 --> 00:02:08,310 Of course we do, because I did not specify 41 00:02:08,310 --> 00:02:11,160 where I want to copy this file too. 42 00:02:11,160 --> 00:02:13,170 I'm adding a dot to the command. 43 00:02:13,170 --> 00:02:14,790 Dot is the current directory. 44 00:02:14,790 --> 00:02:18,960 So copy etc passWD to the current directory. 45 00:02:18,960 --> 00:02:19,793 There we go. 46 00:02:19,793 --> 00:02:23,100 And LS minus L on passWD is showing 47 00:02:23,100 --> 00:02:27,030 that it has copied the file to the current directory. 48 00:02:27,030 --> 00:02:30,990 Of course, it can only copy files that we can read. 49 00:02:30,990 --> 00:02:35,430 If for instance, I would use etc shadow, 50 00:02:35,430 --> 00:02:37,320 which is a file that I can't read. 51 00:02:37,320 --> 00:02:39,630 I'm getting permission denied. 52 00:02:39,630 --> 00:02:41,310 All right, now there's something else 53 00:02:41,310 --> 00:02:42,750 that I want to show you. 54 00:02:42,750 --> 00:02:46,367 And that is CP, etc passWD. 55 00:02:48,390 --> 00:02:50,823 Why wouldn't I work on the same file name? 56 00:02:52,530 --> 00:02:54,030 To mydir. 57 00:02:54,030 --> 00:02:57,930 Mydir, well, I hope that mydir is a directory. 58 00:02:57,930 --> 00:03:00,510 And this command seems to be doing all right. 59 00:03:00,510 --> 00:03:04,230 But if I use LS minus LD on mydir, what do we see? 60 00:03:04,230 --> 00:03:06,840 We see it created a file with the name mydir. 61 00:03:06,840 --> 00:03:08,910 That's not exactly what I had in mind. 62 00:03:08,910 --> 00:03:12,420 So let me use RM to remove this file. 63 00:03:12,420 --> 00:03:17,040 And let me do it again with one small modification. 64 00:03:17,040 --> 00:03:20,580 CP etc passWD to mydir/ 65 00:03:20,580 --> 00:03:22,770 which means that it copies to a directory 66 00:03:22,770 --> 00:03:25,590 with the name mydir in the current directory. 67 00:03:25,590 --> 00:03:28,500 The slash is indicating that it must be a directory. 68 00:03:28,500 --> 00:03:30,840 And if the directory does not exist, 69 00:03:30,840 --> 00:03:32,310 it's giving me an error message. 70 00:03:32,310 --> 00:03:34,440 And that's exactly what I want. 71 00:03:34,440 --> 00:03:37,380 Now, if I do create a directory, I try it again. 72 00:03:37,380 --> 00:03:39,363 Then it is working out all right. 73 00:03:40,350 --> 00:03:44,370 Then we have this thing about hidden files. 74 00:03:44,370 --> 00:03:46,620 You know what, I'm just going to use mydir 75 00:03:46,620 --> 00:03:47,943 in order to do so. 76 00:03:49,740 --> 00:03:51,930 You know what, I'm just going to use mydir 77 00:03:51,930 --> 00:03:53,460 in order to demonstrate. 78 00:03:53,460 --> 00:03:57,990 I'm using CP minus A of ./.* to mydir 79 00:04:03,420 --> 00:04:05,040 which is doing what? 80 00:04:05,040 --> 00:04:06,810 Well, it's giving an error message 81 00:04:06,810 --> 00:04:10,230 because it's trying to copy things into itself. 82 00:04:10,230 --> 00:04:11,070 Please ignore that. 83 00:04:11,070 --> 00:04:13,140 Please ignore this error message as well. 84 00:04:13,140 --> 00:04:15,510 I don't even wanna talk about it right now. 85 00:04:15,510 --> 00:04:19,800 What I do wanna talk about is what it has done on mydir. 86 00:04:19,800 --> 00:04:23,070 And we can see it has copied everything to my dir. 87 00:04:23,070 --> 00:04:23,940 Everything? 88 00:04:23,940 --> 00:04:26,670 Well, that is something that we are only going to figure out 89 00:04:26,670 --> 00:04:29,880 if we include the minus A as well. 90 00:04:29,880 --> 00:04:33,510 Minus A is taking care of all files, 91 00:04:33,510 --> 00:04:35,523 including the hidden files. 92 00:04:36,750 --> 00:04:38,070 But that's in LS. 93 00:04:38,070 --> 00:04:43,070 Now in CP, the minus A needs the wildcard 94 00:04:43,140 --> 00:04:44,940 to be slightly different. 95 00:04:44,940 --> 00:04:49,500 Now in CP minus A, you also need to manipulate the wildcard. 96 00:04:49,500 --> 00:04:50,583 What does that mean? 97 00:04:51,630 --> 00:04:55,050 Let me create a new directory, take two. 98 00:04:55,050 --> 00:05:00,050 And let me use CP minus A ./* to take two. 99 00:05:01,050 --> 00:05:05,370 And let me do an LS minus A on take two. 100 00:05:05,370 --> 00:05:08,520 And there we go, no hidden files. 101 00:05:08,520 --> 00:05:11,160 So if you want to copy the hidden files, 102 00:05:11,160 --> 00:05:15,600 you really need to refer to .* instead of just star. 103 00:05:15,600 --> 00:05:17,400 And that's a little bit confusing. 104 00:05:17,400 --> 00:05:20,190 And that is why I told you you might be better off, 105 00:05:20,190 --> 00:05:24,150 if you really want to archive contents of a directory, 106 00:05:24,150 --> 00:05:27,750 to use the tar utility for the tape archiver. 107 00:05:27,750 --> 00:05:29,130 That's all about CP. 108 00:05:29,130 --> 00:05:30,033 Let's move on.