Removing Directories with rmdir


Removing Directories with rmdir

Another handy thing you can do is to remove directories using rmdir. Think of removing directories as trimming branches on a tree. That is, you can't be sitting on the branch you want to trim off. You have to sit on the next closest branch; otherwise, you'll fall to the ground along with the branch you trim off. Ouch! Similarly, when you remove a directory, you must not be located in the directory you want to remove.

You must remove a directory's contents (all subdirectories and files) before you remove the directory itself. In doing so, you can verify what you're removing and avoid accidentally removing important stuff. In the following steps (illustrated in Code Listing 2.10), we'll show you how to remove a directory's contents, then remove the directory itself.

Code Listing 2.10. Removing directories with rmdir can be a little tediousbut better safe than sorry.

$ cd /home/ejr/Yourdirectory $ ls -la total 7 drwxrwxr-x      2 ejr      users     1024 Jun 29 20:59 . drwxrwx-       8 ejr      users     1024 Jun 29 20:59 .. -rw-rw-r       1 ejr      users     1475 Jun 29 20:59 cancelled.project.notes -rw-rw-r       1 ejr      users     2876 Jun 29 20:59 outdated.contact.info $ rm * $ cd .. $ rmdir Yourdirectory $ ls Newdirectory    all.programs.txt     newer.programs         short.fortunes Projects        files                newest.programs        temp Xrootenv.0      fortunes             newstuff               touching all.programs local.programs.txt schedule $ 

Tips

  • You can remove multiple directories at one time. Assuming you're starting with empty directories, just list them like this: rmdir Yourdirectory Yourotherdirectory OtherDirectory

  • As an alternative to rmdir, you can remove a directory and all of its contents at once using rm with the -r flag; for example, rm -r Directoryname. Be careful, though! This method automatically removes the directory and everything in it, so you won't have the opportunity to examine everything you remove beforehand. If you're getting asked for confirmation before deleting each file and you're really, absolutely, positively, completely sure that you're doing the right thing, use rm rf Directoryname to force immediate deletion.

  • If you're getting comfortable with long command strings, you can specify commands with a complete directory path as in ls /home/ejr/DirectorytoGo or rm /home/ejr/DirectorytoGo/*. This technique is particularly good if you want to be absolutely sure that you're deleting the right directory, and not a directory with the same name in a different place on the system.


To Remove a Directory:

1.

cd /home/ejr/Yourdirectory

To begin, change to that directory by typing cd plus the name of the directory you want to remove.

2.

ls -a

List all (-a) of the files, including any hidden files that might be present, in the directory, and make sure you don't need any of them. If you see only . and .. (which indicate the current directory and its parent directory), you can skip ahead to step 4.

3.

Do one or both of these:

  • If you have hidden files in the directory, type rm .* * to delete those files plus all of the rest of the files.

  • If you have subdirectories in the directory, type cd and the subdirectory name, essentially repeating the process starting with step 1. Repeat this process until you remove all subdirectories.

When you finish this step, you should have a completely empty directory, ready to be removed.

4.

cd ..

Use the change directory command again to move up one level, to the parent of the directory that you want to remove.

5.

rmdir Yourdirectory

There it goeswave goodbye to the directory! See Code Listing 2.10 for the whole sequence.




Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net