What does mkdir -p create as the parent directory? - windows

I read from http://www.techonthenet.com/unix/basic/mkdir.php that the mkdir command "creates a single directories or multiple directories." and that -p "If the parent directories don't exist, this command creates them". I tried this command in cmd(window operating system) and saw that with -p and without -p, the command just created a file with a file name. How do you see the effect of "If parent directories don't exist creates them." I am assuming C:\Users\chris> is the parent directory when i run the command(therefore the parent directory exists). Is there a way of seeing the effect of -p?

Try help.
C:\Users\User>mkdir/?
Creates a directory.
MKDIR [drive:]path
MD [drive:]path
If Command Extensions are enabled MKDIR changes as follows:
MKDIR creates any intermediate directories in the path, if needed.
For example, assume \a does not exist then:
mkdir \a\b\c\d
is the same as:
mkdir \a
chdir \a
mkdir b
chdir b
mkdir c
chdir c
mkdir d
which is what you would have to type if extensions were disabled.
Installing Unix
Unix Services for Windows are either a download from MS website or installed via Control Panel - Programs and Features - Windows Features. Which depends on version and edition of your Windows.

Related

Bug in GnuWin32 port of mkdir?

I seem to have come across a wierd behaviour of GnuWin32's mkdir (from here):
C:\sandbox>"C:\Program Files (x86)\GnuWin32\bin\mkdir.EXE" --verbose -p Q:/scratch/foo/bar
C:\Program Files (x86)\GnuWin32\bin\mkdir.EXE: created directory `Q:/scratch'
C:\Program Files (x86)\GnuWin32\bin\mkdir.EXE: created directory `Q:/scratch/foo'
C:\Program Files (x86)\GnuWin32\bin\mkdir.EXE: created directory `Q:/scratch/foo/bar'
In the above, the current drive is C and the target drive is Q.
As expected, the hierarchy gets created in Q:/scratch/foo/bar
Now, almost the same example but the current and the target drives are both C.
C:\sandbox>"C:\Program Files (x86)\GnuWin32\bin\mkdir.EXE" --verbose -p C:/scratch/foo/bar
C:\Program Files (x86)\GnuWin32\bin\mkdir.EXE: created directory `C:/scratch'
C:\Program Files (x86)\GnuWin32\bin\mkdir.EXE: created directory `C:/scratch/foo'
C:\Program Files (x86)\GnuWin32\bin\mkdir.EXE: created directory `C:/scratch/foo/bar'
mkdir's output looks right and very predictable.
However, nothing really gets created at C:/scratch/foo/bar. Rather, the directory hierarchy gets created using the current directory as a root, i.e. what gets created is C:/sandbox/scratch/foo/bar
Can anyone confirm ? Is this a bug ?
I would look at the source code if I had the time but....
Does anyone know of a more recent version of CoreUtils than 5.3.0 that's been ported to Win32 ?
Looks like a small bug indeed. It seems GnuWin32's port doesn't like the forward slash after the colon following the drive letter. Using backslashes it behaves correctly with both
C:\sandbox> mkdir --verbose -p Q:\scratch/foo/bar
and
C:\sandbox> mkdir --verbose -p C:\scratch/foo/bar
But (as reported) when using forward slashes, it only behaves correctly with
C:\sandbox> mkdir --verbose -p Q:/scratch/foo/bar
(in the above, note the target is on a different drive than the prompt) but not with
C:\sandbox> mkdir --verbose -p C:/scratch/foo/bar
(in the above, note the target is on the same drive as the prompt)
The really old mkdir port from UnxUtils has the correct behaviour all the time. That's the only working alternative I could find.

Mkdir combined with "-p" flag

I am following a tutorial where I have to create a directory but also pass -p flag. I tried running it and I got a syntax failure. So I wanted to figure out what the -p did and found that this abbreviation is short for privileged. And found
Script runs as "suid" (caution!)
Started looking what that meant and found it meant Set User Identification and read that
– When a command or script with SUID bit set is run, its effective UID becomes that of the owner of the file, rather than of the user who is running it. Source
However, I still do not quite understand it. What is the purpose of me setting a directory to have that privilege and why should I be careful? Also, I tried looking here but I couldn't find any clarification(with the different search keywords I used). Also, not necessary.. but , why would me doing mkdir -p src/entities give me a syntax failure? I am using Windows(but I also have a bash package for Anaconda).
It looks like you're following a Unix-ish tutorial but running the commands on Windows in cmd.exe.
As the usage instructions say:
C:\>mkdir /?
Creates a directory.
MKDIR [drive:]path
MD [drive:]path
If Command Extensions are enabled MKDIR changes as follows:
MKDIR creates any intermediate directories in the path, if needed.
For example, assume \a does not exist then:
mkdir \a\b\c\d
is the same as:
mkdir \a
chdir \a
mkdir b
chdir b
mkdir c
chdir c
mkdir d
which is what you would have to type if extensions were disabled.
Windows commands don't use - for options (and in particular, the mkdir command built into cmd doesn't understand -p).
The part about "privileged" is for the shell option -p, as in bash -p. It has nothing to do with mkdir -p, which is explained in man mkdir:
-p, --parents
         no error if existing, make parent directories as needed
