2.5.4. Working with directories#
Working with directories is quite similar as to working with files. This section will discus the basic commands.
2.5.4.1. Making directories#
Like files, you will also want to create and remove directories.
Creating directories is done with the mkdir (for make directory) command.
For instance, to create a directory somedirectory/ in the current working directory, use
$ mkdir somedirectory
It is also allowed to write the new directory name with a trailing slash /.
This is not really necessary, but it can make it more clear for people inspecting the commands that this is intended to create a directory,
$ mkdir somedirectory/
We can also create a directory by specifying an absolute path, so that the directory is not created relative to the current working directory
$ mkdir ~/anotherdir/
Of course, the name of the directory you want to create (here: anotherdir) should not exist yet,
but the parent directory (here your home, ~) does need to exist.
This also applies to relative paths, for example the following won’t work
$ mkdir anotherdir/a/b/c/d/e/f/
since this instructs mkdir to create a directory f/ inside a non-existing parent directory anotherdir/a/b/c/d/e/.
Exercise 2.69
While, mkdir by default only makes a directory if its parent directory already exist,
it does have a handy -p switch which will make it create all non-existing parent directories too, if it can.
This is useful to quickly create a simple nested directory structure.
Create the following nested directories anotherdir/a/b/c/d/e/f/ using a single mkdir command.
If all went well, find anotherdir/ should list all subdirectories.
2.5.4.2. Renaming and moving a directory#
Renaming a directory is done with mv, just as you do with files.
We can rename the directory using mv, giving the old and then the intended new name,
$ mv anotherdir testdir
Note that the content of the directory is unchanged, which you can confirm with find testdir/
If the second argument is an already existing directory, mv will not rename the directory in the first argument,
but move it into that second directory.
So, assuming you created somedirectory/ before, this moves testdir into somedirectory/,
$ mv testdir somedirectory/
Finally, it is even possible to move and rename in one go,
$ mv somedirectory/testdir mytestfiles/
Exercise 2.70
Move the f/ directory that you created in Exercise 2.69 directly into the anotherdir/ directory,
such that the e/ directory has no more subdirectories.
Then, move the anotherdir/a/ directory into anothdir/f/.
You should now have the following nested directories: anotherdir/f/a/b/c/d/e/.
Confirm this with the find command.
2.5.4.3. Removing directories#
The safest way to remove a directory is done with rmdir, for example:
$ rmdir somedirectory
which will remove the subdirectory somedirectory/ in the current working directory,
but only if the directory is completely empty.
Of course, you can also use an absolute path to indicate the directory to remove.
For instance, if your home directory contains an empty subdirectory tmp then you can delete
irrespective of your current working directory by specifying
$ rmdir ~/tmp
If the directory is not empty, the directory will not be deleted and an error will be displayed in the terminal.
Assuming mytestfiles/ still has a bunch of subdirectories, the following will not work:
$ rmdir mytestfiles/
This is a useful safety feature of rmdir, because perhaps there are still hidden files
that you forget to backup and remove before deleting the directory.
Ideally, you run rmdir once you believe it is completely empty, which helps prevent unintended deletions.
If you’re really sure that you want to remove a possibly non-empty directory
and all its contents (including all its subdirectories!),
then you can also remove it recursively using the rm (not rmdir)
command with the -r (recursive) option:
$ rm -r mytestfiles/
Warning
Be careful with rm -r, because it will not ask you for a confirmation in most cases,
and once the files are deleted, they cannot be recovered!
Double check that you specified the correct directory before pressing Enter to execute the command.
Exercise 2.71
Create a directory newdir. Now copy myfile.txt into it. Try to remove the directory with rmdir. If that doesn’t work, try to remove everything at once using rm -r. Be careful!
Exercise 2.72
Create a backup directory and store copies of all text files in your home directory there (ending in .txt).