I am writing a simple helper function to show a specific folder in the format I want:

find . -maxdepth 1 -not -name "." -printf '[%TY-%Tm-%Td]\t%s\t%f\n' | sort -n

However I want to show the filesizes in a nicer format. I found this answer which I used to create a bash function called hr.

user@host:~$ hr 12345

I tried using this in the find call as so:

find . -maxdepth 1 -not -name "." -printf '[%TY-%Tm-%Td]\t'$(hr %s)'\t%f\n' | sort -n

But it still continues to show the full filesize. How should I modify this so it works as expected?

  • At first I'd like to offer you to use -exec stat {} + with desired format instead of -printf – Costas Oct 8 '15 at 8:45
  • @don_crissti I want it ordered by date (which is the first column). This is Ubuntu, but also using it on CentOS – LeonardChallis Oct 8 '15 at 11:37

Assuming sane file names, with numfmt from gnu coreutils (8.21 or later):

find . -type f -printf '[%TY-%Tm-%Td]\t%s\t%f\n' | numfmt --field=2 --to=iec-i

You can further format the output via --padding= and --format= options (and with most recent versions even set output precision) e.g:

... |  numfmt --field=2 --to=iec-i --suffix=B --padding=8


... |  numfmt --field=2 --to=iec-i  --format='%10.3f'

If you don't mind the permissions & links-count columns, with gnu ls:

ls -gohAt --time-style=+%Y-%m-%d

How about -exec du after your -printf ?


jon@local$ find . -maxdepth 1 -not -iname "." -printf '[%TY-%Tm-%Td]\t' -exec du -bh {} \;
[2015-04-01]    1.6K    ./main.go
[2015-04-01]    5.2K    ./main_test.go

This will require some adaptation if you want to use it, but the dc utility incorporates a little known - if possibly very useful - feature of handling arbitrary output radices. It looks strange at first.

echo 1024o 1025p 1024p 1023p|dc

 0001 0001
 0001 0000

But its not too difficult to catch on.

ls -s  file;\
ls -hs file;\
echo 1024o 1048576p|dc

1048576 file
1.0G file
 0001 0000 0000


dd bs=1kx1k seek=500 of=file <>/dev/null 2>&0;\
ls -s  file;\
ls -hs file;\
echo 1024o 512000p|dc

 0500 0000

I'm partial to dc, but bc can do the same kind of thing, it's just not as straight-forward as I regard it.

Anyway, dc will not help you to handle your find ... -printf script - which I think you'll have to do an -exec at least to get it to work the way you want it to if you don't use numfmt as already suggested. But if you were to adapt to something a little more flexible and fleshed out like that, it could be very useful to you. In any case it can probably more easily do much of the job your hr function does.


If fields order is not important

du --time --time-style=+[%F] -sh *

In other case you are free to reorder it by sed for example

… | sed 's/\(\S*\s*\)\(\S*\s*\)/\2\1/'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.