Complete Intro to Linux and the Command-Line Creating & Moving Files
Transcript from the "Creating & Moving Files" Lesson
>> All of these programs I'm showing you have so many flags and so many different ways you can use the tool. And I am showing you the 1% of the surfaces of these tools, right? So I'm sure if I go in here to make, let say help, there's a bunch of different things that I'm not showing you, right?
[00:00:20] So I showed you the -p1 cuz that's what I use a lot but I'm not showing you verbose mode, I'm not showing you all these different modes as well. So keep in mind that all these different tools have more power than what I'm showing you. So let's talk about touch.
[00:00:46] So this is everything I have in here in this directory. I'm gonna say touch my-new-file.txt. And what touch does is if the file doesn't exist, it creates it. So if I go in here and look now, you could see I have a my-new-file. And this was created at 13:42, right there, right?
[00:01:12] Now, If the file already exists, it updates its last modified time. So I don't know if it's been enough time, it hasn't. So, but if I waited another minute or two, and I did touch again on the same file, what it would do is, it would change the modified time.
[00:01:28] It doesn't do anything else to the file, it just updates the modified time. That can be useful sometimes, cuz you have scripts that go in and say, hey, if this file's been modified, it means it's time to run the script again. So you can kinda use that as like a time check, you'll see that a lot.
[00:01:43] So let's see if I can do that again and say, you can see right there, this is now 13:43, whereas up here, it was 13:42, right? So it changes that modified time. Okay, so that's touch. Now let's say we wanted to get rid of my-new-file, how would we do that?
[00:02:06] One of them is remove, so rm my new text file, like that. And now if you go in here and look, it's not there anymore. So that's what rm is, remove, be extremely careful with how you use rm because if you use rm in the incorrect way, you can delete your entire computer.
[00:02:33] Or you can at least delete Linux until it deletes something so critical that it can't run anymore. Let's say I wanted to remove my folder, if I say remove my new folder, it's gonna say it's a directory, I don't remove directories. So in order to remove a directory, you have to say rm-r my new folder.
[00:02:50] And that's how you remove it. But if I say make directory lol, and I touch a file in here, And I say rm-r now of lol, it's gonna, it didn't have a problem with that. Interesting, okay. So frequently it'll ask you, it cuz they're empty files. So anyway frequently you'll see f put on here as well, which stands for force.
[00:03:35] And it's basically saying, hey, I'm about to move a bunch of files and I don't want you to ask me about it, right? Otherwise it'll go through and it's like, are you sure you want to delete this file? Do you wanna delete this file? Do you wanna delete this file?
[00:03:47] And if you're trying to remove a node modules, which has like 10,000 files in it, you don't wanna be asked that 10,000 times. So that's why people will put the f in there. And the -r allows you to delete directories which stands for recursive delete. So a lot of times you'll see people just say rm-rf whatever they're trying to delete.
[00:04:08] I just want to again throw out, this is a scary command to run because unlike when you move things to like the recycle bin, if you make a mistake with then you can just pull it out of the recycle bin. When you rm something, it's gone. It's legitimately gone, you cannot recover from it.
[00:04:24] Just be aware that you are permanently deleting something. There is a package which I'll show you how to install packages later, called trash and a lot of people will switch to trash. And that way, it works more similar to how your operating system like MacOs or Windows works, where there is a temporary trash directory that is emptied every 30 days, or something like that.
[00:04:52] It's a good idea to install that when I remember to try and do that but yeah, just be aware you are permanently deleting things. And I'm just gonna throw out there rm -rf star, we'll talk about star here in a second. This is gonna delete everything, right? Or slash, right?
[00:05:19] Just be aware of, I haven't done that but there's plenty of funny stories out there of engineers that have accidentally run that and it starts deleting their entire computer. Linux is gonna do exactly what you tell it to do. If you tell it to delete everything, it's gonna be like, okay, and it'll go try and delete itself.
[00:05:35] So just be aware of that. Cool, So that's rm, that's another useful command for you to have in your toolbelt. And again there's a lot more stuff to rm than just what I've shown you, there's the interactive one. There's, yeah, there's a bunch of stuff. Cool, let's talk about copy.
[00:06:07] So I have txtfile.txt there. If I want a copy, I can say cp txtfile, so the source file to destination file. And now you'll see I have two files, txtfile.txt and destination file.txt, that's what copy does, is it copies for you, I use that one all the time.
[00:06:29] And by the same thing if I say copy What is it? R, -R hi to bye, right? Now, if I look inside of here, I have a bye directory and inside of that bye directory, Right, now I have a directory called bye with my name is Brian side of it cuz it copied everything inside of hi.
[00:07:02] So that -R for copy means recursive as well. So normally -R or either lowercase or uppercase R means recursive, it means to not only this file but every file inside of it. That's not always true, but it's kinda rule of thumb that people kind of adhere to. And let's talk about move.
[00:07:28] So there's mv, which stands for move and it that make sense that it's called move but for me, it's more rename. That's cuz I think of things more in terms of like I want to rename this from this thing to this other thing, and so you use mv for that.
[00:07:46] So I'm gonna move, let's say I'm gonna move bye to, Something else. And so now you'll notice that I don't have bye anymore, I've renamed the bye directory to something else. And this works just as well for files as well. So if I move destination file to another file, There you go.
[00:08:21] So mv, that's another one to keep in mind. And then let's get into tar. So frequently, you wanna move a bunch of files all at once, whether that's like uploading a file to the server or really anything that you would use a zip file for. Linux has a file format called tar, which is kind of similar to zip files.
[00:08:46] Zip files are inherently compressed, whereas a tar file is not necessarily inherently compressed. A tar file can just be a bunch of files kind of glob together that's meant to be extracted later. And yeah, Tar is one of those things that I always have to look up cuz I can never remember all the flags that I need for it.
[00:09:10] But let's just make a directory, call it folder1. I'm gonna go and type to folder1 and I'm going to make a bunch of files. So I'm gonna touch file1, file1.txt, file2.txt, file3.txt, file4.txt. That's the thing about touches, you can use them many times at once. Okay, so now I have multiple files in here, And let's say I have folder1 and txt file that I wanted to put into one file so that I can move them, right?
[00:09:57] I can move them from this computer to this computer or something like that. I can do tar -cf archive or whatever you wanna call it .tar, and I'm gonna add textfile.txt and I am gonna add folder1 to it as well. And now if you look in here, I have this archive.tar and it is 12 kilobytes and that's because I put three 4 kilobyte files into it, so it is one 12 kilobyte file.
[00:10:32] So I did see an f and that puts all the things into a single file. However, it's not compressed, right? So for the most part, when you put things into an archive, you want them to be compressed so it's smaller. The trade off being that you have to process it so it's small, but at this point, that's cheap.
[00:10:55] You always wanna compress your files. So if I do the same command, and I'm gonna put a z at the end of this, so cfz, and then at the end of the tar, I'm gonna put .gz, that stands for gzip. So sometimes, these flags are important and what order you get them as well.
[00:11:11] So in this particular case, I had to get the z flag first. But now, if I go look at, I have archive.tar, this one has not been compressed, 12 kilobytes, and then this one archive.tar.gz has been compressed, and you can see that it's 4 kilobytes. So in other words, as you can see here, I can never remember all the flags and what order they're supposed to go in for tar.
[00:11:37] So I always have to look this up. But now I have this tar file, and I can extract it whenever I want. So I'm gonna, oops, make a directory here, extracted, and I'm gonna move my archive.gz into extracted, okay? So now, I'm gonna go into extracted and I'm gonna say tar -xcf, the archive file and some folder.
[00:12:22] This part needs to be first. What went on with this? So I have my archive file, -c into destination, Mkdir destination. Okay, again, this is a tool that I always have to fight with cuz there's so many files and you have to get them all right, so I had to do tar -xzf, this is how you extract an archive, right?
[00:13:10] And you have to tell which archive to extract and then you have to do a -C to let you know a destination and the destination has to exist. If it doesn't exist, it will not create it for you. But now if you look inside the destination, you can see that I have the folder there, the text file and it's all there and ready to go.
[00:13:30] So again, why tars are useful. It's the same reason that you would ever use a zip file, whether that's to compress a bunch of files down into one archive that you can move. Or if you just need to zip something for some reason, and then you can use both the tar, Command to also extract these archives as well.
[00:13:50] So that's more or less what you need to do to interact with various different files. It's a lot of touching. It's a lot of cat. It's a lot of, I know, touching, you can touch all the files. There's a lot of, I'm not even gonna go there. But there's a lot of jokes that people like to make about various different command line tools and how to use them together, none of which I'm going to repeat.