-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,067
  • 1
  • 8
  • 21
  • 1
    Here you go: https://askubuntu.com/questions/413658/how-to-delete-files-and-subdirectories-by-size-and-age?rq=1 Next time, search before asking. – mikewhatever Oct 06 '22 at 16:00
  • You haven't described how you decide which file is your standard. In your example, it's the largest file, but it's not clear from the question that that is always the criterion. – MDeBusk Oct 06 '22 at 16:28

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
  • 131,985
  • 21
  • 239
  • 326
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
  • 82,909
  • 6
  • 116
  • 186
  • I tried using above command but when I used command find /c/test -type f -size -10k , i find the files less than the 10kb size ,but when I use find /c/test -type f -size -10k -exec echo rm {} \ i get the error missing arugment to '-exec' – Basavaraj Pn Oct 07 '22 at 03:16
  • Thanks for the feedback. I again checked the command that I presented in the answer, and it works. – vanadium Oct 07 '22 at 11:31
  • Thanks alot , That worked for me later as I was trying to work on my local folders and I faced that issue , But later it got resolved – Basavaraj Pn Oct 09 '22 at 16:54
  • 1
    If this answer resolved your question, then please show your appreciation by "accepting" it: click the checkmark next to the question. – vanadium Oct 10 '22 at 08:58
  • @vanadium I took the opportunity to point out this oversight on the OP's [new question](https://askubuntu.com/q/1442635/1165986). – NotTheDr01ds Nov 26 '22 at 17:50