Making directory in linux

mkdir command: Create New Directories in Linux

mkdir is one of the essential Linux commands that every Linux user should know. You can create new directories using mkdir.

One of the essential Linux commands is mkdir. The mkdir allows you to make new directories (folders in common term) in Linux.

In this beginner series, you’ll learn to use the mkdir command.

mkdir command examples

The mkdir command is one of the rare few Linux commands that doesn’t have tons of options. And that makes it really simple to use.

mkdir [option] directory_name_or_path

Create new directories

To make a new directory, use mkdir command without any option:

This will create a new directory named new_dir in the present directory. You can check it using the ls command.

[email protected]:~/tuts$ ls [email protected]:~/tuts$ mkdir new_dir [email protected]:~/tuts$ ls -l total 4 drwxrwxr-x 2 abhishek abhishek 4096 May 14 16:15 new_dir

You may also specify the path to where you want to create the new directory.

[email protected]:~/tuts$ ls new_dir [email protected]:~/tuts$ mkdir new_dir/another_new_dir [email protected]:~/tuts$ tree . └── new_dir └── another_new_dir 2 directories, 0 files

Create multiple directories

You may also create several new directories with a single command:

mkdir new_dir_1 new_dir_2 new_dir_3

All the new directories are created at the same level. You may also create nested directories which is described in the next section.

Create nested directories

You can use the option -p to create a nested directory structure. If the parent directory doesn’t exist, it will create it for you.

This is particularly helpful when you want to create a directory structure or if you want to make sure that the directory path exists.

This is what the above command created:

[email protected]:~/tuts$ mkdir -p dir1/dir2/dir3/dir4 [email protected]:~/tuts$ tree . ├── dir1 │ └── dir2 │ └── dir3 │ └── dir4 └── new_dir └── another_new_dir 6 directories, 0 files

You may also use the -p option with a single directory. It won’t create a new directory that already exists, but it won’t throw any errors as well:

[email protected]:~/linuxhandbook$ mkdir new_dir mkdir: cannot create directory ‘new_dir’: File exists [email protected]:~/linuxhandbook$ mkdir -p new_dir [email protected]:~/linuxhandbook$ ls -l total 8 drwxrwxr-x 3 abhishek abhishek 4096 May 14 16:39 dir1 drwxrwxr-x 3 abhishek abhishek 4096 May 14 16:16 new_dir

Create directory with specific permissions

By default, your shell’s umask controls the permission on the newly created directories. If you want different file permissions on the directory, instead of creating the directory first and then changing the permission with the chmod command, you can use the -m option.

Читайте также:  Поставить линукс виртуал бокс

Suppose you want permission 766 on the directory you are going to create. You can use:

mkdir -m 766 new_directory

That’s pretty much what you need to know about the mkdir command. Now that you know how to create directories, maybe you would want to learn about the rmdir command and deleting directories in Linux command line.

Источник

Terminal Basics #2: Making Directories in Linux Terminal

Learn to make new folders in the Linux command line in this part of the Terminal Basics tutorial series.

In the previous chapter of the Terminal Basics series, you learned about changing folders in the Linux command line. I gave an exercise at the end that briefly mentioned making directories. In this part of the series, I’ll discuss how you can make new folders in the Linux command line using the mkdir command.

Making a new directory in Linux

You should be familiar with the concept of absolute and relative paths in Linux by now. If not, please refer to this tutorial. Open the terminal on your system if it is not already opened. Normally, you start with your home directory (/home/username). But for the sake of this tutorial and to recall a couple of things, I presume you are not in your home directory. So, change to your home directory first.

Yes. If you simply enter cd without any options and arguments, it takes you to your home directory. You could also use cd ~ among other methods. Here, make a new directory called practice.

Great! Now you have a dedicated folder where you’ll practice the Linux command line tutorials in this series.

Creating multiple new directories

You just created a new directory. What if you have to create more than one? Let’s say three of them. You may use the mkdir command three times in a row for each of them. It will work. However, it is not really needed. You can save time and effort by creating multiple directories at the same time like this:

Create multiple new directories in Linux with mkdir command

Go on and do that please. You can list the contents of the practice directory to see all the newly created directories. More on the ls command later.

Making multiple nested subdirectories

So, you now know about creating multiple directories at once. But what if you have to create a nested directory structure? Let’s say that you have to create a directory subdir2 inside subdir1 inside dir1.

