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
How to
- 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{1..100}
Okay, we know mkdir
but what is the meaning of logs{1..100} in the above command.
The {} is known as brace expansion. So the logs is the prefix that was common in all the directories and {1..100} 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 {a..z} or {A..Z}.
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
In the above example, we used the command mkdir -p {jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec}_{2020..2025}/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
How to
- 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
touch {jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec}_{2020..2025}/logs/log{1..100}
.
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.
Top comments (0)