Creating new folder linux

Linux: How to Make a Directory Command

H ow do I make directory under Linux operating systems using the command prompt or bash shell?

You need to use the mkdir command to create new folders or directories under Linux operating systems. A directory (also known as folder in MS-Windows/macOS ) is nothing but a container for other directories and files. This page explains the basics of using the mkdir command on Linux.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements mkdir on Linux
Est. reading time 3 mintues

mkdir command Syntax

The mkdir command has the following syntax:
mkdir dirname
mkdir dirname1 dirname2
mkdir [option] dieNameHere
mkdir -p dir1/dir2/dir3

Examples

Let us see some commann useful examples.

How to create a new director

Open a terminal and then use the mkdir command to create empty directories. The following command would create a directory called foo:
$ mkdir foo
To list directories, enter:
$ ls
$ ls -l
The following command would create two directories within the current directory:
$ mkdir tom jerry
$ ls -l

How to create Directories in Linux

The -p option allows you to create parent directories as needed (if parent do not already exits). For example, you can create the following directory structure:
$ mkdir -p

/public_html/images/trip
Verify it:
ls -l

/public_html/
ls -l

/public_html/images/
ls -R -l

How to create directories in Linux with verbose option

Pass the -v as follows to display a message for each created directory:
mkdir -v dir1
ls -l

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Setting up permissions when creating a directory

To set directory mode (permission) pass the -m option as follows:
mkdir -m dirName
The -m option is same as giving permissions using the chmod command. For examples:
mkdir data
chmod 0700 data
We can do the same with a single command and save typing time at the command-line:
mkdir -v -m 0700 data
ls -ld data

Setting up SELinux context with mkdir on RHEL or CentOS

The syntax is follows to set up system_u:object_r:httpd_sys_content_t:s0 as SELinux context for foo dir:

How to Create a Directory in Linux with mkdir Command with SELinux

Sample mkdir demo command

Animated gif 01: mkdir in action under Linux / Unix like operating systems

Summing up

The mkdir command in Linux is used to make new directories as per your needs. We create a new directory in current directory or given path:
mkdir my-dir-name-here
ls -l
Also make directories recursively which is useful for creating nested dirs on Linux. For instance:
mkdir -p path/to/dir1/dir2

Getting help

Make sure you read the following man pages:
man mkdir
man ls
mkdir —help

Options summary

Option Description Example
-m ( —mode=MODE ) Set file mode (as in chmod command), not a=rwx – umask. mkdir -m 0644 sales
-p ( —parents ) No error if existing, make parent directories as needed. mkdir -p one/two/three
-v ( —verbose ) Print a message for each created directory. mkdir -v detla
-Z Set SELinux security context of each created directory to the default type. mkdir -Z dir1
—context[=CTX] Like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX. See above
—help Display this help and exit. mkdir —help
—version output version information and exit. mkdir —version

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to create a directory or folder

There are multiple ways to create or add a folder, subfolder, directory, and subdirectory on a computer. The methods depend on the operating system or where the directory is being created. Select a link below for steps on how to create a directory and folder in each major operating system.

Creating a folder in Microsoft Windows

My Computer or Windows Explorer

  1. Open My Computer or Windows Explorer.
  2. Open the drive or folder where you’d like to create the new folder; for example, the C: drive. If you do not want to create a folder in the root directory, browse to the location of your choosing.
  3. In Windows 10 on the Home tab, click the New folder icon. In Windows 7 and earlier on the file menu bar, select File and then Folder.

In all versions of Windows, you can also right-click with your mouse on a blank portion of the folder, select New and then Folder.

If you’re using Windows 7 or earlier and do not see the file menu bar at the top of Windows Explorer, press the Alt key and it should become visible.

Windows desktop

  1. Navigate to the Windows desktop.
  2. Right-click any blank portion of the desktop.
  3. In the menu that appears (like that shown in the picture), click New and then Folder.
  4. A new folder appears. Type the name of the folder you want to use and then press Enter .

Create a new folder using a shortcut key

While in Windows Explorer, you can press Ctrl + Shift + N to create a new folder without using the mouse.

Windows command line

See the following MS-DOS and Windows command line user section for information about creating a directory in the Windows command line.

Creating a directory in MS-DOS and the Windows command line

It is more appropriate to use «directory» instead of «folder» when referring to the command line.

To create a directory in MS-DOS or the Windows command line (cmd), use the md or mkdir MS-DOS command. For example, below we are creating a new directory called «hope» in the current directory.

You can also create multiple new directories in the current directory with the md command. In the next example, we are creating three new directories, called «user1,» «user2,» and «user3,» in the current directory.

If you want to create a directory with spaces, you need to surround the directory name with quotes. In the example below, we are creating a directory called «my example directory» in the current directory.

To create a directory in the parent directory, without first moving into that directory, you can use the command below. This example moves back one directory to create the «example» directory.

