0

What is faster or considered better practice in shell scripts to first Cd into a directory then carry out commands or to write the full path? For example: Is it better to write cd /directory1/ and then mkdir subdirectory/ or mkdir /directory1/subdirectory/.

What if you need to execute a few commands in a directory (in the above example make a few subdirectories) then would the answer change?

When you say mkdir /directory1/subdirectory/ is it, in reality, doing cd /directory1/ and then mkdir subdirectory/ or not?

mkdir is just an example but the question can apply to alot of similar commands.

3

This is really something that is up to you to decide. What could influence that decision could be thing like whether it looks good or whether it's readable. In terms of execution speed, it wouldn't make a whole lot of difference.

Possibilities to create three subdirectories under the preexisting directory /path/to/dir:

mkdir /path/to/dir/a \
      /path/to/dir/b \
      /path/to/dir/b
( cd /path/to/dir && mkdir a b c )

(the subshell allows you to skip the cd back to the original working directory)

With a shell that knows brace expansions (this is essentially the same as the cd-less version above since the shell would expand the brace expansion before calling mkdir):

mkdir /path/to/dir/{a,b,c}

The mkdir utility will likely use the mkdir() C library function. This function does not change working directories before creating the directory it's set out to create1.

It's different when you execute scripts that care about being executed in a particular working directory. If the script expects to find files using relative paths out of its current working directory, then obviously you will have to make sure the the working directory is correct before running the command, possibly with

( cd directory && thescript )

... unless the script does this itself.


1 Slight correction: On Linux, the command mkdir -p a/b/c will call mkdir("a"), chdir("a"), mkdir("b"), chdir("b"), mkdir("c"), while on OpenBSD it will just call mkdir("a"), mkdir("a/b"), mkdir("a/b/c"). On Linux, creating an additional single directory a/b/c/d with mkdir a/b/c/d will just call mkdir("a/b/c/d").

So, on Linux, mkdir -p will indeed internally "do a cd" to each intermediate path while creating the directories (while mkdir with no -p does not do that).

The -p flag to the mkdir utility causes it to create any missing intermediate directory, and obviously the BSD and Linux developers do this ever so slightly differently.

1
  • You might want to mention that there's also the safety issue of avoiding the well-known exploit of another user maliciously renaming the parent directory when a sequence of commands is partway through execution. – JdeBP Oct 17 '18 at 6:37
-1

Doing a chdir into a directory and then use short names to access files in that directory is much faster than using absolute path names for the files.

On the other side: if you are in a specific directory and like to call a command with many file name arguments that are in different directories, it is more safe and easier understand when absolute path names are used.

BTW: If you like to create a longer path of directories (a/b/c/...) and like to make sure that each directory in that list is granted to exist after the command is ready, it is good practice to create one directory, chdir into that directory and then continue with the next directory. This allows to such a long directory path to be created even in case that the path is longer than PATH_MAX.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.