The problem here is that subdir1 does not exist. So if you try `mkdir dir1/subdir1/subdir32, you’ll get an error:

[email protected]:~/practice$ mkdir dir1/subdir1/subdir2 mkdir: cannot create directory ‘dir1/subdir1/subdir2’: No such file or directory

If you didn’t know better, you would go for mkdir dir1/subdir1 and then run mkdir dir1/subdir2 . That will work. However, there is a much better way. You use the -p option, which makes parent directories if needed. If you run the command below:

mkdir -p dir1/subdir1/subdir2

There is no naming convention, but it is better to avoid spaces in file and directory names. Use underscore or dash instead because handling spaces in file/directory names requires special effort.

Читайте также:  Настройка фтп сервера линукс

Test your knowledge

  • Without entering the dir2 directory, create two new subdirectories in it.
  • Without entering the dir3 directory, create two-level nested subdirectories (subdir1/subdir2)
  • Change to the dir2 directory. From here, create a directory named temp_stuff in your home directory. Don’t worry; we will delete it later in this tutorial series.
  • Go back to the parent practice directory and try to create a directory named dir3 . You see an error. Can you make it go away with the -p option?

In the next chapter of the Terminal Basics series, you’ll learn about listing the contents of a directory with the ls command.

Do let me know if you have questions or suggestions.

Источник

Mkdir Command – Create New Directory in Linux

A directory is a location to store files and subdirectories. It is a file system object found in Linux, MS-DOS, OS/2, and Unix operating systems. The directory is also called folder is a friendly name more familiar to Windows users.

In this tutorial, we learn about mkdir command to create a new directory in Linux.

Prerequisites

  • A Computer installed with Linux/Unix.
  • Basic knowledge to use the command line.
  • An open mind to learn.

What is Mkdir command in Linux

Mkdir (make directory) is the command to create a new directory in Linux. It also allows you to create multiple directories at once me.

mkdir [option] directory-name

The following table shows mkdir options.

Options Description
-p, —parents Create parent directories along with the target directory in the specified path.
-v, —verbose Display a confirmation message when the directory is created.
-m, —mode Set permission modes for the new directory. if not available use chmod command.
—version Display the version number and exit.

Create a new Directory in Linux

To create a new directory in Linux type mkdir followed by the directory name:

If the directory already exists mkdir gives an error. The mkdir never overwrites a directory.

mkdir create directory

This creates the directory named «myprojects» in the current directory. Type ls command to view the directory created:

view the directory created

After creating the directory you can use the cd command to change the directory.

Instead of creating in the current directory, you can give absolute path as well. For example to create a new directory named «myprojects» in the directory /home/ubuntu/, type:

$ mkdir /home/ubuntu/myprojects

create a new directory using absolute path

Here we need to make sure all parent directories exist or else it gives an error.

Читайте также:  Linux bluetooth mouse how to

The ls -ld command can give additional information about the directory such as permission and last modification time. For timestamp information, you can use stat command.

To manipulate directories you can use mv to rename directories and cp to copying. You can use rmdir command to delete the directory. Only if the directory is empty and has write permission in its parent directory.

Note: To create a directory, the user should have write permission on the parent directory where you want to create the new directory. Or else need to use sudo with mkdir.

The mkdir command won’t give any output on the terminal on success. You can use -v option to display a confirmation message for the created directory.

mkdir -v command

The message «mkdir: created directory ‘dirname1» indicates that the directory was successfully created.

Mkdir command examples

Let’s look into some useful examples of mkdir command

Create Multiple Directories

To mkdir command we can pass multiple arguments separated by spaces to create multiple directories.

The following command creates 3 directories named dir1, dir2 and dir3

You may use curly braces <> to create multiple directories to specify a range or a list of values.

For example to create directories with a list of values:

You can see the dir1,dir2, and dir3 directories created.

Create directories using a numeric range

mkdir using < data-lazy-src=

You can use mkdir -m command to give a specific mode or permission to the new directory. This can be only set while creating it. Once the directory is created the only option to change mode is to use chmod command.

The following command mkdir command creates a new directory named dir7 with 777 mode

mkdir set mode for new directory

The default permission depends on umask value set on your system. The directory permission will be umask value subtracted from the default permissions (777). Most Linux distributions have an umask value 002 for the root and 002 for the regular user. When a regular user creates a new directory 755 will be default permission. Whereas as for root user it will be 775.

mkdir default directory permission

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

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