[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 16, 2023

How to use rm command.

How To Use Rm Command.

In this guide, we will explain how to use the rm command through examples and explanations of the most common rm options.

rm is a command-line utility for removing files and directories. It is one of the essential commands that every Linux user should be familiar with.

 

How to Use the rm Command

The general syntax for the rm (remove) command is as follows:

rm [OPTIONS]... FILE...

By default, when executed without any option, rm doesn’t remove directories and doesn’t prompt the user for whether to proceed with the removal of the given files.

To delete a single file, use the rm command followed by the file name as an argument:

rm filename

If you don’t have write permissions on the parent directory, you will get “Operation not permitted” error.

If the file is not write protected, it will be removed without notice. On success, the command doesn’t produce any output and returns zero.

When removing write protected files, the command will prompt you for confirmation, as shown below:

rm: remove write-protected regular empty file 'filename'?

Type y and hit Enter to remove the file.

The -f option tells rm never to prompt the user and to ignore nonexistent files and arguments.

rm -f filename

If you want to get information about what is being removed, use the -v (verbose) option:

rm -v filename
removed 'filename'

 

Removing Multiple Files

Unlike the unlink command, rm allows you to delete multiple files at once. To do that, pass the filenames as arguments separated by space:

rm filename1 filename2 filename3

You can use regular expressions to match multiple files. For example, to remove all .png files in the current directory, you would type:

rm *.png

When using regular expressions, before running the rm command. is always a good idea to list the files with the ls command so that you can see which files will be deleted.

 

Removing Directories (Folders)

To remove one or more empty directories use the -d option:

rm -d dirname

rm -d is functionally identical to the rmdir command.

To remove non-empty directories and all the files within them recursively, use the -r (recursive) option:

rm -r dirname

 

Prompt Before Removal

The -i option tells rm to prompt the user for each given file before removing it:

rm -i filename1 filename2

To confirm type y and press Enter:

rm: remove regular empty file 'filename1'? 
rm: remove regular empty file 'filename2'? 

When removing more than three files or recursively removing a directory, to get a single prompt for the entire operation, use the -I option:

rm -i filename1 filename2 filename3 filename4

You will be asked to confirm the removal of all given files and directories:

rm: remove 4 arguments? 

 

rm -rf

If the given directory or a file within the directory is write-protected, the rm command will prompt you to confirm the operation. To remove a directory without being prompted, use the -f option:

rm -rf dirname

The rm -rf command is very dangerous and should be used with extreme caution!

 

Conclusion

We have shown you how to use the Linux rm command to remove files and directories from your Linux system.

Keep reading
How troubleshoot linux problems(server crash, out of memory). Diagnosing and Fixing Common Issues.
June 18, 2023

How troubleshoot linux problems(server crash, out of memory). Diagnosing and Fixing Common Issues.

How Troubleshoot Linux Problems (Server Crash, Out Of Memory). Diagnosing And Fixing Common Issues. When your Linux server seems to be offline or otherwise inaccessible, you should always be able to log in with the web console or through a VNC connection. Steps you need to do for troubleshooting:…

Read article
How to use df command.
June 13, 2023

How to use df command.

How To Use Df Command. In this article we will show you how to check disk space with df command. Using the df Command The general syntax for the df command is as follows: df [OPTIONS]… FILESYSTEM… When used without any argument, the df command will display information about all mounted file…

Read article
How to use watch command.
June 13, 2023

How to use watch command.

How To Use Watch Command. In this tutorial, we will introduce you to the watch command. watch is used to run any arbitrary command at regular intervals and displays the output of the command on the terminal window. The watch utility is a part of the procps (or procps-ng) package which is…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00