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.
Table of Contents
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.
<strong>$ stat file1.txt</strong> File: file.txt Size: 4076 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d <strong>Inode: 1573697</strong> 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.
<strong>$ ls -li</strong> <strong>1573697</strong> .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 <strong>INODE_NUM</strong> -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 <strong>1573697</strong> -exec rm -i {}
Hit 'y'
to confirm the removal and press ENTER.
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
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
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
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] <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175020967621997.jpg" class="lazy" alt="How to Delete a Large Directory with Files in Linux"></p> <p>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.</p> <h5> <span class="ez-toc-section" id="Conclusion"></span>Conclusion<span class="ez-toc-section-end"></span> </h5> <p>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.</p>
The above is the detailed content of How to Delete a Large Directory with Files in Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

When encountering DNS problems, first check the /etc/resolv.conf file to see if the correct nameserver is configured; secondly, you can manually add public DNS such as 8.8.8.8 for testing; then use nslookup and dig commands to verify whether DNS resolution is normal. If these tools are not installed, you can first install the dnsutils or bind-utils package; then check the systemd-resolved service status and configuration file /etc/systemd/resolved.conf, and set DNS and FallbackDNS as needed and restart the service; finally check the network interface status and firewall rules, confirm that port 53 is not

As a system administrator, you may find yourself (today or in the future) working in an environment where Windows and Linux coexist. It is no secret that some big companies prefer (or have to) run some of their production services in Windows boxes an

Built on Chrome’s V8 engine, Node.JS is an open-source, event-driven JavaScript runtime environment crafted for building scalable applications and backend APIs. NodeJS is known for being lightweight and efficient due to its non-blocking I/O model and

In Linux systems, 1. Use ipa or hostname-I command to view private IP; 2. Use curlifconfig.me or curlipinfo.io/ip to obtain public IP; 3. The desktop version can view private IP through system settings, and the browser can access specific websites to view public IP; 4. Common commands can be set as aliases for quick call. These methods are simple and practical, suitable for IP viewing needs in different scenarios.

Linuxcanrunonmodesthardwarewithspecificminimumrequirements.A1GHzprocessor(x86orx86_64)isneeded,withadual-coreCPUrecommended.RAMshouldbeatleast512MBforcommand-lineuseor2GBfordesktopenvironments.Diskspacerequiresaminimumof5–10GB,though25GBisbetterforad

Written in C, MySQL is an open-source, cross-platform, and one of the most widely used Relational Database Management Systems (RDMS). It’s an integral part of the LAMP stack and is a popular database management system in web hosting, data analytics,

Ubuntu has long stood as a bastion of accessibility, polish, and power in the Linux ecosystem. With the arrival of Ubuntu 25.04, codenamed “Plucky Puffin”, Canonical has once again demonstrated its commitment to delivering a

MongoDB is a high-performance, highly scalable document-oriented NoSQL database built to manage heavy traffic and vast amounts of data. Unlike traditional SQL databases that store data in rows and columns within tables, MongoDB structures data in a J
