The mkdir command in Linux: create directories

Introduction

In the Linux environment, the organization of files and folders is essential to maintain a clean and efficient system. One of the most basic and useful commands to achieve this task ismkdir, which allows to create directories quickly from the terminal. Although its operation seems simple,mkdiroffers several options that make it much more powerful than many users imagine. In this article we will explore your syntax, the most common options and some practical examples that will help you to make the most of it.

Basic syntax

The simplest way to usemkdiris:

mkdir name _ of _ directory

This command creates a directory with the name indicated in the current work directory. If the name contains spaces, it is necessary to lock it between quotes or escape the spaces with an inverted bar.

Common options

  • -p(parents): creates the necessary directory and all parent directories. For example,mkdir -p a/b/ccreatea, a/banda/b/cin one step.
  • -v(verbose): shows a message for each created directory, which is useful when creating several directories at a time or used with-p.
  • -m(mode): allows to set the new directory permissions in octal format, such asmkdir -m 750 datosto give reading, writing and execution to the owner, and reading and execution to the group.
  • -Z(context): in systems with SELinux, it sets the security context of the directory.

Practical examples

Imagine that you need to create a project structure for a web application. You can do it with a single line:

mkdir -p project / {css, js, img, including, logs}

This creates the directoryproyectoand within it the subfolderscss, js, img, includesandlogs. If any of them already exist,-pprevent the command from failing and simply continue.

Another useful case is to create a directory with specific permissions for a backup directory:

mkdir -m 770 backups

In this way, only the owner and the group have full access, while other users cannot read or write on it.

Tips and good practices

  • Always check that there is no directory with the same name before creating it, especially if you are going to use-pon deep routes, to avoid accidentally overwriting something.
  • Combinemkdirwith other commands likecdorcpin scripts to automate the preparation of working environments.
  • In installation scripts, use-pto ensure that the necessary routes exist before copying files.
  • Remember that directory names in Linux distinguish between capital letters and lower letters, soFotosandfotosThey're different.

Conclusion

The commandmkdiris an essential tool in any Linux user's toolbox. Its apparent simplicity hides great flexibility thanks to options such as-p, -vand-m. Dominating its use will allow you to create complex directories structures quickly and safely, improving your productivity both in the command line and in automation scripts. The next time you need to organize your files, remember that just a simplemkdirto put everything in place.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish