coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Suggestion: cdm command


From: Erik Auerswald
Subject: Re: Suggestion: cdm command
Date: Wed, 13 Jan 2021 09:07:35 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0

Hi all,

On 12.01.21 16:55, Bernhard Voelker wrote:
On 1/12/21 3:20 PM, Mourad Bougarne wrote:

I know that there's a command option to create a directory if not exists
using mkdir -p DIRECTORY_NAME, but what I suggest is create a directory if
not exists when we change it, so:

$ cd non-exists-directory
$ bash: cd: non-exists-directory: No such file or directory

With the command that I suggest we can do:
cdm: change directory make if not exists
$ cdm non-exists-directory

This is not possible as a separate tool, because the working directory of
the calling shell process would not change.

What about a simple shell function?

   $ function cdm() { mkdir "$1" && cd "$1"; }

   $ cdm hello

   $ cd ..

   $ cdm hello
   mkdir: cannot create directory 'hello': File exists

I think using 'mkdir -p' is more in line with the suggestion:

    cdm() { mkdir -p "$1" && cd "$1"; }

That would create the directory (and parent directories) only if
necessary, and would not fail if the directory already exists.

To not create parent directories, but still allow the target
directory to exist, one can replace '&&' with ';':

    cdm() { mkdir "$1"; cd "$1"; }

Thanks,
Erik





reply via email to

[Prev in Thread] Current Thread [Next in Thread]