How to create a directory in Linux and other useful commands

linux directory

The Linux directory follows the Unix File Hierarchy Standard with some modifications.

Since the appearance of the first graphical operating system in the 60s, all computers work with the metaphor of the office. The files are called documents and are kept in folders. Those folders in turn are organized according to their content in other folders. Depending on the level of access, the user will have the possibility to modify or not its content.

In this post we will see how to create a directory in linux and work with it both using the terminal emulator (from now on the terminal) and the graphical terminal.

What do we call a directory in Linux?

Folders in Windows

While in Linux we talk about directories, on Windows we refer to file containers as folders.

Linux, macOS, and BSD derivatives all adopted the Unix file structure and terminology, while Windows went its own way.  We could say that the directory in these operating systems is the Windows folder with another name, but since UNIX is older, it is correct to say that the Windows folder is the renamed directory. The directory registers its content in an index making its location easier. This means that the Documents directory does not contain the documents that are displayed when you access it. What it saves is the actual location of those documents.

The Linux Directory Structure

Not just in naming, UNIX-inspired operating systems follow a different path than Windows. Also in the directory structure.  On Windows, the base device is identified by a capital letter followed by :\ while on Linux the base directory is identified as /

Like the other derivatives, Linux uses the File Hierarchy Standard or Filesystem Hierarchy Standard for its acronym in English, although it makes some modifications. In a Linux system we find the following directories:

  • /:  It is the root directory that contains all other directories. Even the files contained in other storage units are listed under the root directory.
  • /son of: Essential user binaries are stored here. That is, the programs that must be available for the operation of the system or for the first use.
  • /boot: This section lists the files necessary to start the system, such as the boot manager and the different versions of the kernel that were installed.
  • /CD ROM: Shows the files listed on a cd or dvd inserted in the drive.
  • /giant: This section lists connected devices as directories. In /dev there are two main types of devices, the so-called block devices are those that store or save data (disk drives), while the so-called character devices are those that transmit or transfer data (keyboard, mouse, connection ports). ).
  • /etc: Saves the configuration files of the entire system. They can be modified with a text editor having the corresponding permissions.
  • /home: There are as many /home folders as there are users on a system. It contains the data and configuration files of each one of them. In principle, each user can edit their /home directory, but. to edit others, you must become an administrator.
  • /lib:  To reduce programming time and disk space, programmers often turn to third-party programs to perform common tasks like saving or printing a file or displaying a menu. These programs are called libraries. This sector of the directory stores the libraries used by the programs stored in the /bin and /sbin directories.
  • /lost+found: In the event of a file system failure, damaged files will be listed in this section when the system is rebooted. In this way it will be possible to detect them and try to recover as much data as possible.
  • /half: A subdirectory is opened here for each external storage device that is connected to the computer.
  • /mt: File systems that are temporarily accessed are mounted at this location.
  • /opt:  It is the place where the files of the programs installed manually are usually stored.
  • /proc: Contains special files that represent system and process information.
  • /root: It is the /home directory of the admin user. For security reasons, it is not included in this folder with the rest of the users.
  • /run: Ideal location for applications to store temporary data without the risk of being accidentally deleted by third parties.
  • /sbin: Contains the binaries that the root user needs for system administration.
  • /selinux: SELinux is a security module for the Linux kernel. In this directory we find the special files it uses listed.
  • /srv: It is a directory used by certain services (such as a web server) to store and find the data you need in one place.
  • / Tmp: Files that applications need temporarily are saved here. They are removed when the system is rebooted.
  • /usr: User files and applications are gathered here and are not essential for system operation. Within this subdirectory there are /bin, /sbin, and /lib folders.
  • /was:  It is the subdirectory in which there are files in which the system writes information while it is being used.

How to create directories in Linux

mkdir manual

The command mkdir is the one used to create a directory in Linux

Before getting into the subject of this article, the creation of directories in Linux, We must stop at the subject of the types of users in Linux and their level of access depending on the permissions of the directories.

We can classify users according to the type of account in:

  • Root user account (Root): It is the highest hierarchy account within a Linux system. It is initially created automatically during installation, although some distributions assign many of these functions to the first normal user created and disable them. This account can perform any administrative work and access any place within the directory including the /home of other users.
  • Regular user account: It has moderate privileges and can only perform certain tasks and access a limited number of directories.
  • Service accounts: They are created by programs at the time of their installation in order to execute processes and functions.

To determine access to directories, Linux uses two parameters:

  • Permissions
  • Owner.

In Linux each of the directories and files has three types of people with access:

  • User: He is the creator of the file and is also called the owner.
  • Group:  When multiple users need access to a file it is easier to assign access privileges to a group and add users to it.
  • Others: Users who do not fall into either of the two categories.

With respect to each directory and file it is possible to assign three types of permissions to the different types of users:

  • Reading: This permission gives the user privileges to open and read a file. In the case of a directory, you will also have the ability to list the content.
  • Writing: In the case of files, it consists of privileges to be able to modify the content of a file, but not to move, rename or delete it. This can be done if you have write privileges for the directory.
  • Execution: This is used for manually downloaded programs that are self-contained (they don't need to interact with system libraries). Allow the app to run.

Use the terminal or do it graphically?

Graphical directory creation

In the address book / Home it is very easy to graphically create subdirectories, rename, move and delete them. If it is restricted access directories, things are a bit more complex, so it is recommended to do it from the terminal emulator.

Within the /home directory, creating a directory graphically has no major inconvenience. You just have to place the pointer inside the desired directory and with the right button choose the corresponding option and determine the name. In the properties section we can determine the access permissions from the Properties menu. Accessing protected directories graphically as an administrator user is more complex and varies by desktop. Therefore, in order not to lengthen the article unnecessarily, we are going to explain how to do it from the terminal.

The command to create a directory in Linux is mkdir, a shortening of the English phrase make a directory. Usage is very simple

mkdir <nombre_del_directorio>

For example, if you want to create the directory linux_addicts

mkdir linux_adictos

If what we want is to create several directories

mkdir <nombre1> <nombre2> <nombre3> 
...

We would be left with the following:

mkdir linux_adictos desde_linux ubuntulog

To verify that the directory was created

ls -l <nombre1>

Suppose we want to create a directory inside another with restricted access. Assuming we have the corresponding privileges we do

sudo /directorio_contenedor/nombre_de archivo.

It would be something like:

sudo mkdir/opt/linux_adictos

It may be the case that we are interested in creating a subdirectory and the directory that contains it at the same time. The structure of the command is as follows:

sudo mkdir -p /directorio_existente /<nombre_directorio>/<nombre_subdirectorio>

We can write something like:

sudo mkdir -p /opt/linux_adictos/artículos

The permission assignment commands being:

  • r: Permission to read.
  • w: Permission to write.
  • x: Permission to execute.

Conclusion:

  1. chmod +rwx nombre_directorio to give permissions.
  2. chmod -rwx nombre_directorio to remove them.

If you want to assign permissions within the protected directories, you must precede the sudo command. If you want to assign group permissions, put a ge after chmod.

chmod g + (o -) rwx nombre_directorio.

Keep in mind that only the letter corresponding to the permission you want to give or remove is put.


Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.