Saturday, 20 August 2011

Removing files and directories

To delete (remove) a file, use the rm command. As an example, we are going to create a copy of the science.txt file then delete it.

Inside your unixstuff directory, type
% cp science.txt tempfile.txt
% ls
% rm tempfile.txt
% ls

You can use the rmdir command to remove a directory (make sure it is empty first). Try to remove the backups directory. You will not be able to since UNIX will not let you remove a non-empty directory.

rmdir mydir
Removes the directory mydir 

rm -r directory
Remove a directory, even if files existed in that directory.
 
And for extra measures (since files rm'd cannot be recovered easily if at all),

rm -rfv /path/to/dir

The -f forces the deletion (if you're allowed to delete the files) and the -v shows verbose information (tells you what is being deleted).

If you're sudo'd and you're using rm, be sure to check what you're deleting and make sure the path is correct. 

No comments:

Post a Comment