How to use the rm to delete Files and Folders in Ubuntu Linux with examples

This tutorial provides guidance on the use of the ‘rm’ command in Ubuntu, mainly for beginners or students. The ‘rm’ command, commonly used in Linux, deletes files and directories. The post explains its syntax with options like ‘-f’ (force delete), ‘-i’ (prompt before removal) and ‘-d’ (remove empty directories). Examples of how to correctly use…

This brief tutorial shows students and new users how to use the rm command on Ubuntu to delete or remove files and folders, including directories.

The rm command is one of the most common daily commands that Linux users use. It is a utility that removes or deletes files and directories on Linux systems, including Ubuntu.

Learning how to use the rm command is important because it allows you to delete unwanted files and folders, freeing up disk space on your system. It is also a useful tool for managing your files and directories.

Whether a student or a professional, learning to use the rm command can help you be more productive and efficient in your work.

About rm command:

The rm command is a utility that removes or deletes files and directories on Linux systems, including Ubuntu. It’s one of the most common commands that Linux users use daily.

Syntax:

The syntax is the rule and format of how the rm command can be used. These syntax options can be reordered, but a straight format must be followed.,.

Below is an example syntax of how to use the rm command.

rm [OPTION]. [FILE].

Options:

The command line options are switches or flags that determine how the commands are executed or controlled. They modify the behavior of the commands. They are separated by spaces and followed after the commands.

Below are some options for the rm  command:

  FILE.Replace FILE.. with the file(s) you want to remove or delete
-f, –forceUse the -f or –forced to ignore nonexistent files and arguments and never prompt before deletion
-iUse the -i option to prompt before every removal or a file
-r, -R, –recursiveUse the -r or -R or –recursive to remove directories and their contents recursively
-d, –dirUse the -d or –dir to remove empty directories
–no-preserve-root  Use the –no-preserve-root to not treat ‘/’ especially. By default, the / directory is not removed
–helpDisplay a help message and exit.

Examples:

Below are some examples of how to run and use the rm on Ubuntu Linux.

Simply run the rm to invoke it.

If you want to use the rm command to remove a file named Private.txt, you simply run the commands below:

Example:

rm Private.txt

When you run the above command and have the right to delete from that directory, the command will delete the file defined above. (Private.txt)

If you don’t have permission to delete content from the parent directory (the directory in the file lives), you won’t be able to. You will get “Operating not permitted” error.

This simply means you don’t have rights there to carry out the operation you executed.

Each time you run the rm command to delete an item, you’ll get prompted to confirm that you want to delete the specified file. If you prefer not to be bothered or prompted, use the -for –force option.

Example:

rm -f Private.txt

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

To use the rm command to delete one or more empty directories, you use the -d option.

Example:

rm -d PrivateDirectory

rm -d behaves exactly as the rmdir command was written about previously.

However, the rm -d command only deletes empty directories. If the directories contain files and other directories, the command will fail.

To recursively remove all non-empty directories and files within them, you run the rm command with the -r option against the directories.

Example:

rm -r PrivateDirectories

To force-ably remove non-empty directories and all content in them without being prompted, use the -r and -f options:

Example:

rm -rf PrivateDirectories

You may have to use the rm command with sudo if you don’t have rights.

Example:

sudo rm -rf PrivateDirectories

That’s it!

Congratulations! You have learned how to delete or remove content using the rm command on Ubuntu.

Richard Avatar

Comments

  1. It will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data on Ubuntu Linux.

  2. very helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *


Exit mobile version