Linux tip for DevOps💡: how to create a directory and move into it with one command.

Andrey Byhalenko
DevOps Manuals and Technical Notes
2 min readFeb 20, 2024

In this short article, I will share with you the way you can create a directory and move into it with one command.

I will use my Ubuntu 22.04 server to demonstrate this.

Instead of doing this:

mkdir mydir
cd mydir

Do the following:

  • Use any editor you want to add the mkcd function to the bottom of the bashrc file.
# edit the bashrc file
vim ~/.bashrc

# add the mkcd function to the bottom of the file
function mkcd {
if [ ! -n "$1" ]; then
echo "Enter mkcd followed by a directory name"
elif [ -d $1 ]; then
echo "\`$1' already exists"
else
mkdir $1 && cd $1
fi
}

# save the file and exit
  • Reload the bashrc settings by executing the following command:
. ~/.bashrc
  • Call the mkcd function next time you need to create a folder.

If you liked my articles, you can join my newsletter, and you will receive weekly DevOps tutorials, articles, and tips every Saturday.

As a bonus, you will receive a free step-by-step DevOps CI/CD project, which you can use in your portfolio.

Subscribe here: https://junior-devops-hub.ck.page

--

--

Andrey Byhalenko
DevOps Manuals and Technical Notes

I'm a DevOps Engineer, Photography Enthusiast, and Traveler. I write articles aimed at junior DevOps engineers and those aspiring to become DevOps engineers.