O'Reilly logo

Linux Shell Scripting Cookbook by Sarath Lakshman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Making directories for a long path

There are circumstances when we are required to make a tree of empty directories. If some intermediate directories exist in the given path, it will also have to incorporate checks to see whether the directory exists or not. It will make the code larger and inefficient. Let's see the use case and the recipe to solve the issue.

Getting ready

mkdir is the command for creating directories. For example:

$ mkdir dirpath

If the directory already exists, it will return a "File exists" error message, as follows:

mkdir: cannot create directory `dir_name': File exists 

You are given a directory path (/home/slynux/test/hello/child). The directory /home/slynux already exist. We need to create rest of the directories (/home/slynux/test ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required