-3

I want to delete the files in a particular directory which are smaller than than a specific size.

For example, I have five files on a server:

  1. File1.20221001 size:-1430KB
  2. File1.20221002 size:-1320KB
  3. File1.20221003 size:-27654KB
  4. File1.20221004 size:-350KB
  5. File1.20221005 size:-765434KB

I want the first four files to be deleted based on size of the fifth.

MDeBusk
  • 1,386

2 Answers2

4

For files in a particular directory (i.e. not needing recursive search), I'd suggest using zsh with its L glob qualifier to select files by length in bytes (or LK for length in kilobytes). For example:

ls -l -- *(.LK-765434)

or

rm -- *(.LK-765434)

For the rules about how units and rounding are applied during comparison, see man zshexpn.

steeldriver
  • 142,475
4

You can use the utility find to find files larger than a specific size, and have find run the rm command on the found files as in:

find <yourdirectory> -type f -size -765434k -exec echo rm {} \;

This will find files only (-type f) that have a size smaller than (- before the number) 765434 KB (the k after the number indicates the unit). On the found files, the command echo rm {} will be used, where {} will be replaced by the path of the found file.

Only the command that would be executed will be printed: this is good to check whether you find what you expect. To effectively delete the files, remove the echo.

vanadium
  • 97,564