Removing Files with rm

A task that needs to be performed on a regular basis is cleaning up the file system. For this purpose, you can use the rm command. For example, use rm /tmp/somefile to remove somefile from the /tmp directory. If you are root and have all the proper permissions on the file, you will succeed without any problems. (See Chapter 6 for more about permissions.) Since removing files can be delicate (imagine removing the wrong files), it may be necessary to push the rm command a little. You can do this by using the -f (force) switch. For example, use rm -f somefile if the command complains that somefile cannot be removed.

You can use the rm command to wipe entire directory structures as well; in this case, you have to use the -r option. If you combine this option with the -f option, the command will become very powerful. For example, use rm -rf /somedir to clear out the entire content of /somedir. You should be careful when using rm this way, especially since a small typing mistake can have serious consequences. Imagine, for example, that you type rm -rf / somedir (with a space between / and somedir) instead of rm -rf /somedir. As a result, the rm command will first remove everything in / and, once finished, will remove somedir as well. You should understand that the second part of the command is no longer required once the first part of the command has completed.

Was this article helpful?

0 0

Post a comment