Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we have seen how to create, move, rename and copy files, we are ready to see how to go about deleting files and directories. Removing is very simple. Before I show you how, let me also just make the caveat, this is not the same thing as in the Finder when you delete something and it moves into your trash. If your remember ls -la, our user folder, and you'll see that there's this special folder called Trash and that is where things go. That's what's actually down here. When we put files in the trash that's where they exist.
When we empty the trash, it empties them out. What happens when you delete a file in the Finder is it's actually moving it. It's not to empty the trash. That it actually deletes it. So there is a sort of buffer time where you can recover a file. Delete in Unix doesn't work that way. When you remove something, it is gone. It is gone right away. It doesn't move int to the trash. You can move things to your trash to if you want. No problem you can do that. You can move a file into the trash and it would sit there and wait until you actually have decided to empty your trash. But that's not what we are talking about here.
So let's just notice I am inside my unix_files folder in my user directory. Lets take a look at the files that are in there. Let's start by deleting this somefile.txt that we created a little while ago. To delete it, all we use is rm for remove, nice and simple. rm somefile.txt and now it's gone completely. So again, use it carefully. The file is completely gone, not recoverable, we can't get it back. So use that very wisely. What about with directories? Well let's try that. Let's try making a new directory.
We'll call it delete_me. Now we have made a directory and now let's try removing it. remove delete_me. It doesn't work. It comes and says ah, this is a directory. To remove directories, we have two options and there's a difference between them. The first is we can say rmdir. Just like we had make directory, now we have the reverse of that, remove directory. delete_me. Voila, deleted it. It's gone now. Now, the one thing about remove directory is it will only remove directories that are empty.
Let's try and remove directory on test1 copy that we created earlier. Directory not empty. So therefore, we have to go inside that directory and empty it out. Inside that directory, there may be more things and more things and more things. It could take a lot of emptying things out. In that case, what we have is actually the same thing as we had in copy recursive. Remember we had cp -R and that's how he created the copy of test1 to test1_copy. Well, we have the same thing remove, with the capital R, test1_copy.
So again, use this very carefully. It will remove everything that's in test1_copy recursively. So it doesn't care whether they are empty or not. It will remove non-empty directories. There it is. Now it's gone, take a look. Now test1_copy is gone too. So to review rm only removes files. rmdir only remove empty directories. It's up to us to go inside and empty them out, and there's no way to override that. There's no option you can pass in. rmdir is always safe that way.
That makes it good. It's a good choice to use most of the time. rm -R is really powerful. That's the one that will remove files and directories recursively and it doesn't care if the directories are empty by default. So just be careful with that one. That's the really powerful one. That's a great tool to have in your toolbox. But just be careful, as you are cutting away things. Make sure you don't accidentally cut away something you don't need to.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.