In this article, we will show you how to work with directory in Linux. We can create a directory using mkdir command. To delete a directory use rmdir command.

Current Working Directory

To find the current working directory, use pwd command as shown below.

pwd

What the pwd does is printing the PWD environment variable. You will get the same output if you type:

echo $PWD

Creating Directory

mkdir command will create a new directory, provided it doesn’t exists. The syntax for the mkdir command is as follows:

mkdir [OPTION] [DIRECTORY]

In the following example we are creating a new directory test.

mkdir test

To create a new directory at a given location, you’ll need to use the parent directory’s absolute or relative file path. For example, to create a new directory in the /tmp directory you would type:

mkdir -v /tmp/test

The -v (–verbose) option tells mkdir to print a message for each created directory.

Create Parent Directories

A parent directory is a directory that is above another directory in the directory tree. Let’s say you want to create a directory /local/mnt/dev/test. If any of the parent directories don’t exist, below comamnd will throw an error.

mkdir /local/mnt/dev/test

To create parent directories, use the -p option as shown below

mkdir -p /local/mnt/dev/test

When the -p option is used, the command creates the directory only if it doesn’t exist. If you try to create a directory that already exists and the -p option is not provided, mkdir will throw error.

The mkdir command also allows you to create multiple directory tree with one command:

mkdir -p local/mnt/{t1/t2,t3,t4,t5/{t6,t7,t8},t9/t10/t11}

$ tree
.
├── t1
│   └── t2
├── t3
├── t4
├── t5
│   ├── t6
│   ├── t7
│   └── t8
└── t9
    └── t10
        └── t11

11 directories, 0 files

Set Permissions

To create a directory with specific permissions, invoke the mkdir commanf with the -m (-mode) option. The syntax for assigning permissions is the same as with the chmod command. In the following example, we’re creating a new directory with 700 permissions, which means that only the user who created the directory will be able to access it:

mkdir -m 700 test

When the -m option is not used, the newly created directories usually have either 775 or 755 permissions.

Deleting Directory

rmdir command will delete a directory, provided it exists. The syntax for the rmdir command is as follows:

rmdir [OPTION]… DIRECTORY]

This command will remove the test directory located in the ~/Documents path only if it is empty. If the directory is not empty, the command will fail.

rmdir -v ~/Documents/test

To see what the rmdir command is doing in the background, use the -v option as shown above.

Remove Multiple Directories

To remove multiple empty directories, type rmdir followed by the directory names or path to directories as follows:

rmdir ~/Documents/myfiles images

This command will remove the ~/Documents/myfiles and images directories only if they are empty. The rmdir command also allows removing the directories along with their parent directories, use the -p option for this.

Recursive Delete

Following command deletes a directory recursively along with its content. Use -r option for recursive delete.

rm -r directory

In the following example we are forcefully deleting a directory example along with its content.

rm -rf directory

-f option is used to force delete.