×

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

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.

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.

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.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

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

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.

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.

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.

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.

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.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

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

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.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

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