But again, that only applies to the Unix mkdir, not Windows / cmd.
"-p" creates parent directories if they don't exist.
For example:
With "-p" if "first" directory doesn't exist.
mkdir -p first/second # "first" parent directory is created
Without "-p" if "first" directory doesn't exist.
mkdir first/second # "first" parent directory is not created
mkdir: cannot create directory ‘first/second’: No such file or directory

How to change directory under git bash

Could you tell me please how to change directory using git bash?
Exactly I want go to C:/Program Files
I type cd c:/ - move to c:/.
I type cd Program Files and I get message that "there is no such directory Program".
So the problem is how to go to the directory name of which consists of two words that are separated by whitespaces.
I'm using Windows 7 operating system.
cd gets just one argument, so if you say cd Program Files it does cd Program. So you need to quote Program Files if you want cd to it as just one argument:
cd "Program Files"
otherwise it is trying to find the directory Program.
You could also type the space as escape seqence:
cd /c/Program\ Files/Git/
Please find below to add windows path in git bash or in unix
export PATH=$PATH:/c/Program\ Files/Java/jdk1.8.0_221/bin:/c/Program\ Files/apache-maven-3.6.1/bin

mkdir -p in Mac

I have been reading the description of the OSX Man page. It has description like following regarding mkdir -p:
-p
Create intermediate directories as required. If this option is not specified, the full path prefix of each operand must already exist. On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists. Intermediate directories are created with permission bits of rwxrwxrwx (0777) as modified by the current umask, plus write and search permission for the owner.
I am not quite following this description. especially "If this option is not specified, the full path prefix of each operand must already exist. On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists."
Does someone has an example regarding this explanation?
Given this directory structure:
/
foo/
bar/
baz/
This will obviously work:
mkdir /foo/x
This will not work:
mkdir /foo/x/y
Because /foo/x does not exist, the directory /foo/x/y cannot be created underneath it. The prefix /foo/x/ needs to exist in order to create /foo/x/y.
This is where -p comes in. This works:
mkdir -p /foo/x/y
/foo/x will implicitly be created together with /foo/x/y.
If you try:
mkdir /bar/baz
You'll get an error that the directory already exists. However, if you do:
mkdir -p /bar/baz
you will not get an error, it'll just silently ignore all already existing directories and be content with the result without doing anything.
Imagine you have an empty folder, and you want to create a subdirectory called "d1" and a subirectory inside "d1" called "d2". Normally you must do this:
mkdir d1
mkdir d1/d2
With the "-p" option you can have mkdir create the in-between directory (d1) for you:
mkdir -p d1/d2
The bit you are asking about says that if "d1" already exists and you use "mkdir -p" it won't matter that it is already there and there won't be any error messages.
This work on version 10.10.4
mkdir -pv d1/d/d3
Not sure if it is mac only, but on mac os x you can do
mkdir -p src/{main,test}/{java,resources,scala}
which will not only give you a nested, but also cartesian product of your directories:
src/test/java
src/test/resources
src/test/scala
src/main/java
src/main/resources
src/main/scala

What is equivalent to Linux mkdir -p in Windows?

In Linux, mkdir -p creates a folder tree.
What is the equivalent option in Windows to create a folder tree? Is there any?
In Windows, mkdir creates directory trees by default.
mkdir a\b\c
The Windows mkdir does it automatically if command extensions are enabled. They are on just about every box I've ever used but, if they're not, you can create your own script to do it:
#echo off
setlocal enableextensions
md %1
endlocal
Expanding:
Command extensions are an added feature of cmd.exe which allows you to do so much more (at the cost of a little compatibility with earlier incarnations of the batch language).
Windows XP cmd.exe should have these extensions enabled by default but you can configure your box so that they're disabled by default (using "cmd /e:off" as the default processor). If you do that and want to use the extensions, your cmd files must have a setlocal to turn them back on.
The script above could be called md2.cmd and then you would be guaranteed to be able to create multiple directory levels with "md2 a\b\c" without having to worry whether the extensions were enabled.
Almost every one of the cmd scripts I write begins with:
setlocal enableextensions enabledelayedexpansion
to ensure I get as close as possible to the behavior of my beloved bash :-)
For a strange reason when I attempted to create a directory with the following method;
mkdir src/main/java/main/resources
it didn't work, I had to surround the path in double quotes, as shown below;
mkdir "src/main/java/main/resources"
Additionally, unix allows for this;
mkdir -p src/main/java src/main/resources
where two branches will be created as shown below, the equivalent to that on windows is;
mkdir "src/java/resources" "src/main/resources"
src
-----java
-------resources
-----main
-------resources
I hope this helps!
xox
If you want to use forward slashes, just give the directory structure you want within double quotes.
mkdir "org/frame/bu/fed/config"
mkdir by default makes all intermediate directories. Just ensure that you use '\' as the separator.
I just try to create multiple folders on today and it is working!
mkdir "templates" "static/css" "static/js"

Resources