Command Line Basics: Creating and Removing Files and Folders (aka Directories)
4 min read
You are an amazing computer genius. You can navigate around your computer using only the command line and if you change the Terminal text colour to ‘Matrix Green’ then your non-technical friends will ‘ooh’ and ‘ahh’ in wonder. Congratulations!
Now comes the fun part: adding and removing files and folders. By the end of this post, you should know how to use the following commands:
$ touch $ rm $ mkdir $ rmdir
Before we begin, it’s important that you understand that what we commonly know as ‘folders’ are usually termed ‘directories’. I will mostly use the term ‘directory’ but if I slip up and use the word ‘folder’, please understand that they are identical.
Creating a new directory
To practice our amazing new skills, we’re going to start by creating a new directory in which we’ll work. If your Terminal isn’t open yet, whack Ctrl-Alt-T and then crack on my typing the following:
~$ mkdir MyTest
Congratulations! We’ve now created a new folder called ‘MyTest’ in our Home Directory.
$ mkdir is an abbreviation of ‘make directory’
Please don’t get too emotionally invested in our new baby, though, because at the end we’re going to remove it to show how all-powerful we are.
Creating a new file in the Terminal
Next, we’re going to make our index.html homepage in our test directory.
$ cd MyTest $ touch index.html
You. Are. Amazing. With two swift commands, you’ve entered the folder and created a new file called index.html. Now, you may have noticed that when you did this, the Terminal didn’t say or do anything. This might concern you, but trust me that file is there. Don’t trust me? Well fine, let’s check using the ‘list’ command we learned last time!
Tada! The Terminal should now be showing you the contents of the MyTest directory (since you’re there right now), and there is our little homepage, index.html.
Removing files using the Command Line
You know… I thought I wanted to make index.html in this folder, but I’ve decided I’d rather work with PHP so this HTML file is useless to me. Let’s remove it using the
$ rm index.html $ ls $ touch index.php $ ls
It’s gone! Simple as that, and we now have a sparkling new PHP file. Keep in mind that we’re only running the
$ls command so we can see the changes being made: it's not actually crucial to adding/removing files in Terminal.
Removing directories in the BASH shell
Before we go any further, I’d like to make a new directory within 'MyTest' which contains all the images for the MyTest website we’re creating.
$ mkdir images $ ls
Nice! Now we have a file called ‘index.php’ and a folder called ‘images’, all in the MyTest file. But y’know… now that I think about it I’ve decided to create a text-only website, so I really don’t need the images folder after all. Let’s remove it by running the $rmdir (remove directory) command:
$ rmdir images $ ls
Amazing! It’s gone! Whew, I really didn’t want to deal with a spooky ghost folder hanging around my website structure.
Now, here’s an important note:
$rmdironly works with empty directories.
Removing Directories with Items Inside
I have some bad news. Our funding has been cut and our project has been cancelled. Luckily, we were in a very early stage of development so we didn’t put much time in yet. As such, I don’t see any point in saving our work. Let’s just delete it. Let’s go to the Home Directory and try to remove the MyTest folder:
$ cd $ rmdir MyTest
Ruh-roh, we got an error:
rmdir: failed to remove 'MyTest/': Directory not empty
You might be thinking “Well that’s a bit ridiculous. Of course folders are going to have files in them!” but don’t get too judgey Dr McJudgerson: the Terminal is not like a regular GUI file manager: once a file is deleted, it is gone. It isn’t in some Recycle Bin or Trash or anything like that… it’s GONE.
BASH wants to ensure you don’t accidentally delete your grandma’s chicken soup recipe while trying to clean up your desktop, so it makes you use a slightly different formula. Try this:
$rm -r MyTest
-r flag stands for ‘recursive’, and it basically means that BASH is going to remove not only the directory you specify, but also everything within it. If you now run
$ls you will see that our beloved project is no more, and we wait with blinking cursor for the next underfunded passion piece.
$touch filename.ext to create a file
$rm filename.ext to remove a file
$mkdir directoryname to create a directory
$rmdir directoryname to remove a directory
$rm -r directoryname to remove a directory and all files/folders within it