Deleting Files and Folders

Written by Chris Gregg, with modifications by Nick Troccoli

Click here for a walkthrough video.

To remove a file from a linux system, use the rm command:

$ rm hello.c
rm: remove regular file 'hello.c'? y
$

This is a permanent removal; there is no trash can with the ability to recover a file. On myth, you will be prompted to remove a file, but on most linux systems, this is not the default behavior, so be careful.

If you want to remove an entire directory, you have some options. You can use the -r (recursive) option alone, but you will have to manually answer 'y' for each file:

$ rm -r assign7
rm: descend into directory ‘assign7’? y
rm: remove regular file ‘assign7/readme.txt’? y
rm: remove regular file ‘assign7/hello.c’? y
rm: remove directory ‘assign7/innerFolder’? y
rm: remove regular file ‘assign7/hello’? y
rm: remove symbolic link ‘assign7/samples’? y
rm: remove regular file ‘assign7/helloOutput.txt’? y
rm: remove regular file ‘assign7/look.html’? y
rm: remove directory ‘assign7’? y  
$

While this is safe, it is also annoying. So, you can use the -f flag to "force" remove the files, without prompting. However, be very careful with the -rf flags. You don't get any second chances (and you might have to resort to the backup), and if you type it incorrectly in the wrong directory, you can wipe out many files you don't want to destroy.

But, it can be an important command:

$ rm -rf assign7
$

This will also remove the directory itself.

Another choice to remove an empty directory is to use the rmdir command:

$ rmdir assign7