mkdir
The mkdir command in Linux is a commonly used command in the command-line interface, and its primary function is to create new directories. The name mkdir stands for "make directory". Understanding the usage of mkdir is essential for file system organization and management in Linux. Here are the key aspects and functionalities of the mkdir command:
Basic Directory Creation: To create a new directory, you simply use
mkdirfollowed by the name of the directory you want to create. For example,mkdir new_directorywill create a directory namednew_directory.Creating Multiple Directories:
mkdircan create multiple directories at once. For instance,mkdir dir1 dir2 dir3will create three new directories nameddir1,dir2, anddir3.Creating Nested Directories: The
-p(parents) option allows you to create a directory along with any necessary parent directories. For example,mkdir -p dir1/dir2/dir3will createdir3and also createdir1anddir2if they don't already exist. This is useful for setting up a nested directory structure in one command.Setting Permissions: When creating a directory, you can set its permissions using the
-moption followed by the permission set. For example,mkdir -m 755 new_directorycreates a directory with read, write, and execute permissions for the owner, and read and execute permissions for the group and others.Checking for Errors: If
mkdirencounters an error (like trying to create a directory that already exists), it will display an error message. This behavior can be suppressed using the-poption, which will not show an error if the directory already exists.Verbose Mode: Using
mkdirwith the-v(verbose) option will make it display a message for each directory it creates. This can be useful for debugging or confirming the actions of a script.Use in Scripts:
mkdiris frequently used in shell scripts to ensure the required directory structure is in place for the script's operations.
Last updated