Live data from Hacker News

Hacking ls -l

lemis.com

11–20 of 93 posts

Re: Hacking ls -l

#11
post #9

Guys! The point of this article is not to prescribe the only method of displaying human-readable file sizes. Obviously one could use `ls -lh`; the author clearly demonstrates that he is willing and able to read man pages to find answers. Rather, this is a pretty interesting look into what it actually entails to make what ought to be a very simple and straightforward change. It turns out that these simple changes are…

It is not very easy because it is an unusual request.

But I still wonder if this is easier than ls -l | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' ??

The investigations would be interesting it they were more complete, i.e. if the actual result was a change in the locale which could be appliable to other tools printing numbers besides ls (in the author TODO).

I mean, will it work with bc?

At the moment it's not better than an shell script alias giving the output to sed, but it is more complex - you have to recompile a binary for every OS you use.

Re: Hacking ls -l

#12
post #4

Is this front page worth materiel? I mean, it's good you took the time to add a ', but doing the same with sed would have been faster. Alternatively, do you know about ls -lhrS? It will print size in human formats and reverse sort the files by size - ie the bigger will be at the end of the list

Yes, it is front-page worthy, because the details give us insight into how one might make any sort of change to a codebase like this. "If you want to be a hacker, these are specific steps I took to hack something up." In a domain that most of us are not very familiar with!

Re: Hacking ls -l

#15
post #12
post #4

Is this front page worth materiel? I mean, it's good you took the time to add a ', but doing the same with sed would have been faster. Alternatively, do you know about ls -lhrS? It will print size in human formats and reverse sort the files by size - ie the bigger will be at the end of the list

Yes, it is front-page worthy, because the details give us insight into how one might make any sort of change to a codebase like this. "If you want to be a hacker, these are specific steps I took to hack something up ." In a domain that most of us are not very familiar with!

Editing ls/print.c sourcecode and recompiling is not hacking in my definition.

I usually consider the portability of the solution. I have linux i386 and x64 machines, my arm n900, an osx latop, etc.

Recompiling ls (or, heavens forbid, cross compiling!) for each machine may be a bright idea.

Adding a line to your profile that will take advantage of the existing tools like sed is closer to hacking in my definition, because it tries to think about the bigger problem - but still I wouldn't dare calling the following "hacking":

echo "alias lll=\"ls -l | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'\"">> ~/.bashrc

Re: Hacking ls -l

#16
I enable this for GNU ls like:

    alias ls="BLOCK_SIZE=\'1 ls --color=auto"
The above is a bit hacky and not very UNIXy as it's lumping more logic into ls, rather than splitting out into functional units.

Number formatting being a very common requirement, I've proposed a design for a new numfmt GNU coreutil

http://lists.gnu.org/archive/html/coreutils/2012-02/msg00085...

which would be used like:

    ls -l | numfmt --field=5 --format=%'d

Re: Hacking ls -l

#17
post #9

Guys! The point of this article is not to prescribe the only method of displaying human-readable file sizes. Obviously one could use `ls -lh`; the author clearly demonstrates that he is willing and able to read man pages to find answers. Rather, this is a pretty interesting look into what it actually entails to make what ought to be a very simple and straightforward change. It turns out that these simple changes are…

On the other hand, you could consider it a cautionary tale about not reinventing wheels, because a problem that may seem trivial at first often turns out to be far more complex than expected.

This is why you try to re-use work when possible, rather than endlessly reinventing things, because while sure, adding a comma to the printf string is easy enough, your assumptions (English locale, compiler not trying to be clever) are going to quickly become visible as things fall apart because your assumptions aren't in line with the system's assumptions.

What this story really demonstrates is that without a clear understanding of how a system is designed and the basic assumptions it makes, just "hacking on the code" is just as likely to break things as it is to fix them.

Re: Hacking ls -l

#18
post #4

Is this front page worth materiel? I mean, it's good you took the time to add a ', but doing the same with sed would have been faster. Alternatively, do you know about ls -lhrS? It will print size in human formats and reverse sort the files by size - ie the bigger will be at the end of the list

Yes, because it's interesting, and because for the OP in particular they've spent a little bit of effort up front to save themselves time in the long run, and learn something too.

Re: Hacking ls -l

#19
post #11
post #9

Guys! The point of this article is not to prescribe the only method of displaying human-readable file sizes. Obviously one could use `ls -lh`; the author clearly demonstrates that he is willing and able to read man pages to find answers. Rather, this is a pretty interesting look into what it actually entails to make what ought to be a very simple and straightforward change. It turns out that these simple changes are…

It is not very easy because it is an unusual request. But I still wonder if this is easier than ls -l | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' ?? The investigations would be interesting it they were more complete, i.e. if the actual result was a change in the locale which could be appliable to other tools printing numbers besides ls (in the author TODO). I mean, will it work with bc? At the moment it's…

When I use your sed transformation on one of my directories, I see entries like:

    drwxr-xr-x  1,653 dalke  admin       56,202 Mar  5  2,012 pubchem
    -rw-r--r--     1 dalke  staff       59,252 Nov 16  2,011 pubchem_10,000.fps.0.9.cluster
I don't want to see the year written "2,012", and the file name is 'pubchem_10000" not "pubchem_10,000".
Post reply on HN