mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-04 22:00:34 +08:00
145 lines
5.6 KiB
Markdown
145 lines
5.6 KiB
Markdown
[#]: subject: "Making Directories in Linux Terminal"
|
||
[#]: via: "https://itsfoss.com/make-directories/"
|
||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||
[#]: collector: "lkxed"
|
||
[#]: translator: " "
|
||
[#]: reviewer: " "
|
||
[#]: publisher: " "
|
||
[#]: url: " "
|
||
|
||
Making Directories in Linux Terminal
|
||
======
|
||
|
||
In the [previous chapter][1] of the Terminal Basics series, you learned about changing folders in the Linux command line.
|
||
|
||
I gave an exercise at the end that briefly mentioned making directories.
|
||
|
||
In this part of the series, I'll discuss how you can make new folders in the Linux command line using the mkdir command.
|
||
|
||
```
|
||
mkdir dir_name
|
||
```
|
||
|
||
mkdir is short of make directories. Let's see about using this command.
|
||
|
||
📋
|
||
|
||
In case you didn't know, folders are called directories in Linux.
|
||
|
||
### Making a new directory in Linux
|
||
|
||
You should be familiar with the [concept of absolute and relative paths in Linux][2] by now. If not, please refer to this tutorial.
|
||
|
||
Absolute vs Relative Path in Linux: What’s the Difference?In this essential Linux learning chapter, know about the relative and absolute paths in Linux. What’s the difference between them and which one should you use.![][3]Linux HandbookAbhishek Prakash![][4]
|
||
|
||
Open the terminal on your system if it is not already opened. Normally, you start with your home directory (/home/username). But for the sake of this tutorial and to recall a couple of things, I presume you are not in your home directory.
|
||
|
||
So, change to your home directory first.
|
||
|
||
```
|
||
cd
|
||
```
|
||
|
||
Yes. If you simply enter cd without any options and arguments, it takes you to your home directory. You could also use `cd ~` among other methods.
|
||
|
||
Here, make a new directory called practice.
|
||
|
||
```
|
||
mkdir practice
|
||
```
|
||
|
||
Can you switch to this newly created practice directory?
|
||
|
||
```
|
||
cd practice
|
||
```
|
||
|
||
Great! Now you have a dedicated folder where you'll practice the Linux command line tutorials in this series.
|
||
|
||
![Example of making new directory in Linux][5]
|
||
|
||
### Creating multiple new directories
|
||
|
||
You just created a new directory. What if you have to create more than one? Let's say three of them.
|
||
|
||
You may use the mkdir command three times in a row for each of them. It will work. However, it is not really needed. You can save time and effort by creating multiple directories at the same time like this:
|
||
|
||
```
|
||
mkdir dir1 dir2 dir3
|
||
```
|
||
|
||
Go on and do that please. You can list the contents of the `practice` directory to see all the newly created directories. More on the ls command later.
|
||
|
||
![Create multiple new directories in Linux with mkdir command][6]
|
||
|
||
💡
|
||
|
||
You cannot have two folders or files of the same name in the same location.
|
||
|
||
### Making multiple nested subdirectories
|
||
|
||
So, you now know about creating multiple directories at once.
|
||
|
||
But what if you have to create a nested directory structure? Let's say that you have to create a directory subdir2 inside subdir1 inside dir1.
|
||
|
||
```
|
||
dir1/subdir1/subdir2
|
||
```
|
||
|
||
The problem here is that subdir1 does not exist. So if you try `mkdir dir1/subdir1/subdir32, you'll get an error:
|
||
|
||
```
|
||
[email protected]:~/practice$ mkdir dir1/subdir1/subdir2
|
||
mkdir: cannot create directory ‘dir1/subdir1/subdir2’: No such file or directory
|
||
```
|
||
|
||
If you didn't know better, you would go for `mkdir dir1/subdir1` and then run `mkdir dir1/subdir2`. That will work. However, there is a much better way.
|
||
|
||
You use the `-p` option, which makes parent directories if needed. If you run the command below:
|
||
|
||
```
|
||
mkdir -p dir1/subdir1/subdir2
|
||
```
|
||
|
||
It will create subdir1 and then subdir2 inside subdir1.
|
||
|
||
> 💡 There is no naming convention, but it is better to avoid spaces in file and directory names. Use underscore or dash instead because handling spaces in file/directory names requires special effort.
|
||
|
||
### Test your knowledge
|
||
|
||
This is rather a short tutorial because the mkdir command has only a few options.
|
||
|
||
Now, let me give you some practice exercises to utilize the `practice` directory you had created earlier.
|
||
|
||
- Without entering the `dir2` directory, create two new subdirectories in it.
|
||
- Without entering the `dir3` directory, create two-level nested subdirectories (subdir1/subdir2)
|
||
- Change to the dir2 directory. From here, create a directory named temp_stuff in your home directory. Don't worry; we will delete it later in this tutorial series.
|
||
- Go back to the parent `practice` directory and try to create a directory named `dir3`. You see an error. Can you make it go away with the `-p` option?
|
||
|
||
You can [discuss this exercise in the community forum][7].
|
||
|
||
In the next chapter of the Terminal Basics series, you'll learn about listing the contents of a directory with the ls command.
|
||
|
||
Do let me know if you have questions or suggestions.
|
||
|
||
--------------------------------------------------------------------------------
|
||
|
||
via: https://itsfoss.com/make-directories/
|
||
|
||
作者:[Abhishek Prakash][a]
|
||
选题:[lkxed][b]
|
||
译者:[译者ID](https://github.com/译者ID)
|
||
校对:[校对者ID](https://github.com/校对者ID)
|
||
|
||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||
|
||
[a]: https://itsfoss.com/author/abhishek/
|
||
[b]: https://github.com/lkxed/
|
||
[1]: https://itsfoss.com/change-directories/
|
||
[2]: https://linuxhandbook.com/absolute-vs-relative-path/
|
||
[3]: https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png
|
||
[4]: https://linuxhandbook.com/content/images/2021/04/absolute-relative-path-linux.png
|
||
[5]: https://itsfoss.com/content/images/2023/02/make-directory-example.svg
|
||
[6]: https://itsfoss.com/content/images/2023/02/create-multiple-directories-linux.png
|
||
[7]: https://itsfoss.community/t/exercise-in-making-directories-in-linux-terminal/10227
|