How to Delete Large Directory with Thousands of Files in Linux

Introduction

Deleting large directories with thousands of files in Linux can be a daunting task. It can take a long time to delete the files one by one, and it can be difficult to keep track of which files have been deleted and which ones still need to be deleted. Fortunately, there are several methods that can be used to quickly and easily delete large directories with thousands of files in Linux. In this article, we will discuss the different methods that can be used to delete large directories with thousands of files in Linux. We will also discuss the advantages and disadvantages of each method, so that you can choose the best option for your needs.

How to Delete Large Directory with Thousands of Files in Linux

1. Use the rm command with the -r flag to recursively delete the directory and its contents.

Example:

rm -r /path/to/directory

2. Use the find command to delete files in batches.

Example:

find /path/to/directory -type f -delete

3. Use the rsync command to delete files in batches.

Example:

rsync -a –delete /path/to/directory/ /path/to/empty/directory/

4. Use the xargs command to delete files in batches.

Example:

find /path/to/directory -type f -print0 | xargs -0 rm -f

File management is one of the common tasks that a user undertakes on a Linux system, which includes creating, copying, moving, modifying, and deleting files and directories.

This article provides a few command-line tips on how you can delete a large directory that contains thousands of files in a Linux system.

Delete Files in Linux

The most common way of deleting files on a Linux system is using the rm command, which takes the following syntax format:

$ rm [ options ] sample_file.txt

For example, to delete a text file called file1.txt, run the command:

$ rm file1.txt

To forcefully remove a file without being asked for permission, pass the -f flag as follows.

$ rm -f file1.txt

Delete Directory in Linux

To remove or delete a directory called sample_directory, run the following command:

$ rm -rf sample_directory

The -r option recursively deletes the directory alongside all the subdirectories and files contained therein.

To delete or remove an empty directory use the rmdir command, which comes in handy when you want to remove an empty directory called test_directory as shown:

$ rmdir test_directory

Delete a Large Directory with Tons of Files

When the rm command is executed, the filesystem only removes the link to the file, which makes the file unavailable to the user, but in the real sense, the file’s data itself remains intact on the disk.

Therefore, when the rm command is issued, only the reference to the files is removed, which frees up the storage blocks in the filesystem.

As such, there exist several avenues to delete files in Linux.

Delete Files With Inode Number in Linux

For example, you can delete a file using its inode number. You can find out a file’s inode number using the stat command as shown.

$ stat file1.txt

File: file.txt
  Size: 4076      	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 1573697     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ tecmint)   Gid: ( 1000/ tecmint)
Access: 2023-05-08 12:10:55.656070248 +0530
Modify: 2023-05-08 12:10:55.656070248 +0530
Change: 2023-05-08 12:10:55.656070248 +0530

In addition, you can pass the -i flag in the ls command when listing files inside a directory.

$ ls -li

1573697 .rw-rw-r-- tecmint tecmint 4.0 KB Mon May  8 12:10:55 2023  file1.txt

To remove the file using its inode, use the find command as shown in the syntax below.

$ find /path/to/file -inum INODE_NUM -exec rm -i {} +

In our example, to remove file file1.txt that sits in the current directory, the command will be:

$ find /path/to/file -inum 1573697 -exec rm -i {} +

Hit 'y' to confirm the removal and press ENTER.

Delete Files By Inode Number
Delete Files By Inode Number

Let us now see how to delete large directories with thousands of files.

Create a Directory with Thousands of Files

The good old rm command is the fastest way of deleting a large directory with thousands of files. To demonstrate this, we will, first, create a sample directory and navigate into it.

$ mkdir test_dir 
$ cd test_dir

Next, we will create an insanely huge number of files, in this case, 500,000 text files using the following bash for a loop.

$ time for item in {1..500000}; do touch file_name$item.txt; done

NOTE: The above command is resource intensive and, hence, consumes substantial CPU and RAM. It also takes quite some time depending on your system specifications. In my case, I’m running a VM with 4GB RAM and 3 CPUs.

Create Thousands of Files in Linux
Create Thousands of Files in Linux

Fastest Way to Delete Directory in Linux

The fastest way to delete a large directory is using the good old rm directory as shown below. Here, the time option displays the time taken to successfully execute the command.

$ time rm -rf /test_dir
Fastest Way to Delete Large Directory
Fastest Way to Delete Large Directory

From the output, you can see that it has taken roughly 6 seconds to delete the entire directory.

Delete Large Directory with Find Command

Another way to delete large directories is using the find command as shown in the following syntax.

$ time find /path/to/directory -delete

Although not as fast as the rm command it still gets the job done.

$ time find test_dir -delete
Find Command - Delete Large Directory
Find Command – Delete Large Directory

Delete Large Directory with Perl Command

Another approach is to use the Perl scripting language inside the directory to remove tons of files.

$ cd test_dir
$ time perl -e 'for(<*>){((stat)[9]<(unlink))}'
Perl Command - Delete Large Directory
Perl Command – Delete Large Directory

From the output, you can this that it took much longer to delete all the files in the directory than the previous commands that we looked at earlier.

Conclusion

There you have it. In this guide, we have looked at how you can delete large directories that contain thousands of files on a Linux system.

How to Delete Large Directory with Thousands of Files in Linux

Deleting a large directory with thousands of files in Linux can be a daunting task. It can take a long time to delete all the files, and it can be difficult to keep track of which files have been deleted and which ones still need to be deleted. Fortunately, there are a few methods that can make this process much easier.

Using the rm Command

The simplest way to delete a large directory with thousands of files is to use the rm command. This command can be used to delete a single file or an entire directory. To delete an entire directory, you need to use the -r flag. This flag stands for recursive, which means that it will delete all the files and subdirectories in the specified directory.

For example, if you wanted to delete the directory /home/user/my_directory, you would use the following command:

rm -r /home/user/my_directory

This command will delete the directory and all of its contents. Be sure to double-check that you are deleting the correct directory before running this command.

Using the find Command

Another way to delete a large directory with thousands of files is to use the find command. This command can be used to search for files and directories in a given directory. You can then use the -delete flag to delete all the files and directories that were found.

For example, if you wanted to delete all the files and directories in the /home/user/my_directory directory, you would use the following command:

find /home/user/my_directory -delete

This command will delete all the files and directories in the specified directory. Be sure to double-check that you are deleting the correct directory before running this command.

Using the rm -rf Command

The last method for deleting a large directory with thousands of files is to use the rm -rf command. This command is a combination of the rm and -r flags, and it stands for recursive force. This command will delete all the files and directories in the specified directory, without prompting for confirmation.

For example, if you wanted to delete the directory /home/user/my_directory, you would use the following command:

rm -rf /home/user/my_directory

This command will delete the directory and all of its contents without prompting for confirmation. Be sure to double-check that you are deleting the correct directory before running this command.

Conclusion

Deleting a large directory with thousands of files in Linux can be a daunting task. Fortunately, there are a few methods that can make this process much easier. You can use the rm command, the find command, or the rm -rf command to delete a large directory with thousands of files in Linux.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?