InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Creating and Changing into a New Directory in One Step in bash
Bash

Creating and Changing into a New Directory in One Step in bash

InfinityCoder June 25, 2017

 

You often create new directories and immediately change into them for some operation, and all that typing is tedious.

Add the following function to an appropriate configuration file such as your ~/.bashrc file and source it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# cookbook filename: func_mcd
 
# mkdir newdir then cd into it
# usage: mcd (<mode>) <dir>
function mcd {
   local newdir='_mcd_command_failed_'
   if [ -d "$1" ]; then # Dir exists, mention that...
       echo "$1 exists..."
       newdir="$1"
   else
       if [ -n "$2" ]; then # We've specified a mode
          command mkdir -p -m $1 "$2" && newdir="$2"
       else # Plain old mkdir
          command mkdir -p "$1" && newdir="$1"
       fi
    fi
    builtin cd "$newdir"        # No matter what, cd into it
} # end of mcd

For example:

1
2
3
4
5
6
7
8
9
10
11
12
$ source mcd
 
$ pwd
/home/jp
 
$ mcd 0700 junk
 
$ pwd
/home/jp/junk
 
$ ls -ld .
drwx------ 2 jp users 512 Dec 6 01:03 .

This function allows you to optionally specify a mode for the mkdir command to use when creating the directory. If the directory already exists, it will mention that fact but still cd into it.

We use the command command to make sure that we ignore any shell functions for mkdir, and the builtin command to make sure we only use the shell cd.

We also assign _mcd_command_failed_ to a local variable in case the mkdir fails. If it works, the correct new directory is assigned.

If it fails, when the cd tries to execute it will display a reasonably useful message, assuming you don’t have a lot of _mcd_command_failed_ directories lying around:

1
2
3
$ mcd /etc/junk
mkdir: /etc/junk: Permission denied
-bash: cd: _mcd_command_failed_: No such file or directory

You might think that we could easily improve this using break or exit if the mkdir fails. break only works in a for, while, or until loop and exit will actually exit our shell, since a sourced function runs in the same process as the shell.

We could, however, use return, which we will leave as an exercise for the reader.

1
2
command mkdir -p "$1" && newdir="$1" || exit 1 # This will exit our shell
command mkdir -p "$1" && newdir="$1" || break # This will fail

You could also place the following in a trivial function, but we obviously prefer the more robust version given in the solution:

1
function mcd { mkdir "$1" && cd "$1"; }

BASH  – 2  list files, change directory, and navigation

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Getting Yesterday or Tomorrow with Perl in bash
You need to get yesterday or tomorrow’s date, and you …

Getting Yesterday or Tomorrow with Perl in bash

Restricting Guest Users in bash
The material concerning the restricted shell in this recipe also …

Restricting Guest Users in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap