Wednesday, 31 May 2017

How to Perform mkdir and cd using a single command

Sometimes when you create a new directory, you may cd to the new directory
immediately to perform some work as shown below.


# mkdir -p /tmp/subdir1/subdir2/subdir3
# cd /tmp/subdir1/subdir2/subdir3
# pwd
/tmp/subdir1/subdir2/subdir3
Wouldn’t it be nice to combine both mkdir and cd in a single command? Add
the following to the .bash_profile and re-login.
$ vi .bash_profile
function mkdircd () { mkdir -p "$@" && eval cd
"\"\$$#\""; }
Now, perform both mkdir and cd at the same time using a single command as
shown below:
13

# mkdircd /tmp/subdir1/subdir2/subdir3
[Note: This creates the directory and cd to it
automatically]
# pwd
/tmp/subdir1/subdir2/subdir3

No comments:

Post a Comment

What is PS2 - Continuation Interactive Prompt in Linux

A very long command can be broken down to multiple lines by giving \ at the end of the line. The default interactive prompt for a multi-lin...