How to create and remove directory in linux.
The mkdir
command is used to create a new directory. The command can also be used to create multiple and nested directories too.
mkdir
command Syntax:
1
2
3
mkdir Directory_Name
Example:
1
2
3
mkdir Directory1
use the ls
command mentioned in 001-list-files-with-dot.md file and you will see the directory is created.
shall return
1
2
3
Directory1
Let’s make multiple directories at once. To do so we will use the mkdir command with space after every directory name.
1
2
3
mkdir Dir1 Dir2 Dir3
shall return
1
2
3
Dir1 Dir2 Dir3
To create parent directories using the mkdir
command and pass the -p
option. By using this option you can create a nested directory.
1
2
3
mkdir -p Dir_out/Dir_in/Last
shall return
1
2
3
4
5
6
7
Dir_out
└── Dir_in
└── Last
The rmdir
command is used to remove a directory(it is like the delete folder option in windows) the directory deleted using the rmdir
command can not be restored again. This command can not be used to remove
a directory with files and sub directory inside them, To do so we use the rm
command
rmdir
command Syntax:
1
2
3
rmdir Directory_Name
Example: let’s suppose we have created three directory by using mkdir Dir1 Dir2 Dir3
now we will remove the Dir1.
1
2
3
rmdir Dir1
shall return
1
2
3
Dir2 Dir3
To delete a directory having more directories and files inside them we use the rm
command with -r
option. Let’s have two directory Dir1 and Dir_out having Dir_in, inside
it. If we use the rmdir
command To remove the Dir_out it will give you an error as Directory not empty. To delete it we use the rm command.
rm
command Syntax:
1
2
3
rm -r directory_name
1
2
3
rm -r Dir_out
shall return
1
2
3
Dir1