Creating files and directories in linux

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:

Источник

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

Источник

Creating and Removing Files and Directories Under Linux

Here is an absolute beginner post on creating and removing Files and Directories under Linux.

Creating Files

The touch command creates a new empty file.

You can create multiple files with the same command. If the file name or directory name already exists, the touch command updates the modification time and access time to the current date and time. You can use absolute or relative path names on the command line when creating new files.

To create an empty file named file1 in the /tmp directory, enter the following commands:

To create multiple empty files use the touch commands with the file names in one line as shown below.

Creating Directories

The mkdir command creates new directories.

Include the –p option if the directory name includes a path name. The command used with the -p option creates all of the non-existing parent directories that do not yet exist in the path to the new directory. You can use absolute or relative path names on the command line when creating new directories.

For example, create a new directory, named dir1, within the /tmp directory.

You can use the command ‘ls -ld’ to view the created directory.

To create a new directory named dir_in located inside a directory named dir_out, use the mkdir command with the -p option. The dir_out directory does not yet exist.

To create the dir1, dir2, and dir3 directories, enter the mkdir command with all the directory names in one line as shown below.

Removing Files

You can permanently remove files from the directory hierarchy with the rm command.

The rm command is a destructive command if not used with the correct option. The table describes the options that you can use with the rm command when removing files and directories.

Option Description
-r Includes the contents of a directory and the contents of all subdirectories when you remove a directory
-i Prevents the accidental removal of existing files or directories

The –r option allows you to remove directories that contain files and subdirectories. The -i option prompts you for confirmation before removing any file.
– A yes response completes the removal of the file.
– A no response aborts the removal of the file.

For example, remove the file named file1 from the /tmp directory.

Lets see an example of using the -i option to delete the files.

Removing Directories

You can use the rm command with the -r option to remove directories that contain files and subdirectories.

For example, remove the dir1 directory and its content by using the rm –r command.

If you do not use the -r option with the rm command while removing directories, the following error message appears:

To interactively remove a directory and its contents, use the –i option along with the rm –r command. For example,

The rmdir command removes empty directories.

For example to remove the empty directory dir3, use the command below.

To remove a directory in which you are currently working in, you must first change to its parent directory.

Источник

Creating Files and Directories in Linux System

In the previous post, we added a powerful tool in our arsenal known as Wildcards. Wildcards are special characters that help us rapidly specify groups of filenames. These characters are * , ? , [characters] , [!characters] and [[:class:]] .

Now let’s move forward and learn some cool new commands to manipulate files and directories.

So where should we start? What is the first thing we require for manipulating files and directories?

Yup! You guessed it. The first thing we need to know is how do we create these directories?

1. mkdir command

The mkdir command stands for «make directory» is used to create a new directory.

The syntax for mkdir command

For creating a new directory we only need to call mkdir command along with the directory_name.

In the above example, we created a directory by simply using the command mkdir and the directory_name logs. Then we used ls command to verify that the directory with the name logs has been created.

Let’s take a few scenarios for better understanding

  • create multiple directories at once

In the above example, we created directories logs1, logs2, and logs3 at one go.

Okay, so we mention all the directory_names we want to create after mkdir .

What if we wanted to create a nested directory?

  • create directory level1 and inside level1 create level2 and inside level2 create level3

So for the above scenario we probably need to run mkdir 3 times one for each level.

What if I tell you that it will only take a single command to create such a nested directory.

Let’s check it out

In the above example, we use an option -p which allowed us to create a nested path in a single command

The command mkdir -p level1/level2/level3 means create a nested path level1/level2/level3.

To verify that the directories have been created properly we get into the directory using cd command and then traversed all the way till level3 and to check the current working directory we used pwd command.

That made creating a nested path so easy for us.

But there is still another problem what if we need to create 100 directories?

  • create 100 directories namely logs1, logs2 till logs100

So do we need to write the name of all the directories even though the only thing that changes in the folder name is the number at the end?

No, we don’t need to write the names of all the directories.

How will this be possible then?

Good Question, let’s checkout

  • create directories logs1 to logs100

In the above example, we created 100 directories using a simple command mkdir logs

Okay, we know mkdir but what is the meaning of logs in the above command.

The <> is known as brace expansion. So the logs is the prefix that was common in all the directories and represents numbers 1 to 100.

The Linux Command Line reads the above command as
mkdir logs1 logs2 logs3 . till logs100

Isn’t this Awesome!!

In operations such as this, the true powers of command line come forward because doing these types of operations from GUI will take us a lot of time but when the same operation is performed using the command line it takes less than a second.

The next question that comes into our mind is whether this is possible only with numbers?

No, we can use it with alphabets as well. We tried the first one together now why don’t you try this on your own I will give you a hint we can write alphabets as or .

Okay, the brace expansion seems to be very powerful but can we use this in the real-life scenario?

  • create directories like jan_2020, feb_2020 till dec_2025 and inside each directory, we need to create a directory named logs

We used mkdir to create a directory
-p since we need to create a nested path
<>_<> to create the required directories

So that was the mkdir command.

Next, we want to find out how to create files.

So there are many ways we can create a file but we are going to be looking at the most basic way to create a file.

2. touch command

The syntax for touch command

For creating a file we only need to use touch command along with file_name

In the above example, we created a file by simply using the command touch and the file_name notes.txt. Then we used ls command to verify that the file with the name notes.txt has been created.

Let’s take a few scenarios for better understanding

  • create multiple files at once

In the above example, we created files notes1, notes1, and notes1 at one go.

Okay, so we mention all the file_names we want to create after touch .

So the touch command works for files just as the mkdir works for the directories.

So that means that we can use the bracket expansion with the touch command as well

  • create files notes1 to notes100

In the above example, we saw the use of bracket expansion along with the touch command.

Now let’s take a real-life scenario, let us expand the scenario we discussed during the creation of a directory

  • create directories like jan_2020, feb_2020 till dec_2025 and inside each directory, we need to create a directory named logs and inside each directory logs we need to create 100 files from log1 till log100

In the above example, we first created the directories from jan_2020 till dec_2025 each having a directory named logs and then we used the command

where <>_<>/logs specifies all the directories where files are to be created and log <1..100>creates the files in each of the specified directories.

Okay, so that’s all the commands we need to know for Creating Files and Directories in Linux System.

In the next post, we will look into Deleting Files and Directories in Linux System.

I hope you understood the basics of exploration in Linux. Please, let me know if there are any questions.

Источник

Читайте также:  Монтирование образов для линукс
Оцените статью