[Super beginner's guide in 3 minutes] Done! Creating and deleting directories

table of contents
Hello!
I'm Inoue, the Persian cat at Beyond Shikoku office.
Before joining the company, I heard the word " Linux " at the company orientation , a word I'd never heard before, and my brain was like, ?????? After joining the company, I encountered this " Linux " thing, and it's been about five months since then. This time, I'll talk about the command " mkdir, " which was my favorite when I first joined the company! Every time I created a directory with the " mkdir " command, I would be so happy I'd grin... (・´з`・) As a result, a huge number of directories were created on my Persian cat's AWS instance, and it became a real pain to delete them later... ((; ゚Д゚)) Oh dear. So, I'll also write about " rm " and " rmdir "!
mkdir command
"mkdir" is a command used to create directories.
First, let's try creating a directory without any options.
$ mkdir blog $ls -l drwxrwxr-x 2 ec2-user ec2-user 6 Aug 4 05:42 blog
A directory named "blog" has been created
Next, use the " -m " option to specify the directory's permissions (authority) at the same time as creating the directory .
$ mkdir -m 500 blog2 $ ls -l dr-x------ 2 ec2-user ec2-user 6 Aug 4 05:51 blog2
Permissions are here
r read w write x execute
Finally , add the " -p " option to simultaneously create child directories under the parent directory .
$ mkdir -p blog3/cat $ ls -l drwxrwxr-x 3 ec2-user ec2-user 17 Aug 4 05:55 blog3 $ ls -l blog3 drwxrwxr-x 2 ec2-user ec2-user 6 Aug 4 05:55 cat
We tested whether it was possible to create deep directories all at once
$ cd blog3/cat $ mkdir -p cat2/cat3/cat4/cat5
We were able to create subdirectories "cat2" through "cat5" under the "cat" directory all at once.
In this case, "cat2" would be followed by "cat3", and so on.
Delete a directory
To delete a directory using the " rm " command, specify the " -r " option to recursively delete the directory tree
$ cd cat2/cat3/cat4/ $ rm -r cat5 $ ls -l total 0
In this case, the files and directories in the directory "cat5" to be deleted will also be deleted
$ rm -r cat2 $ ls -l total 0 $ cd cat3 No such file or directory
If you delete the "cat2" directory, its subdirectory "cat3" will also be deleted, resulting in an error message saying "that directory doesn't exist." Therefore, if you're unsure, you can add the " -i " option. This will prompt you for confirmation before deleting, asking "Are you sure you want to delete?". Entering " y (yes) " will delete the directory, while entering " n (no) " will cancel the deletion. Very convenient!
$ rm -ri cat remove directory 'cat'?yes
To delete an empty directory, you can use the " rmdir " command
$ rmdir blog3
The main difference between the ` rmdir` command and `rm` is that the ` rmdir` command if there are files already in the directory
will result in an error . If you try to execute ` rmdir` when there is a file named `bydcat` inside the `haruka` directory ..
$ ls -l haruka -rw-rw-r-- 1 ec2-user ec2-user 0 Aug 4 06:52 bydcat $ rmdir haruka failed to remove 'haruka': Directory not empty
It says, "You can't delete it because there's a file in the directory." So, in cases like this, you need to use the " rm " command!
The advantage of the " rmdir " command is that it deletes empty directories, so if there are hidden files that start with a "." , it essentially tells you "you can't delete this." This prevents accidental deletion of hidden files without realizing it.
summary
When you think about work efficiency, options are really important! Creating subdirectories one by one is inefficient. To be honest, until I started writing this blog, I didn't really understand the difference between " rm " and " rmdir ". I just thought , "Basically, they're both commands to delete things" (;'∀') I hadn't really been actively using options, so it was a good learning experience and a good review. I hope I can write blogs like this that give Linux beginners like me the feeling of " I did it! " I'm getting used to the terminal (the black screen) now,
but I still find myself clicking the mouse a lot
Growing every day, moving forward every day.
I must update myself every single day!!!
Thank you for reading to the end.
2
