Mkdir combined with "-p" flag - windows

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

Related

mkdir also if part of the path does not exists

I need to run something like
mkdir /var/log/apache2/www/custom-name/
The problem is if some of directories in path are missing. Shell does not create missing directories but throws me an error.
Is it possible to make missing directories in the path without testing if it exists?
If I understood the question, yes, it is.
In your case, instead of specifying
mkdir /var/log/apache2/www/custom-name/
write
mkdir -p /var/log/apache2/www/custom-name/
The -p flag enables the creation of parent directories. It should run without any error. (Reference 1 and 2)

purpose of chdir with a single dot (cd .)

This might appear a noob question.
While working in bash, if we run cd ., it stays in the current folder.
I understand the functionality, however, I am not able to understand the rationale of this functionality?
What would be some practical ways to use this?
The primary use case I've seen for cd . is to test whether your file handle on the current directory is still valid.
If you're on a directory from a network share -- NFS, or the like -- it can be possible for directories to be remotely deleted, but for the local client to still believe they're accessible and in use.
cd . is a way to trigger an error if your handle on the current working directory is no longer valid.
This is the only "practical" case that came to my mind
$ cd .
cd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
when your process has a current working directory referencing a directory that has been removed by another process.
That command has no functionality. But in a POSIX-compliant environment, if you add a -P option, then it has functionality: it resolves symlinks. So for example on a Mac, if you cd to a path with a symlink:
cd /System/Library/Frameworks/AppKit.framework/Versions/Current
...then do cd -P . ... you will point to:
/System/Library/Frameworks/AppKit.framework/Versions/C
. is a special file that represents the current directory.
There are plenty of things which make use of directories and it is sometimes useful to refer to the current directory.
Changing the directory to the current directory is not one of those.
A simple example where cd . fails:
mkdir my_error
cd my_error
rm -rf ../my_error
cd .
When the rm is embedded in a difficult script or can be done by some other cleanup process, is can be an useful check.
I use a build script which removes and recreates a directory.
The old directory disappears and new appears with new inode.
If, in one of my shells my $PWD is that reappeared directory and I notice
it became unusable (and I know it was recreated), I just
$ cd .
to get the (new) directory useable again and can continue my work there.

Creating a skeleton project directory

I am a rookie in Python who has been working on Learn Python the Hard Way. This whole process goes well as I have a smattering knowledge on Python until I march into ex46 where I get stuck in the 'Creating the skeleton Project Directory' section. I have no idea where I should run those commands guided on this book. Following are the excerpt of this part:
First, create the structure of your skeleton directory with these commands:
$ mkdir projects
$ cd projects/
$ mkdir skeleton
$ cd skeleton
$ mkdir bin
$ mkdir NAME
$ mkdir tests
$ mkdir docs
I have tried to run these commands in Windows Powershell, only to be warned that these commands can’t be recognized. I also fumbled to execute them in Pycharm, but all in vain. Could someone point out how I could get it done?
In addition, I am somewhat curious about this method because there seems to be handy way to approach this on Pycharm. Could I achieve the same goal on that?
I am using Python 2.7 and all previous exercises operate well until ex46.
You get that error because you're typing a superfluous $ at the beginning of each command. That $ is the (Linux) command prompt. On your Windows machine, it's something like C:\WINDOWS\system32>. Don't type that.
Just type
mkdir projects
and press Enter. That creates a folder (directory) named "projects". Then type
cd projects
and press Enter. That changes the current directory to that new folder you just created. And so on.
Content migrated from comments since this is what actually solved the issue.
Remove the dollar sign $ from the statement, as this is just the symbol used as a CLI prompt not part of the statement itself.
Then type the mkdir command and it should work e.g.
mkdir my_directory

What does mkdir -p create as the parent directory?

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.

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

Resources