×

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

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.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

unzip Command in Linux/Unix with Examples

The 'unzip' command in Linux is used to extract files from a ZIP archive file. The command allows you to decompress and extract the files and directories stored in a...

3 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.

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.

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

12 minutes read.

scp Command in Linux/Unix with Examples

The scp (secure copy) command in Linux is a command-line utility that is used to securely transfer files between two hosts over a network. It is similar to the cp...

4 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

10 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

man Command in Linux/Unix with Examples

The man command in Linux is used to display the manual pages for a given command or system call. These manual pages provide detailed information on the command's usage, options,...

3 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.

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.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

3 minutes read.

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...

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.