Deleting Files and Directories

Deleting Files and Directories

In Unix, when you remove a file or directory, it is gone forever.

In Unix, when you remove a file or directory, it is gone forever.

In Unix, when you remove a file or directory, it is gone forever.

Are we clear on this? The file is not moved to the Trash, where you can go back and retrieve it later. Yes, it is theoretically possible to recover a deleted file, but unless you're with the CIA and have a government agency behind you, don't count on it. (See the sidebar "To Use the Trash from the Command Line.")

To remove a file:

  • rm path

    You can remove multiple files by supplying multiple paths as arguments; for example, the following would remove the two named files and all files in the current directory whose names end in .jpg:

    rm file1 file2 *.jpg

To remove an empty directory:

  • rmdir path

    If the directory named by path is not empty, you get an error message. As with many commands, you can supply multiple file paths.

To remove a directory and everything inside it:

  • Use the -r option with rm . For example:

    rm -r file1 directory1/ file2

    Table 5.3 shows the options for the rm command.

    Table 5.3. Options for rm




    Remove empty directories as well as files.


    Recursively remove directories and their contents; implies -d .


    Force attempts to override permission restrictions and does not report errors.


    Inquire before removing each file; type a y to confirm.


  • Be extremely careful with rm . Consider moving the file(s) to ~/.TRash instead.

  • Be especially careful when using wildcards in arguments; for example, rm * would remove every file (except dot files) from the current directory. Think twice before doing that.

  • Some people like to configure their shells so that rm is always used with the -i option. See Chapter 7 to find out how to create an alias of rm in your shell so that it becomes rm -i .

To Use the Trash from the Command Line

When you use the Finder, the files you put in the Trash are actually moved into a "hidden" directory in your home directory, called .TRash (in Unix, files whose name starts with a . are not normally displayed).

Instead of deleting files, you can move them to the Trash with

mv -i filename ~/.TRash

The -i (for inquire ) option prompts you before overwriting a file with the same name in the .TRash directory. This is different from the Finder behavior in which the filename of the newly trashed file gets changed, instead of the older trashed file of the same name being deleted. (See Chapter 9, "Creating and Using Scripts," for a script that emulates the Finder behavior.)

Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: