Score:-3

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

im flag

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.

hu flag
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.
ng flag
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.
Score:4
hr flag

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.

Score:4
cn flag

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.

Basavaraj Pn avatar
im flag
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'
vanadium avatar
cn flag
Thanks for the feedback. I again checked the command that I presented in the answer, and it works.
Basavaraj Pn avatar
im flag
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
vanadium avatar
cn flag
If this answer resolved your question, then please show your appreciation by "accepting" it: click the checkmark next to the question.
NotTheDr01ds avatar
vn flag
@vanadium I took the opportunity to point out this oversight on the OP's [new question](https://askubuntu.com/q/1442635/1165986).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.