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

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.

We previously showed you how to use the rmdir command on Ubuntu. rmdir command is used to delete folders or directories and not files.

The rm command does both.

If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS. It’s a great Linux operating system for beginners.

Ubuntu is an open-source Linux operating system that runs on desktops, laptops, servers, and other devices.

Both Ubuntu and Windows systems allow you to be productive, easy to use, and reliable and enable you to install and run thousands of programs from gaming to productivity suite software for individuals and businesses.

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 determined 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 you 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 wrote about previously.

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

To remove all non-empty directories and all files within them recursively, 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

If you don’t have rights, you may have to use the rm command with sudo.

Example:

sudo rm -rf PrivateDirectories

That’s it!

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