- 1 year ago
How To Delete All Files of A Certain size in Command Line
To delete all files of a certain size using the command line, you can use various shell commands depending on your operating system. Here, I'll provide examples for both Windows and Unix-like systems (such as Linux and macOS).
Keep in mind that deleting files is a permanent action, so use these commands carefully.
On Windows:
You can use the forfiles
command to loop through files and execute a command on files that match specific criteria.
For example, to delete all files in the current directory that are larger than 10 MB:
batchforfiles /S /C "cmd /c if @fsize GTR 10485760 del @path"
Explanation:
/S
tellsforfiles
to search for files recursively in subdirectories./C
specifies the command to execute for each file.cmd /c if @fsize GTR 10485760 del @path
is the command. It checks if the file size (@fsize
) is greater than 10 MB (10 MB = 10 * 1024 * 1024 bytes), and if so, it deletes the file (del @path
).
On Unix-like Systems (Linux and macOS):
You can use the find
command to locate files based on various criteria and then delete them.
For example, to delete all files in the current directory and its subdirectories that are larger than 10 MB:
bashfind . -type f -size +10M -delete
Explanation:
.
specifies the current directory as the starting point for the search.-type f
specifies that only regular files should be considered.-size +10M
filters files that are larger than 10 MB.-delete
deletes the found files.
Remember that using commands like these can lead to irreversible data loss if not used carefully. Always double-check your command and consider performing a dry-run (printing the list of files to be deleted without actually deleting them) before running the actual command.