Building Bash Muscle (11)

This article is going to be pretty basic but it’s always good to go back to the basics.

You can create a new directory like this.

mkdir foo1

That was too easy. What if you want to create multiple directories at a time?

mkdir foo1 foo2 foo3

That’s all good but what if you want to create a tree structure? like foo1/foo2/foo3? Use the -p option to create a tree structure.

mkdir -p foo1/foo2/foo3

-p option is “no error if existing, make parent directories as needed” according to the man page. Here is the tree view of the directory structure that was just created by the command.

What if you want to remove foo1? rmdir command is available to remove a directory.

rmdir foo1

That didn’t work… What happened?

Well, if there is any object such as sub directory or file in a directory, you rmdir cannot remove the directory. If you read the man page for rmdir, the first line says “rmdir – remove empty directories”.

If you want to remove all foo1/foo2/foo3 directories, here is what you can do with rmdir.

rmdir -p foo1/foo2/foo3

Hmm, this is still cumbersome. I want to be able to express it like “rmdir foo1” and remove everything including files and sub directories under foo1. Here is what you can do.

rm -rf foo1

rm -rf is really powerful so be careful when using it.

If you want to remove everything in a directory, execute the following command.

rm -rf thediriwannaempty/*

To recap, we went over how to create and remove directories in bash.

 

Author: admin

A software engineer in greater Seattle area

Leave a Reply

Your email address will not be published. Required fields are marked *