How to Use the rm Command in Linux

29/12/2020
Chưa phân loại
The rm command is used to remove files and directories in Linux. This is one of the commands that you must know as a Linux System Administrator for managing files and directories.  In this article, I am going to show you how to use the rm command to remove files and directories in Linux.

Removing a Single File:

You can remove a single file with rm.

Let’s say, you want to remove an image file 529395.jpg in the Pictures/ directory in your users HOME directory.

To remove the file, you may run the following command:

$ rm Pictures/529395.jpg

The file should be removed.

Print What rm is Doing:

If you want rm to print what it’s doing on the console, then you can use the -v option.

For example, to remove a file test.txt from your current working directory, you may run the following command:

$ rm -v test.txt

As you can see, the file is removed. rm also printed removed ‘test.txt’ on the console as it successfully removed the file test.txt.

Prompt Before Removing Files:

As rm is a very destructive command, you may accidentally remove important files. The good news is that, rm has an -i flag which prompts (to confirm) you before removing every file.

For example, let’s say, you want to remove the file hello.txt but you want rm to prompt you to confirm the file removal operation. Then, you would run rm as follows:

$ rm -iv hello.txt

As you can see, you’re prompted to confirm the remove operation of the file hello.txt. To confirm, press y and then press <Enter>.

The file should be removed.

Prompt Before Removing More Than 3 Files:

You can tell rm to prompt for confirmation when you’re removing more than 3 files only with the -I option.

Let’s say, you have 4 files in your current working directory, test1, test2, test3, and test4. Now, you want to make sure you don’t remove more than 3 files without any notice.

To do that, run the following command:

$ rm -Iv test*

Now, rm will prompt you to confirm whether you really want to delete these files. If you are sure that you don’t want these files anymore, then press y and then press <Enter> to continue.

As you can see, all these files are removed.

Removing Empty Directories with rm:

You can also remove empty directories on your Linux system with rm. To do that, you can use the -d option of rm.

Let’s say, you have an empty directory files/ in your current working directory. Now, you want to remove it. You can do so with the following command:

$ rm -dv files/

As you can see, the empty directory files/ is removed.

Removing Files and Directories Recursively with rm:

Let’s say, you have an entire directory of files and other subdirectories with files that you don’t need anymore. Now, you want to remove all of them to save up disk space.

You can remove every files and subdirectories of the directory (let’s say tmp/ in your current working directory) with the following command:

$ rm -rfv tmp/

As you can see, all the files and subdirectories inside the files/ directory including the files/ directory is removed.

If you want rm to ask you to confirm before deleting every directories and files in the files/ directory, then you can run the above command as follows:

$ rm -rvi files/

As you can see, rm is asking you to confirm before it does anything. If you’re okay with this, just press y and then press <Enter> to continue.

rm will keep asking you to confirm till it’s done with it’s work.

If you want to stop the rm command in the midway, just press <Ctrl> + c.

Conclusion:

The rm command is very destructive. You should always be careful with it. Always be extra careful and double check everything before you run the rm command with super user privileges (sudo).

For example, you can delete important system files if you’re not careful with the rm command. Which may cause your computer to fail to boot or certain services to fail to start.

A good way to double check everything is to use the ls command.

For example, let’s say, you want to remove every files that start with tmp from your current working directory. You can of course remove all these files with the following command:

$ rm -v tmp*

But for safety purpose, instead of using rm command first, check whether using tmp* lists all the files that you want to remove with the ls command as follows.

$ ls tmp*

As you can see, the command lists all the files that match tmp*. If you’re sure that you don’t need them, then you can use the rm command as shown above to remove them all.

So, that’s all about the rm command. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

10 Best Blue Color Wallpapers for Ubuntu Desktop

BLUE the color of the ocean and the sky and water, three of the most fascinating things in human race. Ever wondered why...
29/12/2020

Quản lý cơ sở dữ liệu MariaDB trên CentOS 7

MariaDB là một hệ quản trị cơ sở dữ liệu được sử dụng phổ biến hiện nay. Vậy nó làm...
30/12/2020

Ubuntu Live patch

In mission critical production servers, nothing can be worse than having to reboot them. For big companies, time is money....
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024