1

There is an existing question for "Finding all sparse files", and I think I could use it, but the scripts were too complex to understand. I don't think the GUI of Ubuntu file manager provides a way to know whether a file is sparse or not. How to tell?

The file system is EXT4.

  • 4
    The accepted answer there is quite straightforward -- a one-liner. The find part will need to be tailored for your requirements -- as posted, it searches all files from the root directory. The format after the -printf uses %S to show the estimated proportion of 'real' data blocks, and %p to show the pathname. This is piped to an awk script which only lists the files that use fewer disk blocks than a non-sparse file of the same size would need. – Paul_Pedant Oct 04 '20 at 21:07
  • My answer there gives code that does just that (and more reliably than gfind -printf %S approaches). In which way does it not address your question? – Stéphane Chazelas Oct 05 '20 at 11:57

2 Answers2

3

The accepted answer is a one-liner:

# find / -type f -printf "%S\t%p\n" | gawk '$1 < 1.0 {print}'

So there's a number of parts to this. Let's break it down:

find / -type f

This part will search all files on the machine

-printf "%S\t\%p\n"

This part will print out the "sparsiness" of the file and the complete filename.

So the output, at this point will look like a list of entries in the following format:

1.23456 /tmp/a/file

If the first number is less than 1.0 then the file is considered "sparse".

So then we can filter this through awk:

gawk '$1 < 1.0 {print}'

This will limit the output to only the lines which are sparse by only reporting on those where the first number is < 1.0

The result is a list of all the files that are sparse, along with their "sparsiness".

That's a lot of work for a simple command!

If you just want to test to see if a specific file is sparse then you can use a variation of this. e.g.

find file_to_test -printf "%S"

will return a number. That can be tested to be < 1.0

  • Thanks for the explanation, but I am not trying to "search for files". I am trying to check if a specific file is sparse file or not. Let's say that the file name is "test.img". How can I check if "test.img" is sparse or not? – Damn Vegetables Oct 05 '20 at 01:24
  • 2
    That's what the last section of the answer explains; find test.img -printf "%S". If the answer is less than 1 then it's a sparse file. – Stephen Harris Oct 05 '20 at 02:21
1

Do you want to check it on the terminal?

find FILENAME -type f ! -size 0 -printf '%S\n'

If it prints something less than 1 it's a sparse file.

laktak
  • 5,946
  • Bash (IIRC) does not understand non-integer arithmetic (hence the use of awk in previous answers). This may work in ksh, however. – Paul_Pedant Oct 05 '20 at 10:33
  • Thanks, I've removed that part because it is better answered by Stephen Harris. – laktak Oct 05 '20 at 11:08