Is your Linux system cluttered with excessive files? This guide demonstrates how to efficiently locate and remove files exceeding or falling short of a specified size, reclaiming valuable disk space. We'll leverage the powerful find
command for this task.
Caution: File deletion is permanent. Always back up your data before proceeding.
Table of Contents
- Listing Files by Size
- Deleting Files Based on Size
- Removing Files Smaller Than X Size
- Removing Files Larger Than X Size
- Alternative Deletion Commands
- Interactive Deletion Confirmation
- Best Practices for File Deletion
- Conclusion
Listing Files by Size
Before deleting, it's crucial to preview the files targeted for removal. The -print
option with the find
command provides this preview without actually deleting anything.
To list .doc
files under 5MB in the current directory:
find . -type f -name "*.doc" -size -5M -print
Command Breakdown:
-
find .
: Searches the current directory and its subdirectories. -
-type f
: Limits the search to regular files. -
-name "*.doc"
: Filters for files ending in.doc
. -
-size -5M
: Selects files smaller than 5 Megabytes. -
-print
: Displays matching files.
Replace .
with a specific path to search other directories. To list files larger than 5MB, use 5M
instead of -5M
.
The -size
operator uses -
for "less than" and
for "greater than." Units include G (Gigabytes), M (Megabytes), K (Kilobytes), and c (bytes).
Examples:
- Files smaller than 10KB:
find . -type f -size -10k
- Files larger than 2GB:
find . -type f -size 2G
- Files exactly 500 bytes:
find . -type f -size 500c
Deleting Files Based on Size
After verifying the file list, replace -print
with -delete
to perform the actual deletion.
Critical Note: -delete
is irreversible. Always double-check your command and directory path. Consider testing on a sample directory first.
Removing Files Smaller Than X Size
To delete .doc
files smaller than 5MB:
find . -type f -name "*.doc" -size -5M -delete
Removing Files Larger Than X Size
To delete .doc
files larger than 5MB:
find . -type f -name "*.doc" -size 5M -delete
Alternative Deletion Commands
The -exec rm {} \;
construct offers an alternative:
find . -type f -name "*.doc" -size -5M -exec rm {} \;
This executes rm
(remove) on each found file. Use 5M
for larger files.
Interactive Deletion Confirmation
For enhanced safety, add -i
to rm
:
find . -type f -name "*.doc" -size -5M -exec rm -i {} \;
This prompts for confirmation before deleting each file.
Best Practices
-
Test First: Always use
-print
before-delete
. -
Specify Sizes: Use
-size
precisely. - Understand Your Files: Avoid deleting system files or critical data.
- Back Up Your Data: Essential for safety.
-
Cautious Deletion:
-delete
is permanent. - Target Unnecessary Files: Focus on old or temporary files.
Conclusion
Linux offers robust tools for managing files by size. The find
command, combined with -delete
or rm
, provides powerful yet potentially destructive capabilities. Always prioritize data backup and careful command execution. Interactive confirmation (rm -i
) is highly recommended for added safety.
The above is the detailed content of How To Delete Files Bigger Or Smaller Than X Size In Linux. For more information, please follow other related articles on the PHP Chinese website!

Linuxoffersmoregranularcontroloverloggingandauditing,whileWindowsprovidesamorecentralizedsystem.1)Linuxusestoolslikesyslog,rsyslog,andjournaldforcustomizablelogging.2)WindowsusestheEventViewerforcentralizedlogmanagement.3)Linuxisidealforenvironmentsn

Artificial Intelligence (AI) is a term that’s been buzzing around for a while now, from self-driving cars to voice assistants like Siri and Alexa, AI is becoming a part of our everyday lives. But what exactly is AI, and why should Linux users care ab


For someone new to Linux, using it can still feel challenging, even with user-friendly distributions like Ubuntu and Mint. While these distributions simplify many tasks, some manual configuration is often required, but fully harnessing the power of L

In the previous article, we introduced the basics of AI and how it fits into the world of Linux. Now, it’s time to dive deeper and set up your Linux system to start building your first AI model. Whether you’re a complete beginner or have some exper

If you’re looking to manage your server with ease, Kloxo is a great option, as it is free and open-source web hosting control panel that allows you to manage your server and websites with a simple, user-friendly interface. In this guide, we’ll walk

If you’ve ever found yourself in a situation where you’re trying to move a bunch of files and folders, only to be stumped by spaces in the folder names, you’re not alone. Spaces in filenames or folder names can quickly become a frustrat


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
