3

I have created lots of directories and I would to make my life lazy and to auto cd into the directory that I made with the option of -g with the result of mkdir -g foo

The terminal would be like this:

User:/$ mkdir -g foo
User:/foo/$ 

I have looked at this page but with no success.

Is there a one-liner that allows me to create a directory and move into it at the same time?

I have tried in my .bash_profile with this command, but it just overrides the command entirely. (and realizing that it is an infinite loop later)

mkdir() {
 if "$1" -eq "-g"
  mkdir "$2"
  cd "$2"
 else
  ## do the regular code
 fi
}

The other thing is that I don't want to make more aliases because I will eventually forget all of the aliases that I have.

So not I don't want this:

the command entirely. (and realizing that it is an infinite loop later)

mkcd() {
 if "$1" -eq "-g"
  mkdir "$2"
  cd "$2"
 else
  ## do the regular code
 fi
}

I know that I most likely don't have the option thingy right, please correct me on that, but any help would be awesome!

  • 1
    The simplest fix is to use $(which mkdir) to invoke the system mkdir. I strongly recommend making an alias with a different name though. – DepressedDaniel Feb 26 '17 at 23:25
  • 2
    Making an alias with a different syntax and behaviour to an existing UNIX command may be an eternal source of confusion and errors if you try to source other people's code. I would really advise to make a different alias for your command, e.g. mkcdir. – undercat applauds Monica Feb 26 '17 at 23:26
6

Use the command builtin to call an external command, bypassing any function of the same name. Also:

  • What follows if is an ordinary command. To perform a string comparison, invoke the test built-in, or the [ builtin with the same syntax plus a final ], or the [[ … ]] construct which has a more relaxed syntax. See using single or double bracket - bash for more information.
  • The -eq operator is for comparing integers. Use = to compare strings.
  • I added some limited support for passing multiple arguments (including options such as -p). Below I call cd only if mkdir succeeded, and I call it on the last argument passed to the function.
  • -g will only be recognized as the very first argument. Parsing options to locate and remove it from whatever position it's in is possible, but harder.
mkdir () {
  local do_cd=
  if [ "$1" = "-g"]; then
    do_cd=1
    shift
  fi
  command mkdir "$@" &&
  if [ -n "$do_cd" ]; then
    eval "cd \"\${$#}\""
  fi
}

I don't recommend defining your custom options. They won't be shown in --help or man pages. They aren't more memorable than defining a command with a different name: either you don't remember that you have them, and there's no advantage compared to a command with a different name, or you don't remember that they're custom, and then you can find this out immediately with a custom name by asking the shell whether it's a built-in command or function (type mkcd), which is not the case with a custom option. There are use cases for overriding a standard command, but this is not one.

| improve this answer | |
  • also typing mkdir -g won't autocomplete, but mkcddir or whatever might. – Azor Ahai -- he him Feb 27 '17 at 6:38

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.