Thứ Ba, Tháng Hai 11, 2025
spot_img
HomeTechnologyHow to delete files and folders using the Linux command line

How to delete files and folders using the Linux command line

In this guide, Quantrimang.com will help readers learn how to delete files and folders using the command line on Linux. This guide is compatible with all Linux distributions, so it works the same way on Ubuntu, Debian, CentOS, AlmaLinux, Rocky Linux, etc…

Delete a file on Linux

In Linux, the rm command is used to delete files and directories. Navigate to the specific folder containing the file you want to delete. Specify the location otherwise the command will start searching in the current working directory. For example, the article author has a file in the directory /tmp/ want to delete. To delete the desired file, open Terminal and enter the following command:

# rm file.txt
Delete a single file
Delete a single file

Be careful while files and folders from Linux because once deleted, they cannot be recovered. So it will ask you to confirm before deleting the file:

# rm –i file.txt
Confirm file deletion
Confirm file deletion

If you don't want the deletion confirmation message, use the following command:

# rm –f file.txt
Force deletion of files on Linux
Force deletion of files on Linux

A confirmation message will not appear.

Delete multiple files on Linux

To delete multiple files on Linux, the same rm command can be used.

# rm file.txt file1.txt file2.txt
Delete two or more files on Linux
Delete two or more files on Linux

Delete folders on Linux

To delete a folder on Linux, the same command is used. But you need more options -r and -f to delete a folder.

# rm –rf /data
Delete folder
Delete folder

But be careful, this will delete the entire folder, including all files and folders inside. You can use the above command without -fas it will not prompt for confirmation. Options -r used to delete directories.

If you just want to delete an empty directory, use this command instead:

rmdir /data

The command will show an error in case the directory is not empty.

Xem thêm  Why shouldn't you pay for Pro versions of free productivity apps?

Summary

In all Linux distributions, the rm command is used to delete files and folders.

  • If -i is used with rm, you will be prompted to confirm before deleting.
  • If -r used with rm, the directory will be deleted without confirmation.

See more:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments