Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the files exceeding certain size and deleting them

  1. I am looking for a linux command to get all the files exceeding a certain size from the current directory and its sub-directories.

  2. Whats the easiest way to delete all these files?

like image 751
Ran Avatar asked Feb 20 '11 12:02

Ran


People also ask

How do I delete a folder with too many files?

Navigate to the folder that you want to delete (with all its files and subfolders). Use cd *path*, for example, cd C:\Trash\Files\ to do so. Use cd .. to navigate to the parent folder and run the command RMDIR /Q/S *foldername* to delete the folder and all of its subfolders.

How do I delete large files in Linux?

1. Empty or delete the contents of a large file using the truncate command in the Linux/Unix system. The truncate command is used to shrink or extend the size of a file to a specified size in the Linux system. It is also used to empty large file contents by using the -s option followed by 0 (zero) specified size.


2 Answers

Similar to the exec rm answer, but doesn't need a process for each found file:

find . -size +100k -delete 
like image 187
Erik Avatar answered Sep 28 '22 03:09

Erik


One-liner:

find . -size +100k -exec rm {} \; 

The first part (find . -size +100k) looks for all the files starting from current directory (.) exceeding (+) 100 kBytes (100k).

The second part (-exec rm {} \;) invoked given command on every found file. {} is a placeholder for current file name, including path. \; just marks end of the command.

Remember to always check whether your filtering criteria are proper by running raw find:

find . -size +100k 

Or, you might even make a backup copy before deleting:

find . -size +100k -exec cp --parents {} ~/backup \; 
like image 29
Tomasz Nurkiewicz Avatar answered Sep 28 '22 03:09

Tomasz Nurkiewicz