Linux create directory chain

recursive mkdir

funny, I was looking at that page but totally overlooked «parent» because I was thinking of them as children (left to right).

Personally, I overlooked «parent» in the man page as well because, well, I feel like the flag should be «-r» for «recursive» — or at the very least, there should be an alias for such since mkdir has only 6 documented flags in total. Hell, I feel like the command should create directories recursively by default, and if you don’t want this then you should have to specify so explicitly.

4 Answers 4

$ mkdir -p foo/bar/zoo/andsoforth 

Parameter p stands for ‘parents’.

Using mkdir -p is a simple way for most modern OSes:

mkdir -p foo/bar/zoo/andsoforth 

However, mkdir -p is not recommended in many manuals. Read documentation for of GNU make and autoconf about problems with using mkdir -p :

The cross platform installation and configuration systems have their own safe alternatives for mkdir -p .

CMake to use in shell command line:

 cmake -E make_directory foo/bar/zoo/andsoforth 

Autoconf to use in script with preprocessing:

AS_MKDIR_P(foo/bar/zoo/andsoforth) 
AC_PROG_MKDIR_P(foo/bar/zoo/andsoforth) 

But these solutions require cmake or autoconf ( M4 ) tools to be installed (and possible preprocessing)

You can use also install-sh script with -d option:

install-sh -d foo/bar/zoo/andsoforth 

This script is used by autoconf and automake project. I think it must be the safest solution.

At the time I was searching for a cross platform solution for standard /bin/sh without dependences, but haven’t found one. Therefore I wrote the next script that may be not ideal, but I think it is compliant to most cross platform requirements:

#! /bin/sh cdirname() # cross platform alternative for 'dirname' < # $1 - path test $# -eq 1 || < echo "Procedure 'cdirname' must have only one parameter. Scripting error."; exit 1; >echo "$1" | sed -n -e '1p' | sed -e 's#//*#/#g' -e 's#\(.\)/$#\1#' -e 's#^[^/]*$#.#' -e 's#\(.\)/[^/]*$#\1#' - > mkd() # cross platform alternative for 'mkdir -p' < # $1 - directory to create test $# -eq 1 || < echo "Function 'mkd' can create only one directory (with it's parent directories)."; exit 1; >test -d "$1" && return 0 test -d "$(cdirname "$1")" || < mkd "$(cdirname "$1")" || return 1; >test -d "$1" || < mkdir "$1" || return 1; >return 0 > 

This script can be used for old systems, where option -p for mkdir is absent.

Читайте также:  Загрузочная флешка linux winsetupfromusb

