The rm command is a very powerful command. We learned how to delete an empty folder in my previous post, the rmdir command guide. It'd take some times if you are going to delete a folder with lots of contents. But don't worry. The rm command can solve that problem.
Here are basic information about rm command:
rm - remove files or directories
rm format:
rm [OPTION]... FILE...
Example of rm command used with some important options:
Example 1 - Ubuntu delete file using rm with no option.
luzar@ubuntu:~$ ls -l
total 4
-rw-r--r-- 1 luzar luzar 0 2011-05-21 21:36 file.txt
drwxr-xr-x 3 luzar luzar 4096 2011-05-21 21:37 folder
luzar@ubuntu:~$ rm file.txt
luzar@ubuntu:~$ ls -l
total 4
drwxr-xr-x 3 luzar luzar 4096 2011-05-21 21:37 folder
luzar@ubuntu:~$
The rm command with no option deletes a file specified.
Example 2 - Ubuntu delete file using rm with -r option.
luzar@ubuntu:~$ ls -l
total 4
drwxr-xr-x 3 luzar luzar 4096 2011-05-21 21:37 folder
luzar@ubuntu:~$ rm -r folder/
luzar@ubuntu:~$ ls -l
total 0
luzar@ubuntu:~$
The rm command by default cannot delete a directory. Use -r or -R option to remove directories and their contents recursively.
Example 3 - Ubuntu delete file using rm with -i option.
luzar@ubuntu:~$ ls -l
total 4
-rw-r--r-- 1 luzar luzar 0 2011-05-21 22:16 file.txt
drwxr-xr-x 3 luzar luzar 4096 2011-05-21 22:16 folder
luzar@ubuntu:~$ rm -i folder/
rm: cannot remove `folder/': Is a directory
luzar@ubuntu:~$ rm -ri folder/
rm: descend into directory `folder/'? n
luzar@ubuntu:~$
Here are some interesting examples. First, I list the directory content. Then I proved that rm cannot delete a directory unless -r or -R is given. Finally, I used rm -ri and we can see that -i option prompts for confirmation before deleting a file/directory. You can answer with y for yes or n for no.
Example 4 - Ubuntu delete file using rm with -f option.
luzar@ubuntu:~$ ls -l
total 4
-rw-r--r-- 1 luzar luzar 0 2011-05-21 22:16 file.txt
drwxr-xr-x 3 luzar luzar 4096 2011-05-21 22:16 folder
luzar@ubuntu:~$ rm -f folder
rm: cannot remove `folder': Is a directory
luzar@ubuntu:~$ rm -rf folder/
luzar@ubuntu:~$ ls -l
total 0
-rw-r--r-- 1 luzar luzar 0 2011-05-21 22:16 file.txt
luzar@ubuntu:~$
The -f option does not prompt for confirmation. It is used to force delete. This option is very useful when deleting a directory full of contents. Just don't use rm -rf / with root privilege because that will completely remove all Ubuntu file systems.
Example 5 - Ubuntu delete file using rm with -v option.
luzar@ubuntu:~$ ls -R
.:
file.txt folder
./folder:
new_files.txt new_folder
./folder/new_folder:
luzar@ubuntu:~$ rm -rv folder/
removed directory: `folder/new_folder'
removed `folder/new_files.txt'
removed directory: `folder'
luzar@ubuntu:~$
If you don't want to be prompt but need to see what files or directories you deleted, then use -v option. From the example above, I list the directory content with ls -R to show subdirectory recursively. Then I used rm -rv to delete all contents in the folder verbosely.