11 rm Command Examples For Linux

The ‘rm’ command is used to delete files and directories, rm comes from the GNU Coreutils package and should be available by default in Unix/Linux based operating systems.

By the end of this guide you should know how to use rm to remove files and directories in Linux by following these practical examples.

How To Use rm – Command Examples

  • 1. Remove A File

    With the rm command we can specify a file that we wish to delete, by default we will be prompted to confirm that we actually want to delete the file with ‘y’ for yes or ‘n’ for no.

    [root@centos7 test]# rm test.txt
    rm: remove regular file ‘test.txt’? n
  • 2. Force Removal

    Rather than being prompted for confirmation, we can specify the -f option to force the removal without any further prompt.

    [root@centos7 test]# rm -f test.txt
  • 3. Remove Recursively

    We can recursively delete files and directories within a specified directory with the -r option.

    First we’ll create the dir1 directory, which then contains the sub directory dir2 and then dir3 within that, as well as some empty test files within each.

    [root@centos7 test]# mkdir -p dir1/dir2/dir3
    [root@centos7 test]# touch dir1/file dir1/dir2/file dir1/dir2/dir3/file

    After this we run rm with the -r option and also make use of the previously discussed -f option so that we are not prompted for every single file and directory.

    [root@centos7 test]# rm -rf dir1/

    This is extremely powerful and potentially dangerous, be careful when using rm with the -rf option and always double check that you have specified the correct directory to remove. I highly suggest that you always specify the full path that you wish to delete rather than using relative paths to help avoid mistakes.

  • 4. Remove A Directory

    By default the rm command does not remove directories unless we also specify the -d option, however this will only work for empty directories.

    [root@centos7 test]# mkdir dir
    [root@centos7 test]# rm -d dir/
    rm: remove directory ‘dir/’? y

    If the directory has files or sub directories within, we will need to remove the directory with the -r option to delete everything recursively. In the following example we recreate the ‘dir’ directory and create an empty file inside with the touch command. We can see that rm -d fails as the directory is not empty, so we use -r which confirms we want to enter the directory, remove the content within, and then finally remove the directory itself.

    [root@centos7 test]# mkdir dir
    [root@centos7 test]# touch dir/test-file
    [root@centos7 test]# rm -d dir/
    rm: cannot remove ‘dir/’: Directory not empty
    [root@centos7 test]# rm -r dir/
    rm: descend into directory ‘dir/’? y
    rm: remove regular empty file ‘dir/test-file’? y
    rm: remove directory ‘dir/’? y
  • 5. Prompt For Removal

    As we have noted previously, by default rm will prompt us for confirmation before proceeding, this can be enabled with the -i option. The reason that -i is used by default is because it’s set as an alias automatically.

    [root@centos7 ~]# alias | grep rm
    alias rm='rm -i'

    If we wanted to we could remove the -i option from the alias which is the part that actually prompts and makes us confirm, it’s there by default as a safety measure to help avoid accidental deletion.

  • 6. Prompt Once

    In a similar manner to -i, we can use the -I option to prompt once before removing more than 3 files which is considered less intrusive than -i but still gives some protection against most mistakes.

    In this example we delete 3 directories and 3 files but only get prompted once, which is a nice in between option of confirming everything with -i and having nothing confirmed with -f.

    [root@centos7 test]# mkdir -p dir1/dir2/dir3
    [root@centos7 test]# touch dir1/file dir1/dir2/file dir1/dir2/dir3/file
    [root@centos7 test]# rm -rI dir1/
    rm: remove 1 argument recursively? y
  • 7. Ignore Other File Systems

    The --one-file-system option is used when removing recursively and will skip any directory that is on a different file system to the one specified as an argument. This can be very useful if we have different file systems such as secondary disks or NFS mounts mounted somewhere to our system, by default a ‘rm -rf /’ would descend into these and remove those contents too, this option will prevent this.

    In this example we have added a new virtual hard disk /dev/sdb1 and mounted it to /root/test/mount. When we attempt to remove everything in /root/test, the mount directory is skipped as it’s a separate file system.

    [root@centos7 test]# mount /dev/sdb1 mount/
    [root@centos7 test]# rm -rf --one-file-system /root/test/
    rm: skipping ‘/root/test/mount’, since it's on a different device
  • 8. Preserve Root Directory

    By default rm is run with the --preserve-root option which will not remove the ‘/’ directory which can help prevent accidental file system deletion.

    [root@centos7 test]# rm -rf /
    rm: it is dangerous to operate recursively on ‘/’
    rm: use --no-preserve-root to override this failsafe

    We can disable this with the --no-preserve-root option which does not treat ‘/’ in a special way, of course if we do this and delete the content of ‘/’ you will essentially destroy your system, so it’s not a good idea to do this unless you want to trash your system.

  • 9. Verbose Output

    With the -v option we can print out additional verbose information including a list of everything that has been removed. In this example we first create a directory with some sub directories with empty test files and remove the lot, we can see that a list of what has been removed is printed out.

    [root@centos7 test]# mkdir -p dir1/dir2/dir3
    [root@centos7 test]# touch dir1/file dir1/dir2/file dir1/dir2/dir3/file
    [root@centos7 test]# rm -rfv dir1/
    removed ‘dir1/dir2/dir3/file’
    removed directory: ‘dir1/dir2/dir3’
    removed ‘dir1/dir2/file’
    removed directory: ‘dir1/dir2’
    removed ‘dir1/file’
    removed directory: ‘dir1/’
  • 10. Display Version

    We can display the version of rm installed with the --version option.

    [root@centos7 ~]# rm --version
    rm (GNU coreutils) 8.22
    Copyright (C) 2013 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later .
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    Written by Paul Rubin, David MacKenzie, Richard M. Stallman,
    and Jim Meyering.
  • 11. Display Help

    We can show useful help information if we need further details on any of this with the --help option. Note that the full output of the command is not shown for brevity.

    [root@centos7 ~]# rm --help
    Usage: rm [OPTION]... FILE...


The practical examples here have shown many ways that the rm command can be used to delete files and directories in Linux.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>