How to Delete Files and Directories in the Linux Terminal

Share
  • April 1, 2019

Fatmawati Achmad Zaenuri/Shutterstock.com

The rm and  rmdir commands delete files and directories on Linux, macOS, and other Unix-like operating systems. They’re similar to the del and  deltree commands in Windows and DOS. These commands are very powerful and have quite a few options.

It is important to note that files and directories deleted using rm and rmdir do not get moved to the Trash. They are immediately removed from your computer. If you accidentally delete files using these commands, the only way you’ll be able to restore them is from a backup.

How to Remove Files with rm

The simplest case is deleting a single file in the current directory. Type the rm command, a space, and then the name of the file you want to delete.

rm file_1.txt

If the file is not in the current working directory, provide a path to the file’s location.

rm ./path/to/the/file/file_1.txt

You can pass more than one filename to rm. Doing so deletes all of the specified files.

rm file_2.txt file_3.txt

Wildcards can be used to select groups of files to be deleted. The * represents multiple characters and the ? represents a single character. This command would delete all of the png image files in the current working directory.

rm *.png

This command would delete all files that have a single character extension. For example, this would delete File.1 and File.2, but not File.12.

rm *.?

If a file is write-protected you will be prompted before the file is deleted. You must respond with y or n and press “Enter.”

rm command with write-protected file

Read the remaining 33 paragraphs

Source : How to Delete Files and Directories in the Linux Terminal