search
HomeSystem TutorialLINUXHow To Delete Files Bigger Or Smaller Than X Size In Linux

How To Delete Files Bigger Or Smaller Than X Size In Linux

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

  1. Test First: Always use -print before -delete.
  2. Specify Sizes: Use -size precisely.
  3. Understand Your Files: Avoid deleting system files or critical data.
  4. Back Up Your Data: Essential for safety.
  5. Cautious Deletion: -delete is permanent.
  6. 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does logging and auditing differ between Linux and Windows?How does logging and auditing differ between Linux and Windows?May 08, 2025 am 12:03 AM

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

What is AI? A Beginner's Guide for Linux UsersWhat is AI? A Beginner's Guide for Linux UsersMay 07, 2025 am 11:23 AM

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

50 Essential Linux Commands for Beginners and SysAdmins50 Essential Linux Commands for Beginners and SysAdminsMay 07, 2025 am 11:12 AM

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

How to Set Up Your Linux System for AI DevelopmentHow to Set Up Your Linux System for AI DevelopmentMay 07, 2025 am 10:55 AM

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

How to Install Kloxo Web Hosting Control Panel in LinuxHow to Install Kloxo Web Hosting Control Panel in LinuxMay 07, 2025 am 10:52 AM

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

How to Move Files and Folders with Spaces in LinuxHow to Move Files and Folders with Spaces in LinuxMay 07, 2025 am 10:17 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

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.