Removing a file or directory
Note: When you remove a file or directory using Unix, it is
permanently removed.
There is no way to recover it!
Removing a file
You can remove or erase a file using the rm (remove)
command. After you enter the command the system will ask you if you're
sure you want to erase the file. You can then type y if
you still want to erase the file or n if you've changed
your mind. It's always a good idea to doublecheck at this point to ensure
that you are deleting the right file (that you didn't make a mistake when
typing its name). Once you delete the file, it's gone for good. The DOS
equivalent for this command is del or erase.
The process
of removing a file looks like this:
unity% rm test.dat
rm: remove test.dat? y
unity%
After you press the Return key, your prompt appears again, and it seems
like nothing has happened. However, as a general rule, if you don't get
an error message, you can assume that the command worked. You could use
the ls command to verify that the file was erased.
Be careful about using the rm command - you don't want
to accidentally delete your math homework or term paper!
Removing a directory
You can remove a directory with the rmdir (remove directory)
command. For example, to remove a directory named biology, you
would enter the following command:
rmdir biology
However,if the biology directory contained any files you'd get the following
message:
rmdir: biology: Directory not empty
As a safeguard, directories must be empty before they can be removed.
Also, you cannot be inside the directory you are trying to remove.
To remove the biology directory, you would first remove all the files
in it. Then you could remove the directory itself.
To quickly remove all files in a directory, you could use the wildcard
symbol, the asterisk (*). When you use this option, the system will prompt
you for permission to delete a file before actually doing so. Answer "y" to
delete the file or "n" to not delete.
In the above example, you would change to the "biology" directory and
issue the rm command and then respond to the prompts as shown below:
unity% cd biology
unity% rm *
rm bio_homework1? y
rm bio_notes? y
rm test3.txt? y
unity% cd ..
unity% rmdir biology
To remove a directory and all files and directories within it, you can
use the -r option of rm to recursively delete
the files in the directory, its subdirectories and, finally, the directory
itself.
The DOS equivalent of the rmdir command is rd or rmdir.
Last modified
July 20, 2004
by cawalker
|