loader

mkdir Command

In Linux, users use the “mkdir” command to create new directories. So because creating a directory is a “write” action on the parent directory, users should have the write permission on the parent directory. Users also can create multiple directories or nested directories.

Syntax

mkdir [option] dir-name 

Examples:

mkdir without any options and only with just a directory name creates a new directory in the current working directory, or if the dir-name includes a path, so this command creates a new directory in the specified path.
-v: this option verbose the operation, which means that this option displays the result of the command execution
-p: If users want to create nested directories as the image below shows, they use this option. It means that to create a new directory that its parent directory still does not exist, and users want to create the entire path by a single command, they use this option.

				
					[root@Lpic1-CentOs9 ~]# pwd
/root
[root@Lpic1-CentOs9 ~]# mkdir test1
[root@Lpic1-CentOs9 ~]# ls -lah
total 36K
dr-xr-x---.  4 root root  188 May 26 13:47 .
dr-xr-xr-x. 18 root root  235 Apr 28 04:08 ..
drwxr-xr-x.  2 root root    6 May 26 13:47 test1
********************************************************
[root@Lpic1-CentOs9 ~]# mkdir -v test2
mkdir: created directory 'test2'
********************************************************
[root@Lpic1-CentOs9 ~]# mkdir ./d1/d2/d3/d4
mkdir: cannot create directory ‘./d1/d2/d3/d4’: No such file or directory
[root@Lpic1-CentOs9 ~]# mkdir -p  ./d1/d2/d3/d4
[root@Lpic1-CentOs9 ~]# tree .
.
├── d1
│   └── d2
│       └── d3
│           └── d4

				
			

Practice1 

Do practice below considering these images.

1- Create d1 to d5 directories in the Img1 in the current working directory in two ways

2- Consider the Img2, create d1 to d5 directories within the dir1 parent directory in several ways.

				
					****************Answer 1-way 1**************************
[root@Lpic1-CentOs9 ~]# mkdir d1 d2 d3 d4 d5
[root@Lpic1-CentOs9 ~]# ls -lah
dr-xr-x---. 10 root root 4.0K May 26 14:35 .
dr-xr-xr-x. 18 root root  235 Apr 28 04:08 ..
drwxr-xr-x.  2 root root    6 May 26 14:35 d1
drwxr-xr-x.  2 root root    6 May 26 14:35 d2
drwxr-xr-x.  2 root root    6 May 26 14:35 d3
drwxr-xr-x.  2 root root    6 May 26 14:35 d4
drwxr-xr-x.  2 root root    6 May 26 14:35 d5
****************Answer 1-way 2**************************
[root@Lpic1-CentOs9 ~]# mkdir ./{d1,d2,d3,d4,d5}
[root@Lpic1-CentOs9 ~]# ls -lah
dr-xr-x---. 10 root root 4.0K May 26 14:43 .
dr-xr-xr-x. 18 root root  235 Apr 28 04:08 ..
drwxr-xr-x.  2 root root    6 May 26 14:43 d1
drwxr-xr-x.  2 root root    6 May 26 14:43 d2
drwxr-xr-x.  2 root root    6 May 26 14:43 d3
drwxr-xr-x.  2 root root    6 May 26 14:43 d4
drwxr-xr-x.  2 root root    6 May 26 14:43 d5
****************Answer 2*******************************
[root@Lpic1-CentOs9 tmp]# mkdir -p  ./dir1/{d1,d2,d3,d4,d5}
[root@Lpic1-CentOs9 tmp]# tree .
.
├── dir1
│   ├── d1
│   ├── d2
│   ├── d3
│   ├── d4
│   └── d5
******************way 2*********************************
[root@Lpic1-CentOs9 tmp]# mkdir -p ./dir1/d{1,2,3,4,5}
[root@Lpic1-CentOs9 tmp]# tree .
.
├── dir1
│   ├── d1
│   ├── d2
│   ├── d3
│   ├── d4
│   └── d5
******************way 3*********************************
[root@Lpic1-CentOs9 tmp]# mkdir -p ./dir1/d{1..5}
[root@Lpic1-CentOs9 tmp]# tree
.
├── dir1
│   ├── d1
│   ├── d2
│   ├── d3
│   ├── d4
│   └── d5

				
			

Note: we talked about the dot “.” In previous posts. Dot means “here” or the current working directory.

cp Command

To copy files or directories in Linux, use the “cp” command. The “cp” command copies files and directories from a source to a destination. So this command needs two arguments to execute. Users also can copy multiple files or directories using the “cp” command.

