johnny-b-goode
johnny-b-goode

Reputation: 3893

What difference between those two shell commands?

There are two shell commands:

[[ ! -d $TRACEDIR/pattrace/rpt/.tracking ]] && mkdir $TRACEDIR/pattrace/rpt/.tracking  
[[ ! -d $TRACEDIR/pattrace/rpt/.tracking ]] && mkdir -p $TRACEDIR/pattrace/rpt/.tracking

Obviously, the only difference between those commands is -p flag. But what this flag does in this context?

Thanks.

Upvotes: 0

Views: 108

Answers (2)

Levon
Levon

Reputation: 143027

From the mkdir man page:

-p, --parents
      no error if existing, make parent directories as needed

In other words, if the directories needed don't exist, they will be created as required. If the directories already exist, it won't cause an error.

This is a good place to look for man pages (in addition to using google of course)

Upvotes: 4

Christian.K
Christian.K

Reputation: 49220

mkdir with the -p option will create all necessary parent directories of the specified path, should they not exist (see man pages). Also, with -p you won't get an error if the directory itself already exists.

In your particular case, the first command might fail because the test for the complete path is not sufficient. The test will also fail if only, say, $TRACEDIR/ exists but the subsequent mkdir will the fail because it would require $TRACEDIR/pattrace/rpt/ to exist.

The second command will work, because mkdir -p creates all missing directories "in between" as well.

Upvotes: 2

Related Questions