rm command in Linux - Explained with examples

rm stands for remove. It is a command that allows you to remove files, folders, symlinks from the system. Deleted files and folders will not be moved to the recycle bin, but the rm command will delete them immediately. You must be careful before using this command.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux system commands queries.

In this context, we shall look into how to check the manual by using the rm command in Linux.

More about Linux rm command

Basically, the rm command removes (deletes) files.

rm removes each file specified on the command line. By default, it does not remove directories.

When rm is executed with the -r or -R options, it recursively deletes any matching directories, their subdirectories, and all files they contain.

The removal process unlinks a file name in a filesystem from its associated data, and marks that space on the storage device as usable by future writes. In other words, when you remove a file, the data in the file isn't changed, but it's no longer associated with a file name.

The data itself is not destroyed, but after being unlinked with rm, it becomes inaccessible. Remove your files wisely! It's not like putting something in the Windows Recycle Bin; once you rm a file or directory, there is no way to undo it.

What is the syntax for rm command ?

rm command syntax is given below:

$ rm [option]... file...
$ rm [-f | --force] {[-i | --interactive[=always]] | [-I | --interactive=once] |
   [--interactive=never]} [--one-file-system] [--no-preserve-root |
   --preserve-root] [-r | -R | --recursive] [-d | --dir] [-v | --verbose] 
$ rm --help
$ rm --version

For example, I have 2 files: a.txt and b.txt:

Now, To remove file a.txt, run the below command:

$ rm a.txt

This will remove a.txt file from the system.

Or you can remove multiple files, execute:

$ rm b.txt datefile.txt

Linux rm command Options

  • -f, --force - Ignore nonexistant files, and never prompt before removing.
  • -i - Prompt before every removal.
  • -I - Prompt once before removing more than three files, or when removing recursively. This option is less intrusive than -i, but still gives protection against most mistakes.
  • --interactive[=WHEN] - Prompt according to WHEN: never, once (-I), or always (-i). If WHEN is not specified, then prompt always.
  • --one-file-system - When removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument
  • --no-preserve-root - Do not treat "/" (the root directory) in any special way.
  • --preserve-root - Do not remove "/" (the root directory), which is the default behavior.
  • -r, -R, --recursive - Remove directories and their contents recursively.
  • -d, --dir - Remove empty directories. This option permits you to remove a directory without specifying -r/-R/--recursive, provided that the directory is empty. In other words, rm -d is equivalent to using rmdir.
  • -v, --verbose - Verbose mode; explain at all times what is being done.
  • --help - Display a help message, and exit.
  • --version - Display version information, and exit.

Examples of using rm command

1. -i: must confirm before deleting

For example, We will remove file c.txt:

$ rm -i c.txt

You must press y to confirm.

2. -r: remove a directory

For example, We will remove directory snort3:

$ rm -r snort3

3. Remove all files in the working directory. If it is write-protected, you will be prompted before rm removes it:

$ rm -f *

4. Remove all files in the working directory. rm will not prompt you for any reason before deleting them:

$ rm -i *

5. Attempt to remove every file in the working directory, but prompt before each file to confirm:

$ rm -I *

6. Remove every file in the working directory; prompt for confirmation if more than three files are being deleted.

$ rm -r mydirectory

Remove the directory mydirectory, and any files and directories it contains. If a file or directory that rm tries to delete is write-protected, you are prompted to make sure you want it deleted.

$ rm -rf mydirectory

[Need to fix Linux system file permission issues ? We can help you. ]

This article covers how to use the rm command in Linux. In fact, the rm command is used for removing/deleting files and directories.

How to remove directories using rm command?

If you are trying to remove a directory, then you need to use the -r command line option. Otherwise, rm will throw an error saying what you are trying to delete is a directory:

$ rm -r [dir name]

For example:

$ rm -r testdir

How to make rm prompt before every removal ?

If you want rm to prompt before each delete action it performs, then use the -i command line option: 

$ rm -i [file or dir]

For example, suppose you want to delete a directory 'testdir' and all its contents, but want rm to prompt before every deletion, then here's how you can do that:

$ rm -r -i testdir

Related Posts