Creating a Directory with Bash to Test Commands

Published: 2022 February 24

Last edited: 2023 October 14

system administration Linux Bash shell command

Why create a test directory?

When learning to work with new commands or programs it makes sense to be careful.

In particular, command line tools can be especially powerful. They can allow the user to delete or overwrite data before they realize what has happened. Often, these effects will be irreversible.

One way to partially limit that risk is to work in a test directory when trying new commands. The scope of a command’s effects is often (but not always) bounded within a directory, so this can provide some protection against unintended consequences. It can make it easier to see the effects of the command while reducing the risk of impacting files you don’t want to alter.

Nothing offers absolute protection. Caution and care should still be exercised. But this is a sensible step to help avoid some trouble when learning to use new commands.

Test Setup: Creating a test directory

To begin, navigate to the directory that will contain your test directory.

We’ll use the mkdir command to create a new directory called “test-directory”:

mkdir -v test-directory

Then navigate to that directory by entering the command:

cd test-directory

Finally, to confirm that there aren’t any files in the directory that you could unintentionally alter, enter the command:

ls -l

You shouldn’t see any files and the output should resemble the following:

total 0

Now you can work in this test directory with some protection for your other files.

Test Cleanup: Removing the test directory

Once you have finished experimenting, you may wish to remove your test directory. If so, you can use the rmdir command.

Be aware that the rmdir command is powerful and can delete directories in a way that you can’t reverse. Make sure you understand its use before blindly applying the example below.

If you’re not comfortable with that, use a GUI or another method you’re familiar with to remove the directory.

Navigate to the directory that contains your test directory. If you’re currently in the test directory, enter the command:

cd ..

Then to delete test-directory enter the command:

rmdir -v test-directory

You should see the output:

rmdir: removing directory, 'test-directory/'

Copying Directory Structure With a Bash Script - With Help From ChatGPT AI

Published: 2024 January 22

Setting Up a Raspberry Pi as a Linux Print Server with CUPS

Published: 2024 January 19

Setting Up a Raspberry Pi Zero 2 W with Ubuntu Server

Published: 2024 January 04