Tag Archives: Directory

Deleting a LINUX Directory with rmdir

To delete an empty directory named subdir2, use the following command:

I check first the directory, delete the directory when confirmed and verify the directory is deleted.

You can remove a directory in a different location by changing what is after rmdir. To remove a directory in /home/hadi/Music, if you already in your /home directory, you can use the following:

Or from anywhere using an absolute path, you can use this:

Or from anywhere using a path shortcut, you can use the following command:

The directory must be empty to be removed using rmdir. However, you can remove a directory with its contents by using rm.

Note: Anything that has been deleted using rmdir cannot easily recovery, so proceed carefully.

Creating a Linux Directory with mkdir

To create an empty directory called testdirectory within your current directory, use this command:

You can create a directory in a different location by changing what is after mkdir. To create a new directory in /home/hadi/music, if you are already in you /home directory, you can use the following:

Or from anywhere using an absolute path, you can use this:

Or from anywhere using a path shortcut, you can use the following command:

The -p option is valuable. It enables you to create a directory and its parent directories at the same time, if they do not already exist. This can be a real time saver. If the parent directories exist, the command works normally. For example, suppose you want to a new directory with two layers of subdirectories. In this example, music and testdirectory already exist, but subdir1 and subdir2 are to be created: