Score:1

How to delete files with specific size?

us flag

Using ls -lSrh I get this output:

-rw-rw-r-- 1 a a 2.4K Feb 15 04:44 10010.png
-rw-rw-r-- 1 a a 2.4K Feb 15 04:43 1000.png
-rw-rw-r-- 1 a a 2.4K Feb 15 04:44 10009.png
-rw-rw-r-- 1 a a 2.4K Feb 15 04:44 10008.png
-rw-rw-r-- 1 a a 2.4K Feb 15 04:43 10004.png
-rw-rw-r-- 1 a a 2.4K Feb 15 04:43 10000.png

How can I delete all .png files with the specific 2.4K size?

Bodo avatar
pt flag
Try `find` with condition `-size`. Print the file names first in order to check the result. If it matches your expectations, you can add the `-delete` action.
Score:0
ca flag

Bodo's comment points you at the right direction, but things are a bit trickier than they seem. That is because the file size you see in the ls -lSrh output is not the exact size of the files, but the file size in human readable format, due to the use of the -h flag. To use find to find the files of a specific file size, you need to know the exact size of the files, so you need to run the command:

ls -lSr

that is the ls command you used, but without the -h flag, which outputs file sizes in bytes.

If you run this command, then you will most likely find that all these files that previously seemed to all have the exact same size of 2.4K have now somewhat different sizes. What you need to do now is to determine the lower and the upper file size limits of the files you want to delete and then run the following find command from within the directory your files are located in:

find . -type f -name "*.png" -size +<lower_limit>c -size -<upper_limit>c -delete

Explanation of the above command:

  • .: search in the current directory.

  • -type f: search for files only, not directories.

  • -name "*.png": search for files that their name ends with .png.

  • -size +<lower_limit>c -size -<upper_limit>c: search for files with sizes between the <lower_limit> and <upper_limit>, in other words for files larger than the <lower_limit> (that's what + means) and lower than the <upper_limit> (that's what - means). The c suffix that follows <lower_limit> and <upper_limit> is used to tell find that the sizes are in bytes, which is what you get for the file sizes from ls -lSr.

  • -delete: delete the found files that fulfill the above criteria.


CAUTION:

First run the above command without -delete to make sure that it lists the correct files! If you are satisfied with the output, then and only then add -delete!

The -delete action deletes the found files permanently, so you will not be able to recover them in case of a mistake. Always make sure to keep a backup of the original files in case something goes wrong.


References:

I sit in a Tesla and translated this thread with Ai:

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.