- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Part I: Introduction to Mac OS X
- Chapter 1. Mac OS X Component Architecture
- Chapter 2. Installing Mac OS X
- Chapter 3. Mac OS X Basics
- Chapter 4. The Finder: Working with Files and Applications
- Chapter 5. Running Classic Mac OS Applications
- Part II: Inside Mac OS X
- Chapter 6. Native Utilities and Applications
- Chapter 7. Internet Communications
- Chapter 8. Installing Third-Party Applications
- Part III: User-Level OS X Configuration
- Chapter 9. Network Setup
- Chapter 10. Printer and Font Management
- Chapter 11. Additional System Components
- Part IV: Introduction to BSD Applications
- Chapter 12. Introducing the BSD Subsystem
- Chapter 13. Common Unix Shell Commands: File Operations
- Part V: Advanced Command-Line Concepts
- Chapter 14. Advanced Shell Concepts and Commands
- Chapter 15. Command-Line Applications and Application Suites
- Chapter 16. Command-Line Software Installation
- Chapter 17. Troubleshooting Software Installs, and Compiling and Debugging Manually
- Chapter 18. Advanced Unix Shell Use: Configuration and Programming (Shell Scripting)
- Part VI: Server/Network Administration
- Chapter 19. X Window System Applications
- Chapter 20. Command-Line Configuration and Administration
- Chapter 21. AppleScript
- Chapter 22. Perl Scripting and SQL Connectivity
- Chapter 23. File and Resource Sharing with NetInfo
- Chapter 24. User Management and Machine Clustering
- Chapter 25. FTP Serving
- Chapter 26. Remote Access and Administration
- Chapter 27. Web Serving
- Part VII: Server Health
- Chapter 28. Web Programming
- Chapter 29. Creating a Mail Server
- Chapter 30. Accessing and Serving a Windows Network
- Chapter 31. Server Security and Advanced Network Configuration
- Chapter 32. System Maintenance
- Appendix A. Command-Line Reference
- Appendix B. Administration Reference
Deleting Files
Removing files and directories with Unix are fairly straightforward tasks, so you need to know only two commands to accomplish them.
Removing Files and Directories: rm, rmdir
Now that you have filled up your directory with lots of files, it is time to learn how to clean it up a bit. You can remove a file with rm.
Our user nermal has decided that her file called myfile is no longer needed. To remove it she does the following:
[localhost:~] nermal% ls -l myfile
-rw-r--r-- 1 nermal staff 51 Apr 12 15:11 myfile
[localhost:~] nermal% rm myfile
remove myfile? Y
As you notice in this example, rm prompts for confirmation before removing the file. Your system might not be configured to make rm prompt you for the removal of files. Information on how to make it ask you (rm's interactive mode) is provided in Appendix B, and we very highly recommend that you follow the instructions to make your rm command interactive at the first possible opportunity. After you're a seasoned Unix user, you're welcome to run with rm in noninteractive mode by default, but until you've been certain that you're ready for a few years, it's probably safest to have it operate interactively by default.
Our user nermal has also decided that she is done with the data that she copied from user joray's directory, and that she wants to remove the entire directory. The easiest way to do this is to force rm to recursively remove the directory, using options -r for recursive, and -f to override the interactive mode she has enabled by default, as shown here:
[localhost:~] nermal% ls -ld tests-for-nermal
drwxr-xr-x 9 nermal staff 262 Apr 23 11:29 tests-for-nermal
[localhost:~] nermal% rm -rf tests-for-nermal
[localhost:~] nermal% ls -ld tests-for-nermal
ls: tests-for-nermal: No such file or directory
Removing a directory and all its contents using the recursive, and force options to rm is easy, but it is also silent and very, very fast. Remember to use those options only when you really mean it. Double-check everything before you run it. As you can see in the example, there is no recourse if you type the wrong thing.
The complete syntax and options for rm are in Table 13.11, the command documentation table.
Table 13.11. The Command Documentation Table for rm
| rm | Removes directory entries. |
rm [-f | -i] [-dPRrW] <file1>
<file2> ...
|
|
| -f | Forces the removal of files without prompting the user for confirmation. If the file does not exist, no error diagnostic is displayed. The -f option overrides any previous -i options. |
| -i | Invokes an interactive mode that prompts for confirmation before removing a file. The -i option overrides any previous -f options. |
| -d | Attempts to remove directories as well as other types of files. |
| -P | Overwrites regular files before deleting them. Files are overwritten three times before being deleted; first with byte pattern 0xff, and then 0x00, and then 0xff. |
| -R | Attempts to recursively remove files. Implies -d option. |
| -r | Same as -R. |
| -W | Attempts to undelete files. This option can be used to recover only files covered by whiteouts. |
| rm removes symbolic links, but not the files referenced by the links. | |
| Also, attempting to remove the files . and .. is an error. |
There is also a command available for removing directories: rmdir. Unfortunately, it is only useful for removing empty directories. Its complete syntax and options are in the command documentation table, Table 13.12.
Table 13.12. The Command Documentation Table for rmdir
| rmdir | Removes directories. |
rmdir [-p] <directory1>
<directory2> ...
|
|
| rmdir removes each <directory> argument specified, provided it is empty. Arguments are processed in the order listed on the command line. To remove a parent directory and subdirectories of the parent directory, the subdirectories must be listed first. | |
| -p | Attempts to remove the specified directory and its parent directories, if they are empty. |
Searching for Files, Directories, and More | Next Section

Account Sign In
View your cart