For example, if you want to delete all the files that are larger than 100 MB in the current directory, you can use the command:shred -uvfz document
Having too many files can affect your computer’s performance and disk space. Therefore, it is advisable to periodically delete the files that you no longer need or want.rsync -a --delete source/ destination/
Note that you need to add a trailing slash (/
) after the source and destination folder names, otherwise rsync will treat them as file names and create a subfolder in the destination.
Table of Contents
- 1 How Do You Delete a Directory in Linux?
- 2 How to Selectively Delete Files in Linux
- 3 Key Takeaways on Deleting Large Directories in Linux
- 4 FAQs on Deleting Large Directories and Files in Linux
- 4.1 What Does the “Argument List Too Long” Error Mean in Linux?
- 4.2 Why Is rm -r Slow in Linux and How Can I See Progress?
- 4.3 What Is the Difference Between rm -r and rm -rf in Linux?
- 4.4 Can I Do a Dry Run Before Deleting Files in Linux?
- 4.5 What Is the Fastest Way to Delete a Large Directory in Linux?
- 4.6 Can I Recover Files After Deleting Them in Linux?
- 4.7 What Happens If I Cancel a Deletion Mid-Process in Linux?
- 4.8 How to Delete Files and Directories in Linux with find
How Do You Delete a Directory in Linux?
This means your user doesn’t have the rights to delete those files. You may need to run the command with sudo at the beginning, but be extremely careful as this grants root privileges and a mistake can damage your system.rm *.txt
The rm command is one of the most common and basic commands for deleting files and directories in Linux. To delete a directory and all its contents, you can use the -r
option, which stands for recursive.
How to Delete a Large Directory in Linux with rm
These options take a number as an argument, which can be preceded by either a plus sign (+
) or a minus sign (-
). A plus sign means more than the given number, while a minus sign means less than the given number.To delete a directory and all its contents, you can use the -delete
option, which will find and delete all the files and directories under a given path. For example:A deleted file can be recovered using special software or hardware tools that can scan the disk for traces of data. Shred prevents this by overwriting the file multiple times with random data, making it impossible to reconstruct the original data.The shred command can only be used to delete files and not directories. However, it is possible to use it in conjunction with other commands to create automated scripts that shred entire directories.There are multiple ways to delete a file or folder in Linux. Let’s take a look at some of these ways.find . -mtime +10 -delete
The rsync method is often the fastest as it is highly optimized for this task. It works by syncing an empty directory over your target directory, which efficiently removes the files with minimal overhead.
How to Use rsync to Delete a Directory in Linux
Deleting a large directory with thousands of files in Linux can be slow or even fail with basic commands. Whether you’re cleaning up old logs, project files, or datasets, knowing the right commands makes the process faster and more reliable.rm -r project
In this guide, we’ll show you how to delete directories with thousands of files in Linux using several methods – from rm
and find
to faster alternatives like rsync
. You’ll learn which approach is best for different scenarios, how to avoid common errors, and how to free up disk space safely.
-a
archive mode, which preserves almost everything (such as symbolic links, file permissions, user & group ownership, and timestamps).--delete
delete mode, which deletes extraneous files from the destination location.
A plus sign (+
) or a minus sign (-
) before the number means larger than or smaller than respectively.This error occurs when a command like rm *
expands to more filenames than the system can handle at once. You must use a more robust method, like the find or rsync commands, which process files individually or in batches.
How to Securely Delete Files in Linux with shred
RunCloud supports various web servers, such as Apache, Nginx, and LiteSpeed. You can also switch between different PHP versions with the click of a button. RunCloud works with any cloud provider, such as AWS, Google Cloud, DigitalOcean, and more. Sign up to RunCloud today and forget all the hassle!Always double-check the path and the options before executing them.The -r
flag means “recursive” to delete a directory and everything inside it. The -f
flag means “force,” which suppresses confirmation prompts and ignores non-existent files, making it much more dangerous if you make a typo.There are also a number of different ways in which it is possible to selectively delete files in Linux, depending on the criteria that you want to use. Here are some examples:If you use a computer professionally, you may have stored many files on your hard drive over time. These files can include documents, photos, videos, music, and more. Some of these files may be important and useful, while others may be outdated and unnecessary.
-u
flag will remove the file or directory after overwriting it with random data.-v
verbose mode, which shows information on shredded files.-f
force mode, which changes permissions to allow writing if necessary.-z
zero mode, which adds a final overwrite with zeros to hide shredding.