Syntax

cp [OPTION] Source Destination

cp [OPTION] filename1 filename2: the first file name is the source and the second one is the destination. The cp will overwrite the destination file without warnings if the destination file exists. And if the 2nd file doesn’t exist, cp first creates it and then copies the content.

cp [OPTION] filename Directoryname: use this syntax to copy a file to the destination directory. Administrators also can use this syntax to copy multiple files in a directory.

cp [OPTION] Directoryname1 Directoryname2: To copy an entire directory to the destination directory, users must specify two directory names. Note that if the destination directory doesn’t exist, then the “cp” command creates it first and then recursively copies the content. But if the destination directory exists, the “cp” command will copy the source directory as a subdirectory under the destination directory. Use “-r” option with this syntax.

Examples:

-i : If the destination file exists, “cp” will overwrite the destination file without confirming. So this option asks if overwriting the destination file is ok.
-b: stands for backup. If the destination file exists, this option will create a copy of the file as the backup in the same directory with a different name. So the original content of the file would be available.
-r or –R: copies all files and subdirectories in the source directory to the destination directory.

Consider the image below and do the practice 2.

1- Create directory “d1” and its subdirectory “d2” in the home directory.

				
					[root@Lpic1-CentOs9 ~]# mkdir -p d1/d2
[root@Lpic1-CentOs9 ~]# tree
├── d1
│   └── d2

				
			

2- Create files “f1” and “f2” under directory d2 . and echo some content into them.

				
					[root@Lpic1-CentOs9 ~]#touch d1/d2/{f1,f2}
[root@Lpic1-CentOs9 ~]#echo "Linux is the best os" > d1/d2/f1
[root@Lpic1-CentOs9 ~]#echo "I Love Linux" > d1/d2/f2
[root@Lpic1-CentOs9 ~]# tree
.
├── d1
│   └── d2
│       ├── f1
│       └── f2
[root@Lpic1-CentOs9 ~]#more d1/d2/f1
Linux is the best os
[root@Lpic1-CentOs9 ~]# more d1/d2/f2
I Love Linux

				
			

3- Copy f1 with the new name “f1-copy” under d1

				
					[root@Lpic1-CentOs9 ~]# cp d1/d2/f1 d1/f1-copy
[root@Lpic1-CentOs9 ~]# tree
.
├── d1
│   ├── d2
│   │   ├── f1
│   │   └── f2
│   └── f1-copy
[root@Lpic1-CentOs9 ~]#more d1/f1-copy
Linux is the best os

				
			

4-Copy the “f2” to “f1-copy” with confirmation and show the content of file “f1-copy.”

				
					[root@Lpic1-CentOs9 ~]# cp -i  d1/d2/f2 d1/f1-copy
cp: overwrite 'd1/f1-copy'? y
[root@Lpic1-CentOs9 ~]# more d1/f1-copy
I Love Linux

				
			

5-Copy directory “d1” with all content to directory “d1-copy.”

				
					[root@Lpic1-CentOs9 ~]# cp -R d1/ d1-copy/
[root@Lpic1-CentOs9 ~]# tree
.
├── d1
│   ├── d2
│   │   ├── f1
│   │   └── f2
│   └── f1-copy
├── d1-copy
│   ├── d2
│   │   ├── f1
│   │   └── f2
│   └── f1-copy

				
			

6- Create file “f3” under the directory “d2” and copy the entire directory the “d1” to the directory “d1-copy”.
See the differences between this practice with practice number 5.

				
					[root@Lpic1-CentOs9 ~]#touch d1/d2/f3
[root@Lpic1-CentOs9 ~]#tree
.
├── d1
│   ├── d2
│   │   ├── f1
│   │   ├── f2
│   │   └── f3
│   └── f1-copy
[root@Lpic1-CentOs9 ~]#cp -r d1/ d1-copy/
[root@Lpic1-CentOs9 ~]# tree
.
├── d1
│   ├── d2
│   │   ├── f1
│   │   ├── f2
│   │   └── f3
│   └── f1-copy
├── d1-copy
│   ├── d1
│   │   ├── d2
│   │   │   ├── f1
│   │   │   ├── f2
│   │   │   └── f3
│   │   └── f1-copy
│   ├── d2
│   │   ├── f1
│   │   └── f2
│   └── f1-copy

				
			

Leave a Reply

Your email address will not be published. Required fields are marked *