rm Command in Linux with Examples

Posted in

rm Command in Linux with Examples
sangeeta.gulia

Sangeeta Gulia
Last updated on April 25, 2024

    Directory deletion is a common task in Linux. We may need to delete a set of directories or files with a single command. In this article, We'll show you how to use the rm command with examples and explanations of the most common rm options.

    rm Command in Linux

    The abbreviation rm stands for "remove here". rm is a command-line tool that allows you to delete files and directories. To be more specific, rm deletes all references to things from the disc, even if those items have multiple references. It does not remove directories by default. It is one of the most important commands that any Linux user should know. This operation usually works quietly, and you should take caution while using it because after you delete files, you won't be able to recover the contents of those files or directories.

    The syntax for this command is

    $ rm <option> <file names separated by spaces>

    1. -i (Interactive Deletion)

    The -i option causes the command to ask for confirmation before deleting each file; you must press y to confirm the deletion; any other key leaves the file as it is. Below is the example for the command

    $ rm -i demo.txt

    rm: remove regular empty file 'demo.txt'? Y

    2. -d (Directory Deletion)

    We can use the -d option to delete an empty set of directories

    $ rm -d dirname

    3. -f (Force Deletion)

    If a file is write protected, rm will ask for confirmation before removing it. The -f option overrides this small safeguard and deletes the file without warning.

    The syntax is as follows-

    $ rm -f demo.txt

    4. -r (Recursive Deletion)

    The rm command does a tree-walk and deletes all files and sub-directories of the parent directory recursively when the -r (or -R ) option is used. It deletes anything it discovers at each stage. Normally, rm would not remove the directories, but it will if you use this option.

    The syntax is as follows-

    $ rm -r *

    5. –version

    This option displays the version of rm that is presently installed on your system.

    The syntax is as follows-

    $ rm --version

    6. -rf

    The rm command will ask you to confirm the operation if the specified directory or a file within it is write-protected. Use the -f option to remove a directory without being prompted.

    $ rm -rf <directory>

    Conclusion

    In this article, we went through various options related to the rm command. We realize that this command can be used to get its version, perform the recursive deletion, force deletion, interactive deletion, etc.

    Leave a Comment on this Post

    0 Comments