Skip to content

Novice 5: Working with directories

Creating a Directory

If we want to make a new directory (e.g. to store some work related data), we can use the mkdir command:

[learner ~]$ mkdir learning_linux
[learner ~]$ ls
directory1 directory2 file1 learning_linux

You can create sub-directories at the same time as the parent:

mkdir -p learning_linux/parent/subdir

Note

The parent directory must either already exist or use mkdir -p to make parent directories as needed.

Deleting a Directory

We now have a few empty directories that we should remove. To do this use the rmdir command which will only remove empty directories so it is quite safe to use. If you want to know more about this command (or any Linux command), remember that you can look at its man page.

[learner ~]$ rmdir directory1
[learner ~]$ ls
directory2  file1  learning_linux

Note

You have to be outside a directory before you can remove it with rmdir.

As we will not be using the 'parent' or 'subdir' directories in this tutorial, you may delete them. See rmdir -p for how to remove a directory and all of its ancestors in a single command.

Renaming a Directory

Renaming directories can be achieved using the mv (move) command. This command is also used to move and rename files in addition to directories. See the novice 11 tutorial for how to use mv in the context of file location.

[learner ~]$ mv directory2 directory1
[learner ~]$ ls
directory1  file1  learning_linux