sed -based cross platform version of dirname was added to the code. It works with a way similar to dirname (correct with path / , paths with base name only, paths with trailing / , paths with and without trailing \n s). This function can’t work correct if the path has newlines or some invalid characters for current locale. It also replaces any combination of / ( // , /// ) with single /

Changed line mkdir «$1» || return 1 to test -d «$1» || < mkdir "$1" || return 1; >because mkdir terminates with error if path exists and this check is needed for paths containing constructions like aaa\. (If aaa doesn’t exist previous version creates aaa and then tries to create it again).

This version of mkd doesn’t generate an error if path already exist (but it still has the possibility to generate such an error in parallel execution) and can’t get several directories in command line.

Источник

Making Nested Directories in Linux

Learn to make nested directories so that if a directory in the given path doesn’t exist, it is created automatically.

Creating nested directories when the parent directory does not exist is. impossible. But that also does not mean that you need to create the parent directory first and invoke mkdir again.

There is a faster way to achieve this; you can use the -p flag with mkdir command.

mkdir -p parent_dir/child_dir/nested_child_dir

Using the -p flag will let mkdir know that it is okay to make a parent directory if it does not exist yet, and then create a nested child directory.

mkdir syntax

The method for creating directories in UNIX-like systems is to use the mkdir command.

Below is the syntax of mkdir:

Create nested directories with the ‘-p’ flag

Just as an example, I want to create a directory structure where the parent directory’s name is ‘songs’ and the child directory’s name is ‘artists.’

The typical way to achieve this is by the following set of commands:

$ mkdir -v songs mkdir: created directory 'songs' $ mkdir -v songs/artists mkdir: created directory 'songs/artists' # I only included the '-v' flag for verbosity, you need not use it

This can be achieved in a single line like so:

$ mkdir -p -v songs/artists mkdir: created directory 'songs' mkdir: created directory 'songs/artists'

But. you said, «it is impossible.» Technically, you can not create a child directory if the parent does not exist. Yes, that still holds true.

So, let us understand what happened here.

The mkdir command has a ‘-p’ flag, and its descriptor is «no error if existing, make parent directories as needed». The second part of that statement says that mkdir will create a parent directory if the need be.

Читайте также:  Linux nano сохранить изменения

If you look at the output of mkdir in the second example, you will notice that mkdir created the ‘songs’ directory first and then created ‘artists’ under the ‘songs’ directory.

This indicates that mkdir will first make the necessary parent directories and then the child directory if you use the ‘-p’ flag.

Create multiple child directories under a not-yet-existing parent directory

Let’s try to create multiple child directories when the parent directory does not exist.

There are two ways to achieve this. Have a look:

$ mkdir -vp songs/artists songs/albums songs/genres songs/genres/classical songs/genres/pop mkdir: created directory 'songs' mkdir: created directory 'songs/artists' mkdir: created directory 'songs/albums' mkdir: created directory 'songs/genres' mkdir: created directory 'songs/genres/classical' mkdir: created directory 'songs/genres/pop'

Or, you can use the following command:

$ mkdir -vp songs/> mkdir: created directory 'songs' mkdir: created directory 'songs/artists' mkdir: created directory 'songs/albums' mkdir: created directory 'songs/genres' mkdir: created directory 'songs/genres/classical' mkdir: created directory 'songs/genres/pop'

As you can see, both commands have the same output, but the latter one is faster to type.

The ‘<>‘ curly brackets are used to specify a list, in this case, a list of directory names.

In this example, the parent directory ‘songs’ does not exist, so it was created. Then, the child directories ‘artists’, ‘albums’, and ‘genres’ are created. The directories ‘classical’ and ‘pop’ are listed under genres, so these two are the child directory of ‘genres,’ and they are created as I expected.

Conclusion

You can learn more about the mkdir command in the article below.

So, now you know how you can create a child directory when a parent directory does not exist — without creating each directory manually.

Источник

Recursive mkdir Command In Linux

Recursive mkdir Command In Linux

The mkdir command is used to create folders or directories in the Linux operating system. By default, one-level directories or folders can be created and child folders cannot be created with a single command. But to make things easier and faster the mkdir command provides the -p option in order to create multiple parent and child directories in a single command execution even they do not exist previously.

Create Multiple Child Directories Recursively

The -p option is used to create multiple child directories with mkdir command in a recursive manner. In order to create directories recursively non of the specified directories should exist because all of them are created automatically. In the following example, we create the directories aaa , bbb and ccc . The aaa is the most upper parent of the bbb directory. The bbb is the child of the aaa and parent of the ccc.

Читайте также:  Lineage 2 linux mint

Create Multiple Child Directories Recursively with Absolute Path

The mkdir command can be used to create directories recursively by specifying the absolute path or complete path. Providing the absolute or complete path is safer and more reliable as there is no room for error about the path. In the following example we create directories recursively in the home directory of the user ismail .

$ mkdir -p /home/ismail/aaa/bbb/ccc

Alternatively the tilda can be used to express the current user home directory.

List Recusively Created Directories

The recursively created directories can be listed with the ls command in a recursive way. The -R option is used to list all child directories for the specified path. In this case we specify the parent directory “aaa” and list all child directories recusively with the following command.

Create Multiple Child Directories with Brace Expansion

The bash provides the brace expansion which can be used to create multiple same-level children by using the recursive mkdir command. It may seem a little bit complex but very useful. The brace expansion simply specified inside the curly brackets < and >.

Lets list these multiple directories with the recursive ls command.

Источник

mkdir recursively create directories in linux/unix

Creating directories recursively can help us greatly improve efficiency during development.

In linux/unix, we can use the mkdir command to recursively create directories and subdirectories in batches.

There are two methods for mkdir to recursively create directories:

mkdir -p dir/subdir/a/b mkdir -p dir/subdir/a

Use mkdir -p option

If you want to create multiple nested directories at once, we can use the following command:

Obviously, the command will create a tree directory. If some of these directories exist, it is said that they already exist, but other directories do not exist, then mkdir will simply pass them over without error, and create directories under it.

The above command can only create nested directories vertically, so how do we create a vertical and horizontal directory structure?

Use mkdir -p option plus brace expansion

Use brace expansion to create a bunch of directories that follow a single pattern.

For example, when you type mkdir and press Enter, it will create three numbered directories in the current directory.

With the -p option, you can create vertical and horizontal subdirectories in the current directory. For example, mkdir -p dir/ will create a subdirectory dir and three subdirectories of the dir directory 1, 2, 3.

Источник

Оцените статью
Adblock
detector