However, be careful when using these commands, as they are irreversible and may delete important files if used incorrectly.This option takes a number as an argument, which can be followed by a suffix that indicates the unit of measurement. The suffix can be either bytes (b), kilobytes (k), megabytes (M), gigabytes (G), or blocks (c).For example, if you want to delete all the files that were modified more than 10 days ago in the current directory, you can use the command:The command will stop, but the files that were already deleted are gone forever. You will be left with a partially deleted directory that you can attempt to delete again later.
How to Selectively Delete Files in Linux
Yes, before running a delete command, you can first list the files you intend to target by using find /path/to/directory -type f -print
to be sure you’re in the right place.
How to Delete Files by Pattern in Linux
For example, if you want to delete a directory named project, you can use the command:The number can be either days (-mtime
or -ctime
) or minutes (-mmin
, -cmin
, or -amin
).Here are some possible scenarios:However, be aware that the shred command relies on the assumption that the underlying file system overwrites the same physical block when writing new data. Many newer file systems do not follow this assumption, and may use techniques such as journaling, copy-on-write, or wear leveling, which may prevent the shred command from effectively destroying the data. Therefore, shred may not work as expected on some file systems, such as ext3, ext4, btrfs, xfs, etc. rm test*
How to Delete Files by Date or Time in Linux
find . -amin -30 -delete
How to Delete Files by Size in Linux
The rsync command is generally used to transfer and synchronize files between local and remote devices in an efficient way. It uses a special algorithm that only sends the differences between the source and destination files, which reduces the network usage and speeds up the transfers.To shred a document in Linux, you can use the following simple command:For example, if you use rsync -a --delete source destination
, rsync will create a subfolder named source in the destination and sync the contents of the source folder to it.This option will delete the files and directories without prompting for confirmation or showing any messages. This can be used for various scenarios, such as backup, mirroring, updating, copying, or even deleting files and directories. To delete a folder using rsync, you can use the following simple command:find . -size -1k -delete
Key Takeaways on Deleting Large Directories in Linux
find secret -type f -exec shred -u {} ;
find . -size +100M -delete
For example, you can use the ‘find’ command with the -exec
option to execute the shred command on each file found by ‘find’. Here is an example of how to shred and delete all the files in a directory named ‘secret’:We have also shown you how to selectively delete files based on different criteria, such as pattern, date, time, or size. However, you need to be careful when using these commands, as they are irreversible and may delete important files if used incorrectly. Always double-check the path and the options before executing them. Shred also adds a final overwrite with zeros to hide the fact that the file was shredded. Therefore, shred is more secure and reliable than deleting normally, especially for sensitive or confidential files.
FAQs on Deleting Large Directories and Files in Linux
What Does the “Argument List Too Long” Error Mean in Linux?
Why Is rm -r Slow in Linux and How Can I See Progress?
*
), which matches any number of characters. This way, you can delete files that match a certain pattern, such as files with a specific extension, name, or prefix.What Is the Difference Between rm -r and rm -rf in Linux?
.txt
extension in the current directory, you can use the command:Can I Do a Dry Run Before Deleting Files in Linux?
What Is the Fastest Way to Delete a Large Directory in Linux?
find project -delete
If you are struggling with the Linux command line, then you should take a look at RunCloud. RunCloud allows you to manage your sites effectively without needing to become a Linux system administrator. You can easily create, deploy, and update your websites with just a few clicks.
Can I Recover Files After Deleting Them in Linux?
What Happens If I Cancel a Deletion Mid-Process in Linux?
rm -rf project
How to Delete Files and Directories in Linux with find
Alternatively, you can use the find command, which is a powerful and flexible command for finding and manipulating files and directories in Linux.