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

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

Clean Up Your Linux System: Find and Delete Files Based on Size

By sk
333 views

Do you have a Linux system overflowing with files? Perhaps old documents or remnants of past projects are cluttering your storage. This guide will show you how to efficiently find and delete files bigger or smaller than X size in Linux.

We'll explore using the find command to remove unwanted files based on their size, helping you reclaim valuable disk space. Let us get started!

Since we're dealing with the deletion of files, it is highly recommended to follow a safer approach. If possible, make sure you have a backup of the data. It helps you to recover files from accidental deletion.

List Files Based on Size

For a safer approach, you can use the -print option with find command to list the files instead of deleting them first. This will list all the files that would be deleted without actually deleting them.

For instance, to list .doc files smaller than 5 MB in the current directory, you would use:

$ find . -type f -name "*.doc" -size -5M -print

Explanation of the command:

  • find: This is the command used to locate files based on various criteria.
  • .: This specifies the starting directory for the search. In this case, it starts in the current directory and searches recursively through subdirectories (represented by the dot).
  • -type f: This option tells find to only search for regular files (not directories).
  • -name "*.doc": This option filters the search results to only include files with the name extension .doc.
  • -size -5M: This option uses the -size operator to filter files based on their size. The -5M specifies a maximum size of 5 Megabytes. Files larger than 5M will be excluded.
  • -print: This option instructs find to list all files that match the specified criteria.

If you want to list files in other directories, simply replace the . with the /path/to/your/directory in the above command.

Similarly, to display all the .doc files that are bigger than X size (E.g. 5 MB), the command would be:

$ find . -type f -name "*.doc" -size +5M -print

Please mind the plus (+) sign with the -size operator in the above command.

In the find command with the -size operator, the - and + signs define whether the file size is smaller than or bigger than a specified value.

  • -size -<number>: This searches for files with a size less than the specified <number>.
  • -size +<number>: This searches for files with a size greater than the specified <number>.

In our previous example, -size -5M finds files smaller than 5 Megabytes. In the later example, -size +5M finds files larger than 5 Megabytes.

Remember, the <number> can be specified in various units like G (Gigabytes), M (Megabytes), K (Kilobytes), or even c (bytes) depending on your needs.

Here are some examples of how you can specify different units with the -size operator in the find command:

To Find files smaller than 10 Kilobytes, run:

find . -type f -size -10k

Find files larger than 2 Gigabytes:

find . -type f -size +2G

Find files exactly 500 bytes in size:

find . -type f -size 500c

As you can see, you can use various units (K, M, G, c) depending on the size range you're interested in.

Once you've verified the list, you can then run the same command with -delete to perform the actual deletion as shown in the following section.

Delete Files Bigger or Smaller than X Size

As stated already, you can use the + or - signs with the -size operator to define whether the file size is bigger than or smaller than a specified value.

First, we will see how to find and remove files that are smaller than a certain size.

Important:

  • The following commands will permanently delete the files. Make sure you have a backup if needed.
  • Double-check the directory path before running the command. You can replace . with the specific directory path if needed.
  • Test the command on a small sample of files first (maybe in a separate folder) to ensure it works as expected before running it on your entire folder structure.
  • Additionally, you could consider using the --dry-run option with the find command to simulate the deletion without actually removing the files, which can help you verify the results before running the final command.

Remove Files Smaller than Certain Size

To find and delete all .doc files that are smaller than 5MB in your current directory, while keeping the directory structure intact and excluding bigger files, run the following command:

$ find . -type f -name "*.doc" -size -5M -delete

Here's a breakdown of what this command does:

  1. find . -type f: This part of the command searches for all files (not directories) in the current directory and its subdirectories.
  2. -name "*.doc": This part of the command filters the search to only include files with the .doc extension.
  3. -size -5M: This part of the command further filters the search to only include files smaller than 5MB.
  4. -delete: This part of the command actually deletes the files that match the previous criteria, without prompting the user for confirmation.

Remove Files Larger than Certain Size

Similarly, you can find and delete .doc files that are larger than the given size (E.g. 5 MB) using the following command:

$ find . -type f -name "*.doc" -size +5M -delete

In the above example, we find and remove all .doc files that are bigger than 5 MB in size.

