LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-17-2019, 03:57 AM   #1
uteh5
LQ Newbie
 
Registered: Dec 2019
Posts: 3

Rep: Reputation: Disabled
I don't quite understand how mkdir works.


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?

Ill be eagerly waiting a response.
 
Old 12-17-2019, 04:11 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by uteh5 View Post
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?
 
1 members found this post helpful.
Old 12-17-2019, 04:12 AM   #3
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
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.
 
Old 12-17-2019, 04:17 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,139

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
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.
 
Old 12-17-2019, 04:30 AM   #5
uteh5
LQ Newbie
 
Registered: Dec 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-17-2019, 05:25 AM   #6
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,537

Rep: Reputation: 2495Reputation: 2495Reputation: 2495Reputation: 2495Reputation: 2495Reputation: 2495Reputation: 2495Reputation: 2495Reputation: 2495Reputation: 2495Reputation: 2495
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.
 
1 members found this post helpful.
Old 12-17-2019, 07:51 AM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by uteh5 View Post
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.
 
1 members found this post helpful.
Old 12-17-2019, 07:53 AM   #8
quiet.earth
LQ Newbie
 
Registered: Nov 2019
Location: Berkshire, UK
Distribution: Lubuntu, Ubuntu MATE, Debian
Posts: 10

Rep: Reputation: Disabled
Quote:
Originally Posted by uteh5 View Post
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.
 
1 members found this post helpful.
Old 12-17-2019, 09:14 AM   #9
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,503

Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
Old 12-17-2019, 10:00 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,927

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
interestingly man mkdir was not [yet] suggested. So check the man page (especially about -p and also -v)
 
1 members found this post helpful.
Old 12-18-2019, 06:51 AM   #11
uteh5
LQ Newbie
 
Registered: Dec 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] mkdir: cannot creat directory. How to enable mkdir for user in usb dir? Foxbat1155 Linux - Newbie 13 02-16-2012 11:20 AM
how to give options like '-v' for mkdir using system call mkdir() ? nehapawar Linux - Newbie 2 02-05-2010 02:13 AM
Cannot quite understand udftools. davcefai Debian 1 01-28-2006 02:15 PM
Don't quite understand The File Hierarchical System in Linux? jacatone Linux - Newbie 3 08-07-2005 09:55 PM
don't quite understand partition problem of Solaris chuanweizuo Solaris / OpenSolaris 3 03-07-2005 08:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration