rm command tutorial in linux/unix with examples and use cases

linux rm command – remove files or directories

The linux rm command can remove each specified file. By default, the rm command cannot remove directories. if you need to remove directories, you need to use parameter.

Syntax

rm [OPTION]... [FILE]...

Options

    • -f, –force
      ignore nonexistent files and arguments, never prompt

    • -i
      prompt before every removal

    • -I
      prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still  giving  protection against most mistakes

    • –interactive[=WHEN]
      prompt according to WHEN: never, once (-I), or always (-i); without WHEN, 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 ‘/’ specially

    • –preserve-root[=all]
      do not remove ‘/’ (default); with ‘all’, reject any command line argument on a separate device from its parent

    • -r, -R, –recursive
      remove directories and their contents recursively
    • -d, –dir
      remove empty directories

Examples

Remove specified files

In the following example, we remove the file named test.log.

➜ rm test.log

Using regular, remove every matching file

In the following example, we remove each file that starts with the string “test”.

➜ rm test*

Remove directory

In the following example we remove the empty directory. If the directory is not empty, an error will be reported “rm: cannot remove ‘dir’: Directory not empty”

➜ rm -d dir

In the following example, we will remove a non-empty directory. Of course, you can also remove empty directories.

➜ rm -r dir

Remove files using interactive mode

In the following example, we are prompted to remove files.

➜ rm -i w-test.log

Remove files using force mode

In the following example, we use force to remove a file, even if the file does not exist, no error is reported.

➜ rm -f w.log

 

Reference

Linux common commands tutorial and use examples

 

 

 

Add a Comment

Your email address will not be published. Required fields are marked *