Alternative Commands to Delete Files Based on Size

The versatility of the Linux command line is truly one of the reasons why many users, from beginners to advanced, find the Linux operating system to be a powerful for their needs.

We can use the following variants of find command to find and remove files that are smaller or bigger than the given size.

Have a look at the following example:

To delete all the small .doc files without deleting the larger files and while retaining the directory structure, you can use the following command-line approach on Linux:

$ find . -type f -name "*.doc" -size -5M -exec rm {} \;

This command will delete all the small .doc files while keeping the larger .doc, .docx, .xlsx, and PDF files, and preserving the directory structure.

Here's how the command works:

  1. find . -type f -name "*.doc" -size -5M: This part of the command searches for all files (not directories) with the .doc extension and a size less than 5MB (-size -5M) in the current directory and its subdirectories.
  2. -exec rm {} \;: This part of the command executes the rm (remove) command for each file found, deleting the small .doc files.

And, here's a breakdown of the different options used in the command:

  • find .: Starts the search in the current directory and its subdirectories.
  • -type f: Specifies that we're looking for files, not directories.
  • -name "*.doc": Searches for files with the .doc extension.
  • -size -5M: Searches for files smaller than 5MB.
  • -exec rm {} \;: Executes the rm command to delete each file found.

You can replace -5M with +5M in the above command to find and delete files that are larger than 5 MB.

The main difference between the above command and the one I provided in the previous section is that this version used the -exec rm {} \; part to execute the rm command for each file found, whereas the earlier version uses the -delete option, which deletes the files directly without the need for a separate rm command.

Both commands achieve the same result - they delete all the small .doc files while preserving the larger files and the directory structure. The choice between the two commands is mainly a matter of personal preference and the specific use case.

Confirm Deletion for Each File

You can also add -i option with the rm command for extra security:

$ find . -type f -name "*.doc" -size -5M -exec rm -i {} \;

The -i option in the rm command prompts the user for confirmation before deleting each file. This means that instead of automatically deleting the files, the command will ask you to confirm the deletion for each file.

The purpose of this command is to provide an additional layer of safety and control when deleting files. By using the -i option, you can review each file before it's deleted, reducing the risk of accidentally removing something you didn't intend to.

This approach is useful when you want to be extra cautious about the files you're deleting, or if you're not entirely certain about the contents of the .doc files. The interactive prompts give you the opportunity to double-check and confirm the deletion of each file.

Keep in mind that the interactive nature of this command will make the process slightly more time-consuming, as you'll need to respond to the prompt for each file.

Again, I strongly recommend you to make sure to test the command on a small subset of your files first to ensure it's working as expected before running it on the entire folder structure.

Best Practices for Deleting Files in Linux

Here are some best practices for deleting files by size in Linux:

  1. Start Small, Test Often: Don't jump right into deleting everything! Use the find command with the -print option first. This shows you a list of files that would be deleted, allowing you to double-check before taking action.
  2. Target Specific Sizes: Focus on the file sizes you want to remove. The find command with the -size operator lets you target files larger than ( + ) or smaller than ( - ) a specific value (e.g., delete files bigger than 5 Megabytes: -size +5M).
  3. Know Your Files: Be sure you understand what kind of files you're deleting. Don't remove system files or important data by mistake. If unsure, skip a file or research it further.
  4. Consider Backups: If there's a chance you might need the files later, create a backup before deleting them.
  5. Delete Cautiously: The -delete option in the find command is permanent. Only use it after you've thoroughly checked the files listed with -print.
  6. Focus on Unneeded Files: This method is ideal for cleaning up old documents, temporary files, or outdated project leftovers. It helps reclaim valuable disk space for what you truly need.

By following these practices, you can effectively manage your storage space in Linux by deleting unwanted files based on size, while keeping your system organized and your data safe.

Conclusion

The Linux command line provides flexible tools to delete files based on size. The find command allows you to search for files matching specific criteria, such as file extension and size.

You can then use either -delete option or rm command to delete these files, either automatically or with interactive confirmation.

The interactive rm -i option adds an extra layer of safety, prompting you before each deletion. This is useful when you want to closely review the files before removing them.

Whichever approach you choose, always maintain a backup of your data. This ensures you can recover any accidentally deleted files.

Related Read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More