mkdir if not exists
To create new directories if they do not exist and ignore the command if they do (no error message) use:
$ mkdir -pThe -p flag also allows for subdirectories to be created, if they do not already exist. For example:
$ mkdir -p foo/bar/testwill create the folder “test” within the folder “bar” within the folder “foo”.
When should I use mkdir vs mkdir -p?
mkdir -p can handle creating subdirectories in one command. An additional feature of adding the -p flag is the lack of an error message when the directory already exists. For example, to create subdirectories using mkdir you would need to do:
$ mkdir foo #create the folder “foo”
$ cd foo #navigate into the folder “foo”
$ mkdir bar #create the folder “bar” inside the folder “foo”adding the -p flag allows you to create the subdirectory bar inside the directory foo (even if foo doesn’t exist yet) in one line:
$ mkdir -p foo/bar #creates the folder foo and the folder bar inside fooUsing mkdir -p to ignore a directory if it already exists
Without -p, mkdir has this behavior:
$ mkdir foo #create foo
$ mkdir foo #foo exists now, command errors outwill return the error, “File exists”. But, when used in conjunction with the -p flag, the command to create a new directory is instead ignored, with no error returned:
$ mkdir -p foo #create foo
$ mkdir -p foo #foo exists now, command is ignoredSo mkdir -p is useful if you want to make subdirectories quickly, and when using the mkdir command in situations where you want the command to be ignored (no error) if the directory already exists – for example, when using mkdir in combination with other commands to create efficient pipelines for data writing and storage.
Create directory if missing while using cp
mkdir -p can be useful in combination with other commands like cp (copy). Combining the mkdir and cp commands is a powerful way to create directories that don’t exist and copy them over all in one go:
$ mkdir -p && cpwhere the && operator combines the two commands, and the cp command stands for “copy”.
Related articles
Bash Comments
Comments will help make your scripts more readable
Reading User Input
Via command line arguments and prompting users for input
Curl Post Request
Use cURL to send data to a server
Upload Files With curl
Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.
How To Copy A Directory In Linux
Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.
Create Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
How to Check the Size of Folders in Linux
Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.
Count Files in Linux
Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.
List Open Ports in Linux
Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.
Format Command Output In Linux
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.
Create Directories Recursively With mkdir
Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.
Remover Users in Linux
Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.