Linux rm command - delete file and directory in Linux

Delete file and directory in Linux is regular whether you do server administration job or using Linux desktop as an end user. Linux is different from windows operating system, where permission and security is very important in Linux and a default part of the system. Root user has full power like administrator in windows operating system, where you can delete anything and everything in the system. Because root user is god in Linux, we were advised to do our normal job as a normal user. Being a normal user, we can only delete user file and directory and that does not affect the Linux system. That also means, we don't have privilege to remove other users' files as well. If your duty needed you to clean system files and directory, as well as user data, (such as maintain Linux ftp server) then switch to root user or gain root privilege only when you have to.

Linux rm command

The Linux rm command is used to delete file or directory in Linux. There is no delete or del command like DOS. Normally, to remove a directory in Linux, we would use the rmdir command. But that will only works if the directory is empty. Here's where the Linux rm command will come into part. It has lots of useful options too. Here's the synopsis and description from the Linux rm command's manual page which explains all about the command:

rm [options] file...

POSIX options: [-fiRr] [--]

GNU options (shortest form): [-dfirvR] [--help] [--version] [--]

rm removes each given file. By default, it does not remove directories.
But when the -r or -R option is given, the entire directory tree below the
specified directory is removed (and there are no limitations on the depth
of directory trees that can be removed by `rm -r'). It is an error when
the last path component of file is either . or .. (so as to avoid
unpleasant surprises with `rm -r .*' or so).

We can use Linux rm command to delete a normal file without using any option. While to delete a directory, we can use rm -r option. Let's see some examples.

Examples on how to delete files and directory using Linux rm command

First, make sure you login as a normal user.

Create a directory labi in user's home directory for this example.

[email protected]:~$ mkdir labi
[email protected]:~$ ls
labi/ labu/

Create a file named sejarah.txt in labi directory. Here's a tip to create a simple file in linux, use touch command:

[email protected]:~$ touch labi/sejarah.txt
[email protected]:~$ ls labi

Now use 'rm' to delete labi directory:

[email protected]:~$ rm labi
rm: cannot remove `labi': Is a directory
[email protected]:~$ rmdir labi
rmdir: `labi': Directory not empty
[email protected]:~$

What's wrong? We can't delete labi directory with rm, ok. It's a directory, so use rmdir instead. But that's also failed, labi directory is not empty. Remember that rmdir only remove an empty directory? To remove a directory, the '-r' option is needed. So use rm -r option to delete a directory and all files in it.

[email protected]:~$ rm -r labi
[email protected]:~$ ls
labu/ owner/

[email protected]:~$

What if we have a large directory with many child directories and files in it? Linux will prompt for a confirmation to delete every files. So the rm -r option seem inefficient this time. In this situation we use the -f option together with -r. Here's and example:

[email protected]:~$ rm -rf labu
[email protected]:~$

If we use the rm -rf option to remove a directory, Linux will remove the directory and its child without bothering us. The -f option means, just delete and don't ask for my permission. I know what I'm doing. Well, that's true. Only use the -f option if you know what you are doing. My advice is, never issue the rm -rf / command as a root because that will remove all your Linux system.

Back to Linux basic commands main page.

Add new comment