To create a subdirectory in a different directory without moving it, use a command similar to the example below. This command creates a «test» directory in the hope directory.

Once a directory is created, you can use the cd command to change the directory and move into that directory.

To make a directory in another drive without moving into that drive, use a command similar to the example below. This command creates an «example» directory on the F: drive. The drive letter «f:» can be substituted from any drive letter.

Create a directory with a batch file

A batch file is a series of commands that can be entered in the command line. Therefore, you can use any of the examples from the section above in the batch file to create a new folder.

Creating a directory in Linux, Unix, and their variants

It is more appropriate to use «directory» instead of «folder» when in a command line.

You must have the permissions to create a directory outside of your home directory.

To create a directory in Linux, Unix, or any variant, use the mkdir Linux and Unix command. For example, below we are creating a new directory called hope in the current directory.

Once the directory is created, you can use the cd command to change the directory and move into that directory.

If you want to change the permissions of a directory after its been created, use the chmod command.

Creating a folder and directory in Microsoft Windows 3.X

File Manager

  1. Open File Manager
  2. Open the folder where you’d like to place the new folder and on the top menu of File Manager, select File and then new folder.

MS-DOS

See the above MS-DOS user section for information about creating a directory in MS-DOS.

Creating a folder in macOS X

Desktop folder

  1. Navigate to the macOS desktop.
  2. Right-click (tap two fingers on the mouse) any blank space on the desktop.
  3. Select New Folder from the drop-down menu that appears.
  4. Name the folder, and then press Return .

Create a folder in a directory

  1. Open Finder and navigate to the directory where you’d like to create the folder.
  2. Click File in the upper-left corner of the screen.
  3. Select New Folder in the drop-down menu that appears.
  4. Name the folder, and then press Return .

Users may also press the shortcut key combination Command + Shift + N to create a new folder.

To create a directory in the Terminal command line, see the Linux steps.

Источник

How to make a folder in Linux or Unix

How to make a folder in Linux

The procedure is as follows:

  1. Open the terminal application in Linux
  2. The mkdir command is is used to create new directories or folders.
  3. Say you need to create a folder name dir1 in Linux, type: mkdir dir1

Let us see examples and other usage in details. The syntax is:

Now you know the syntax. Let us explore how to create new folders and directories on Linux or Unix-like system using the command line option.

How to create a new folder named foo in Unix

Open the Terminal app and type the following command:
mkdir foo
To see directory listing use the ls command:
ls
ls -l
You can simultaneously create any number of folders/directories:
mkdir dir1 dir2 dir3 dir_4
Verify it:
ls -l

Fig.01: How to create Folders/Directories In Linux/Unix with the mkdir command

More on file mode

The entry type character describes (the first character d rwxr-xr-x ) the type of file, as follows:

  1. : Regular file.
  2. b : Block special file.
  3. c : Character special file.
  4. d : Directory.
  5. l : Symbolic link.
  6. p : FIFO.
  7. s : Socket.
  8. w : Whiteout.

So basically d character in above entry tell us that it is a directory/folder. The next three fields are three characters ach: owner permissions, group permissions, and other permissions. Each field has three character positions:

Источник

Create Folder in Linux Create Directory in Linux [mkdir Command]

Looking to create folder in Linux/create directory in Linux? Then this post will give you detailed insights on the mkdir command and to execute this process.

List of content you will read in this article:

Everyone who is a beginner or advanced Linux administrator somehow is looking to create folder in Linux/create directory in Linux.

Linux is widely adopted and used due to its stability and flexibility in carrying out various tasks in an easier way. One of the key features of Linux is the terminal where you can run any command to run various tasks from making changes to the system to download any required software.

In this article, we will be highlighting the commands with various options for creating directories or folders in the Linux system. Linux offers an easy command for that- mkdir command that stands for “make directory”. Also, the “mkdir” command will allow you to set permissions, create multiple directories using the single command, and do many other tasks.

Before you run the mkdir command, make sure to complete the prerequisites such as-

  • Linux or Unix-like OS.
  • Access to the command-line terminal for executing the commands.
  • The user should be having the right access to creating the directory.

mkdir command [Make Directory Command]

If you want to create a new directory, you can make use of the “mkdir” command for creating single or multiple directories within the Linux system. You can use this command along with various options for applying various functionality. The syntax can you can use for the “mkdir” command is as follows-

mkdir [option] dir_name

Make directory command options

Option / Syntax

Description

This command will create a directory within the current directory location

This command will help in creating multiple directories in the current directory location. Make sure that you do not use the spaces inside the <>

mkdir –p dir/path/new_dir

This command will allow you to create a directory structure with the missing parent directories (if there is any)

mkdir –m777 dir_name

This command will help in creating a directory and allow you to provide full read, write, execute permissions for every user

mkdir –v dir_name(s)

