Within the earlier chapter of the Terminal Fundamentals collection, you realized about altering folders within the Linux command line.
I gave an train on the finish that briefly talked about making directories.
On this a part of the collection, I am going to focus on how one can make new folders within the Linux command line utilizing the mkdir command.
mkdir dir_name
mkdir is in need of make directories. Let’s examine about utilizing this command.
📋
In case you did not know, folders are known as directories in Linux.
Making a brand new listing in Linux
Try to be conversant in the idea of absolute and relative paths in Linux by now. If not, please consult with this tutorial.
Open the terminal in your system if it’s not already opened. Usually, you begin with your private home listing (/residence/username). However for the sake of this tutorial and to recall a few issues, I presume you aren’t in your house listing.
So, change to your private home listing first.
cd
Sure. In the event you merely enter cd with none choices and arguments, it takes you to your private home listing. You can additionally use cd ~
amongst different strategies.
Right here, make a brand new listing known as observe.
mkdir observe
Can you turn to this newly created observe listing?
cd observe
Nice! Now you might have a devoted folder the place you may observe the Linux command line tutorials on this collection.
Creating a number of new directories
You simply created a brand new listing. What if it’s a must to create a couple of? For example three of them.
You might use the mkdir command thrice in a row for every of them. It’s going to work. Nonetheless, it’s not actually wanted. It can save you effort and time by creating a number of directories on the similar time like this:
mkdir dir1 dir2 dir3
Go on and try this please. You possibly can checklist the contents of the observe
listing to see all of the newly created directories. Extra on the ls command later.
💡
You can not have two folders or information of the identical identify in the identical location.
Making a number of nested subdirectories
So, you now learn about creating a number of directories without delay.
However what if it’s a must to create a nested listing construction? For example that it’s a must to create a listing subdir2 inside subdir1 inside dir1.
dir1/subdir1/subdir2
The issue right here is that subdir1 doesn’t exist. So in case you attempt `mkdir dir1/subdir1/subdir32, you may get an error:
[email protected]:~/observe$ mkdir dir1/subdir1/subdir2
mkdir: can not create listing ‘dir1/subdir1/subdir2’: No such file or listing
In the event you did not know higher, you’d go for mkdir dir1/subdir1
after which run mkdir dir1/subdir2
. That may work. Nonetheless, there’s a significantly better method.
You employ the -p
choice, which makes mother or father directories if wanted. In the event you run the command beneath:
mkdir -p dir1/subdir1/subdir2
It’s going to create subdir1 after which subdir2 inside subdir1.
💡
There isn’t any naming conference, however it’s higher to keep away from areas in file and listing names. Use underscore or sprint as a substitute as a result of dealing with areas in file/listing names requires particular effort.
Take a look at your data
That is somewhat a brief tutorial as a result of the mkdir command has only some choices.
Now, let me provide you with some observe workouts to make the most of the observe
listing you had created earlier.
- With out getting into the
dir2
listing, create two new subdirectories in it. - With out getting into the
dir3
listing, create two-level nested subdirectories (subdir1/subdir2) - Change to the dir2 listing. From right here, create a listing named temp_stuff in your house listing. Don’t fret; we’ll delete it later on this tutorial collection.
- Return to the mother or father
observe
listing and attempt to create a listing nameddir3
. You see an error. Are you able to make it go away with the-p
choice?
You possibly can focus on this train locally discussion board.
Within the subsequent chapter of the Terminal Fundamentals collection, you may find out about itemizing the contents of a listing with the ls command.
Do let me know you probably have questions or options.