[Super introductory 3 minutes] Done! Directory creation and deletion

Hello!
This is Inoue, a Persian cat from Beyond Shikoku Office.

I heard a word I'd never heard before called
Linux at a company information session After joining the company, I encountered " Linux " and have been using it for about five months now.
This time, I'd like to talk about " mkdir, " a command that was my favorite when I joined the company!
Every time I created a directory with the " mkdir I'd grin with joy... (・´з`・)
Because of this, a large number of directories were created on my Persian Cat's AWS instance,
and it became a pain to delete them later... ((; ゚Д゚)) Oh no
, I'll also write about rm " and " rmdir

mkdir command

" mkdir " is a command to create a directory.
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 , add the -m
to specify the directory permissions (rights) when 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 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

The child directories "cat2 to 5" were created under the "cat" directory in one go.
In this case, "cat3" is created under "cat2" and so on.

Delete a directory

To remove a directory with
the rm command specify -r , which recursively removes 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 child directory "cat3" will also be deleted, and you will be told "There is no such directory."
So, if you are unsure, you can add the " -i " option,
will ask
"Are you sure you want to delete?" before executing the deletion " y (yes) " will delete the directory, and entering n (no)

$ rm -ri cat remove directory 'cat'?yes

To delete an empty directory, you can use the rmdir

$ rmdir blog3

" rmdir " command differs greatly from
" rm
an error occurs when you try to delete a directory if there are files in it If there is a file called "bydcat" in the "haruka" directory, and you try to run rmdir

$ 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

You will be told that the directory cannot be deleted because it contains files.
In this case, you need to use the rm

" rmdir " command is that it is a command to delete empty directories, so
there a hidden file that starts with "." it will tell you "You can't delete it."
This prevents you from accidentally deleting a hidden file without realizing it.

summary

Options are important when it comes to work efficiency!
Creating child directories one by one is inefficient.
To be honest, I didn't really understand the difference between
rm " and " rmdir I just knew they were commands to delete files (;'∀').
I never actively used options, so this was a good opportunity to learn and review. I hope
to write blogs
like this that help Linux beginners like me feel like they've made it! I'm starting to get used to the terminal (the black screen), but I can't help
but click the mouse. I'm a CUI Persian cat.

I'll continue to write blogs about my favorite commands and those I want to understand more, through trial and error

Growing every day, moving forward every day.
I have to update myself every day! ! !
Thank you for reading to the end.

If you found this article helpful , please give it a like!
1
Loading...
1 vote, average: 1.00 / 11
6,517
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Akika Inoue

Belongs to the System Solutions Department.
He joined Beyond as a founding member of the Shikoku office.
I jumped into the IT industry with no experience. As an education team, we create curriculum and conduct training for new graduates, mid-career, and existing members.
The main business is server operation and maintenance.
Either way, we value your content.
Also belongs to the Web Content Division and YouTube Team.