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:

SYNOPSIS
rm [options] file...

POSIX options: [-fiRr] [--]

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

DESCRIPTION
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.

bill@slackware:~$ mkdir labi
bill@slackware:~$ ls
labi/ labu/
owner/

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

bill@slackware:~$ touch labi/sejarah.txt
bill@slackware:~$ ls labi
sejarah.txt

Now use 'rm' to delete labi directory:

bill@slackware:~$ rm labi
rm: cannot remove `labi': Is a directory
bill@slackware:~$ rmdir labi
rmdir: `labi': Directory not empty
bill@slackware:~$

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.

bill@slackware:~$ rm -r labi
bill@slackware:~$ ls
labu/ owner/

bill@slackware:~$

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:

bill@slackware:~$ rm -rf labu
bill@slackware:~$

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.