Manipulating Directories
Some of the most common tasks we tend to do in our computers are around manipulating directories, albeit with graphical user interfaces. The terminal also allows you to achieve this.
Creating Directories
To create a directory, you can use the command mkdir
, which stands for make directory, followed by the name you want to give the directory. Why don't you give it a try:
- Move to your home directory (~), using the command you've already learned.
- Create a directory called
projects
and another one calledmy-directory
. - Check it has been created, also from the terminal (hint: do you remember how to list content?)
Let's try to group all of the projects in this course inside this new projects folder.
Deleting Directories
Another easy command to remember is rmdir
, as in remove directory. Shall we try?
- Ensure you're in the right directory, which holds the previous 2 created.
- Run this new command, followed by the name of the directory you want to remove, which in this case is my-directory.
- Check if it still exists or not.
What about if a directory has its own content, like other subdirectories and files?
Whether you thought of this question or not, it would be good to clarify it.
Let's:
- Create a new directory called
test
. - Move inside that directory.
- Create a new subdirectory of test, which is called
test-subdirectory
. - Move back to your home folder
(~)
. - Try to remove the directory test.
What happens?
You should be prompted with an error that states:
rmdir: test: Directory not empty
As you might have guessed, rmdir only allows you to delete empty directories. In order to delete a directory which isn't empty, you'll have to use another command with a specific switch:
Try:
rm -r test
This command will recursively (-r
) remove (rm
) all of the files inside a directory, including the directory itself.
A couple of extra switches:
-i
, which stands for interactive, will always asks you to confirm the deletion of each file, which is useful so that you don't accidentally delete something you don't want to. Once you run the command asrm -ri directoryName
, it will prompt you a couple of questions around examining the files in the directory and its subdirectories, to which you can reply with "y", for yes, or "n", for no.-f
, which stands for force, will delete even protected files. With great power, comes great responsibility, so let's leave this one for now.