This command will help in creating a directory in the current location along with the details of the mkdir command.

How to Create Directory in Linux

To run the “mkdir” command, go to the terminal and open it with the admin access if you have, otherwise, you just need to have the right and appropriate access to execute the “mkdir” command.

Always, remember that the options in Linux are case-sensitive so use them correctly else the meaning will change. As per the below example, we have created a directory “Sam”.

mkdir Sam

This command will result in the creation of the directory in the current working directory. If the directory has been created successfully then you will get an empty line on the terminal. If you want to verify the result of the creation of the directory, you can use the “ls” command.

Creating multiple directories

If you want to create several directories in the same directory, you can use the “mkdir” for creating them separately. But it will take time to run individual commands. So to save your time by running separate commands, you can use a single “mkdir” command along with the directory names separated by a comma.

Consider the following example for creating multiple directories at once.

mkdir

There is no need to add space between the directory names in the brackets. But, if you add the space then the name will take an extra character.

Creating a parent directory

If you want to create a structure with multiple subdirectories, then you can use the “mkdir” command along with the “-p: option. Using this option will make sure that the missing parent directory will be added in the process.

Here, we are considering the example where we want to add the “dirtest2” directory within the “dirtest1” directory in the Linux directory, then we have to provide the complete path with the “mkdir” command as shown below.

mkdir –p Linux/dirtest1/dirtest2

Once you run the “mkdir” command with the complete path, then to confirm the creation of the directory, you can run the “ls” command along with the “-R” option. This option will help in showing the recursive directory tree that will display the contents of each directory present in the provided path.

But, if you miss the “-p” option, the terminal will display an error that the directory that you have provided does not exist. You can see the below example.

In the above example, the terminal will show an error that the dirtest3 does not exist rather than creating the parent directory.

Setting permission for directory while creating the directory

If you create any directory in the Linux system, then by default the directory will get the “rwx” permission but only for the user who created that directory. If you want to change the permission for the directory for all the users, then you can use the “-m” option along with the “mkdir” command.

In the below example, we are providing the permission to the directory as “777” that is any user will be able to read, write, or execute that file if required.

mkdir -m
mkdir -m 777 alpha
ls -l


If you want to check the details of the created directory along with their permissions, then you need to get the long listing of the directories. For this, you can run the “ls” command along with the “-l” option.

Verifying the directories

So far, you have noticed that after running the “mkdir” command, you will not get any feedback or the result showing if the creation is successful or not. If you want see the details of the “mkdir” command, then you can use the “-v” option with the “mkdir” command.

Considering the below example with the “-v” option, you will get the details of the command.

Now that you get the complete details of the creation of the directory, you do not have to run the “ls” command to creak the created directory.

How to Create Folder in Linux? [Step By Step Guide]

Follow the below-given steps to create a folder in Linux easily.

Step 1: In Linux, first open the terminal.

Step 2: then enter mkdir dir1 command to build a folder with the name dir1.

Let’s take a closer look at certain cases and other applications. The syntax to create a directory in Linux is as follows:

mkdir folder
mkdir [option] folderName
mkdir directory

By using the ls command to list the contents of the directory, you can confirm that it was created:

This will show the list of all the current directories.

mkdir’s -v (—verbose) option instructs it to print a message for each directory it creates.

mkdir: created directory ‘folder’

The file is generated in the current working directory when only the directory name is provided without the complete path. The latest working directory is the location where the commands are being executed. The cd command is used to modify the actual working directory.

To build a directory in a different region, you’ll need to specify the parent directory’s absolute or relative file direction. To construct a new directory in the /xyz directory, for example, type:

You’ll get a Permission denial error if you want to build a directory in a parent directory where the user doesn’t have enough permissions.

After executing the command you will get output like this:

mkdir: cannot create directory ‘/root/newdirectory’: Permission denied

You can create a parent folder using the following command:

mkdir -p dir11/dir12
mkdir -p parent1/child1
mkdir -p pictures1/memories1

This will list all the directories present in the parent and child folder, respectively to verify it you have to follow the below listed given command.

ls -l
ls -l pictures1

rmdir command/rm command to Remove the Directory/Folder in Linux

Here, we are removing a folder called directory1, so we will execute the below command:

rm -rf directory1

Conclusion

This is all about how to create folder in Linux by using mkdir command. With this guide, you will be able to understand the working of the “mkdir” command. You can use the “mkdir” command along with various options that we have mentioned and explained their usage, how they impact the working of the normal “mkdir” command and how it changes the output. Make sure to use the right option in the right scenario for getting the correct result. The options are case-sensitive, so make sure to use the right option else you will end up with the error

If you feel there is any other option available in Linux to create directories and folders, Please suggest with the comment box, We will try to include your suggestion.

Источник

Читайте также:  Install windows 10 from dvd drive
Оцените статью