How To Remove All Files in a Directory in Linux

Files saved in Linux directories include music, text, image, and other file formats. They tend to occupy some space and are sometimes entirely purposeless. In such a case, you might want to delete all these files to make space for new ones. In Linux, you can remove a single or all the files using a specific command in a terminal. This article will discuss the best methods to remove all files in a Linux directory using the rm and unlink commands.

Tutorial Details

Difficulty Level
Easy
Root Privileges No
Requirements Linux Terminal
Category File Management
Compatibility Ubuntu, Linux  Mint, Debian, openSUSE, Alpine, Fedora Arch,   Alma, RHEL, Rocky, Stream, SUSE, WSL
Command rm command and unlink command

Method 1: Remove All Files in a Directory in Linux Using the rm Command

The rm command is one of the top commands to delete all files in a directory in Linux. People initially used it to remove files and directories with suitable options. It is a manual method to remove files manually rather than deleting a non-empty folder immediately. This command is much safer than most options, and it also prevents the removal of important files accidentally. The basic syntax for the rm command is ‘rm/path/to/dir/’ and ‘rm -r/path/to/dir/.’

The command rm/path/to/dir/ is used to delete a directory and all its content in the Linux terminal. While the rm removes files and directories from a system, the /path/to/dir/ part of the command clearly states the path to the directory you want to delete. When using this command, you should replace this part with the actual path to the directory on your system.

On the other hand, the rm/ -r /path/to/dir/ is used to delete a directory and its content in Linux recursively. The -r option represents recursive and tells explicitly the rm command to delete not just a specific directory but all files and subdirectories contained within it. Below is a breakdown of various components of the command:

  • rm is the command used to remove files and directories
  • -r is the option that tells the rm command to delete directories and their contents recursively
  • /path/to/dir/ is the path to the directory you want to delete. When using the command, replace this with the actual path to the directory on your system.

It is worthy of note that sometimes, using the rm command might require that you confirm each deletion before it is executed. To prevent this, you can use the -f option to force the deletion without confirmation.

Options (-r, -f, -v) and How They Work

You can use several options to change how the rm command works while removing all files in a Linux directory. Below are some of the rm command options and how they work. 

  • -r: remove directories and their contents recursively
  • -f: This is the force option and forces the rm command to ignore nonexistent files and arguments without prompting for confirmation. This is a dangerous option, so it’s essential to be careful when using this option.
  • -v: This is the verbose option and shows clearly what the rm is doing on the screen.

Example to Remove all the Files in a Directory

If you have a directory titled /home/vivek/data/ and want to remove all files in a Linux directory using the rm command, run the command:

rm/home/lost/data

If you want to see what is being done while the rm command deletes all the files in a directory, add the -v option to the rm command. Below is the command to display the progress of the removed file using the -v option:

rm -v /home/lost/data

Example to Delete Hidden and Non-Hidden Files

In Linux, files that start with a dot character are known as dot file. They're hidden files, and you can use the -a option to view these hidden files. Example is:

$ ls -la

view hidden files in linux

To remove all files, including the hidden files in a directory, use the command:

rm -rf /path/to/dir1/{*, .*}

Overall, it’s essential to be careful when deleting files using the rm command as it permanently deletes files, and you cannot quickly recover them. Confirm that you're deleting the correct files before running the command.

Method 2: Remove All Files in a Directory in Linux Using the unlink Command

The unlink command is another option when you have to remove files in Linux. It doesn't remove a full directory or multiple files simultaneously, as the unlink command only removes a single file simultaneously. It also doesn't allow using wildcards like when using the command. 

The syntax for using the unlink command is:

$ unlink filename

For example, if you want to delete a file named doc.txt in a documentary, then enter the command:

$ unlink file

delete with unlink command

The unlink command has several limitations, like the lack of ability to remove multiple files at once. Therefore, it’s recommended that you use the rm command instead, as it provides more possibilities. 

What To Do if You’ve Mistakenly Deleted Files in Linux

If you mistakenly rm delete all files in a Linux directory, then you have some options to recover the deleted files. The highly recommended one is Wondershare Recoverit Linux File Recovery. Wondershare created this top recovery tool with the primary goal of recovering all types of data loss.

Wondershare Recoverit – Your Safe and Reliable Data Recovery Software

5,481,435 people have downloaded it.

Compatibility with all mainstream Linux distros like Linux Mint, Fedora, Manjaro, Ubuntu, Solus, Debian, Opensuse, and many more

Assists in recovering data from 500+ data loss scenarios like disk formatting, power outage, deletion, OS crash, virus attack, and more.

It features a simple interface that allows beginners and experts to recover data from Linux hard drives in just a few clicks.

It recovers lost and deleted documents like music, photos, documents, and other 1000+ file types safely and effectively.

To recover files removed from a directory using the rm or unlink command, you can follow the four simple steps below or watch the video tutorial:

Step 1. Choose Linux Recovery

Download Wondershare Recoverit from the official website and install it following the instructions provided. Then, launch the app and select NAS and Linux > Linux Recovery on the screen.

choose nas and linux

Step 2. Connect to the Linux Device

Enter your Linux information to create a remote connection between Wondershare Recoverit and your Linux device, and then click Connect to proceed.

connect linux computer

Step 3. Scan for All Removed Files in Linux

After establishing the connection, Recoverit will run an automatic scan to look for deleted files on your Linux computer. The scanning process can take a while, depending on how much data is scanned. You can view the progress on the bottom bar. You can also stop the scan process midway if you find the desired file. 

scan for all removed files in linux

Step 4. Preview and Recover

You can preview the scanned files to ensure they're what you want. Finally, select all the files or folders you want to recover and click the Recover button to save them to a safe location.

preview files to recover
Free Download

For Windows Vista/7/8/10/11

Free Download

For macOS X 10.10 or later

Free Download

For Windows Vista/7/8/10/11

Free Download

For macOS X 10.10 or later

Apart from Recoverit, you can explore several other methods to recover deleted files in Linux. However, Recoverit is a safe method to choose.

Conclusion

In this article, you learned how to remove all files in a directory in Linux using the rm and unlink commands. Linux offers these options to find and delete files in a directory. Removing the files in a directory will create free space for new files. If you mistakenly remove a file, you can restore it using Wondershare Recoverit.

You May Also Like