[SOLVED] I don't quite understand how mkdir works.
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello everybody i've been using linux for about 2 months now and i thought i understood mkdir. It just makes a directory.
Thing is today i was following a tutorial and it told me to do
mkdir /tmp/tutorial
At the time i was here: user@user:~/Desktop
Now this worked perfectly fine.
Then later on in the tutorial in it told me to make more directories doing this:
mkdir -p dir4/dir5/dir6
at the time i was here: user@user:/tmp/tutorial
This also worked fine.
Now the question is: Why did i have to use -p here and no slash? Why didn't i the first time? Has it got to do something with the tmp file or the place where i do the command?
See i tried doing mkdir /dir7/dir8 in the tutorial directory and this didn't work.
I also tried doing mkdir /dir7/dir8 in the Desktop directory and this didn't work either. Please help?
Now the question is: Why did i have to use -p here and no slash? Why didn't i the first time? Has it got to do something with the tmp file or the place where i do the command?
The -p options makes directories dir4 and dir5. If you leave it out, mkdir complains that there is no directory dir4/dir5.
You leave out the slash so that dir4 is created in /tmp/tutorial. If you put a slash in front of dir4, it will be created under /. Same semantics as in Windows, by the way (if I am not totally wrong).
Quote:
See i tried doing mkdir /dir7/dir8 in the tutorial directory and this didn't work.
I also tried doing mkdir /dir7/dir8 in the Desktop directory and this didn't work either. Please help?
What do you mean by "it did not work"? What happened when you tried?
If you prefix it with / then that is the root $PATH. If there's no / prefix, then it is a relative location. Relative to the directory you are currently in AKA $PWD. There's also ./ which is basically the same as no prefix. Or ../ which is the parent directory to your current location. If you're coming from a land of broken windows, think of / as C:\ or similar options.
Let's step back a little.
The basic misunderstanding is absolute versus relative path. Your tutorial should cover this, if not search "linux relative absolute path" - I hesitate to suggest any particular article as it may confuse the issue further.
What others are trying to explain is the difference between absolute and relative path. In general terms, regular users are limited with commands but can create directories in their /home/user and /tmp directoriess as you have seen. The second command you posted while your working directory was /tmp/tutorial, did not have a " / " preceding it which means it will creating any directory under /tmp/tutorial. The -p (which I've never used) apparently allows creating of multiple directories, in the form posted first creating dir4 then a sub-directory of it dir5 and yet anther sub-director of that dir6. You could create dir4, then cd there and create dir5, cd there and create dir6 so the -p option saves steps.
Quote:
From what i read from your comment the slash means its created under the root folder yes?
The command run as a normal user would be expected to fail if you preceded it with the forward slash as that would mean you are trying to create a directory in the root of the filesystem (signified by the forward slash /) A normal user has no rights here. Don't confuse root of the fileystem with the /root folder as they are two very different things. Running lss -l / will give you very different ouput than running ls -l /root.
What happened when i did
mkdir /dir7/dir8
Was this
mkdir: cannot create directory ‘/dir7/dir8’: No such file or directory
You tell mkdir to create dir8 in another directory dir7. Since dir7 doesn't exist, this doesn't work. You may wonder why mkdir doesn't simply create dir7 as well. This is by design: You need to explicitly tell mkdir that you also want dir7 to be created. This is done with the -p option.
Sorry incase this is not the way i should reply but i couldnt find a reply button on you comment @berndbausch
I thought i questioned it well sorry for putting a "didnt work" in there.
What happened when i did
mkdir /dir7/dir8
Was this
mkdir: cannot create directory ‘/dir7/dir8’: No such file or directory
I know it says that i can't do it i just didn't understand why.
From what i read from your comment the slash means its created under the root folder yes?
So i think i understand the slash part atleast but im still a bit unclear about the -p part.
Think of the -p as meaning "path". So mkdir /dir7/dir8 means make all the directories along the path specified. You need this because mkdir without -p only makes a single directory. In other words, it's a shortcut for doing mkdir /dir7 then mkdir /dir/dir8 in turn.
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,503
Rep:
You can create a whole path of directories by using the -p option, it means to create all the parent directories.
So, mkdir -p /one/two/three/four will make all the parent directories in the path to /four, that is, it will make /one, /one/two, /one/two/three, & /one/two/three/four all from the one command.
Thank you everybody for explaining so well, i just found out about "man" aswell and i learned that -p and for example -r or -l are all options you can give to commands. As far as i know i understand what happend now, i also followed the ubuntu terminal tutorial and made notes while doing that so my goal, which was getting some basic knowledge on the terminal and its commands, has been achieved.
Last edited by uteh5; 12-18-2019 at 06:52 AM.
Reason: grammar error.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.