×

rm Command in Linux/Unix with Examples

The "rm" command in Linux (and other Unix-like operating systems) is used to remove files and directories. The "rm" command stands for "remove" and it is used to delete files and directories with the specified name or path. The basic syntax of the "rm" command is:

rm [options] file_or_directory

When executed without any options, the "rm" command will delete the specified file or directory without any prompt or warning. For example, the following command will delete a file named "example.txt" in the current working directory:

rm example.txt

The "rm" command also has several options that can be used to control how files and directories are deleted. The most commonly used options are:

-f : force option, which can be used to delete files and directories even if they are write-protected. This option is useful when trying to delete files or directories that are in use by another process. For example, the following command will delete the file "example.txt" even if it is write-protected:

rm -f example.txt

-r : recursive option, which can be used to delete a directory and its contents. This option is useful when trying to delete a directory and all of its subdirectories and files. For example, the following command will delete the directory "example" and all of its contents:

rm -r example

-i : interactive option, which can be used to prompt for confirmation before deleting each file. This option is useful when trying to delete multiple files and you want to review each file before it is deleted. For example, the following command will prompt for confirmation before deleting each file in the directory "example":

rm -i example/*

-v : verbose option, which can be used to display the name of each file as it is deleted. This option is useful when trying to see the progress of the delete operation. For example, the following command will delete the file "example.txt" and display the name of the file as it is deleted:

It's important to note that the "rm" command is irreversible, which means that once a file or directory is deleted, it cannot be recovered. Also, if you use the -r option, the command will delete all files and subdirectories in the specified directory recursively, including hidden files and directories, without any prompt or warning. So it is crucial to use it with caution and make sure that the files and directories being deleted are not needed.

  • The "rm" command can also be used in combination with other commands to delete files based on certain criteria. For example, the following command will delete all files with the extension ".txt" in the current directory:
    • rm *.txt
  • To avoid accidental deletion of important files, it's a good practice to use the "rm" command with the "-i" option, which prompts the user for confirmation before deleting each file. This can prevent accidental deletion of important files.
  • The "rm" command can also be used in shell scripts and automation tasks to delete files or directories as needed. For example, a script may use the "rm" command to delete old backups or log files that are no longer needed.
  • The "rm" command also provides the ability to remove symbolic links. By default, rm command removes the file or directory the link points to, not the link itself. However, using the -d option the symbolic link itself is removed.
    • rm -d symbolic_link

The "rm" command is a powerful command that can be used to remove files and directories in Linux and other Unix-like operating systems. Here are some additional features and uses of the "rm" command:

  • The "rm" command can also be used to remove multiple files and directories at once. You can specify multiple files and directories as arguments to the command and it will remove all of them. For example, the following command will remove the files "example1.txt" and "example2.txt" in the current working directory:
    • rm example1.txt example2.txt
  • To avoid accidental deletion of multiple files, it's a good practice to use the "rm" command with the "-i" option, which prompts the user for confirmation before deleting each file. This can prevent accidental deletion of multiple important files.
  • The "rm" command can also be used in combination with other commands and wildcards to remove files based on certain criteria. For example, the following command will remove all files in the current directory that are older than 7 days:
    • find . -mtime +7 -exec rm {} \;
  • The "rm" command can also be used in shell scripts and automation tasks to remove files or directories as needed. For example, a script may use the "rm" command to remove old backups or log files that are no longer needed, or to remove files that match a specific pattern.
  • In Linux and Unix-like systems, the "rm" command can also be used to remove special files such as named pipes, sockets, and device files by using the option -f, but It is important to be aware of the consequences of removing these files, as they are often used by the operating system or other programs, and their removal can cause unexpected behavior or even system failure.

In summary, the "rm" command is a powerful command that can be used to remove files and directories in Linux and other Unix-like operating systems. It can be used with various options like -f, -r, -i, -v to handle different scenarios and delete write-protected files, directories and its contents, prompt for confirmation before deleting each file, display the name of each file as it is deleted. It can also be used to remove multiple files, remove files based


Related Topics

wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name...

4 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

4 minutes read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.