The rmdir Command

The rmdir command will delete an empty directory. What this means is that the directory you want to remove cannot contain any data within it.

To remove a directory, do the following:

1.
Decide which directory, or directories, you want out of your way.

2.
Issue the rmdir command as rmdir <directory>.

Don't Forget! You Can Do Many At Once! Don't forget that you can remove multiple directories at one time. As with most file management commands, specify the directories in sequence and they will be removed. For example, if you wanted to remove “test” and “test1,” the two directories could be removed with one issuance of the rmdir command. You could rmdir “test” and “test1” by typing rmdir test test1.


When you issue the rmdir command, you will remove the test directory you created before:

> rmdir test

That's it! That was pretty easy to do. You should now be able to list the contents in your current directory and not see the test directory anywhere. It has been removed. If you had any problems, you may need to make the directory you want to remove from the Unix system “empty.” In some cases, you must use rm * first to make the directory empty, and then you can delete it. As this is advanced syntax better covered in Lesson 13, “Regular Expressions,” when we cover regular expressions, you should put this * symbol (known as the asterisk, or wildcard) to memory, as you will most likely often use this command when working with Unix.

Now that we have mastered file creation and deletion, as well as directory creation and deletion, you should learn what combinations are most commonly used and helpful, as well as know the answers to the most commonly asked questions: Why would I make a directory empty before deleting it? Couldn't I delete the directory with the files in it? Yes, you may do so with the rm –r (recursive) command.

What Is This? The Same Command? Just like the remove files command, the recursive command is pretty much the same command—it just does something different when used in a different context (as in the context of directories), not files. When used this way, you can recursively remove all of the contents within that directory you specify.


The rm –r Command

Removing files and directories at the same time can be done when using the rm –r command specifying a directory instead of a file. The rmdir command will only work on empty directories, so you will need a way to remove the directory and any contents that may be within it. When you need this functionality, the rm command in recursive mode can be used.

To delete a directory and all its contents, do the following:

1.
Find the directory you want deleted.

2.
Issue the rm command as rm -r <directoryname>.

When you issue this command, you will be able to specify the directory you want to delete, including anything that may be in it:

> rm -r /priv/home/rob/test2

If you delete a directory you are currently in, you may get error messages. These may be fixed easily by moving to a known good directory, such as your home directory, symbolized by the tilde. The command you can enter to get back to home is cd ~/.

Wow, think about how far you have come! Not only are we navigating the Unix shell prompt, we are actually making and removing data from the Unix system, as well as becoming more familiar with how other commands tie in to the process. Now we will learn how to copy files from one location to another.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.232.95