6 ms·
In a sense, yes. However, some of the things ls does are 'borrowed functionality'. That the displayed listing can be sorted by a flag to ls is a bit of an aberr
by betawolf33 14y ago
In a sense, yes. However, some of the things ls does are 'borrowed functionality'. That the displayed listing can be sorted by a flag to ls is a bit of an aberration in the Unix philosophy. We're meant to have a separate tool for sorting - sort.
I agree that a certain flexibility is useful in the Unix tools, but a well-founded argument can be made that flexibility costs you simplicity.
- pilif 14y agoThe fact that ls can produce output that can't be reliably sorted using the sort utility shows that ls is actually doing three things instead of just one: Listing files, sorting the output and then displaying the output in a human readable format. This is not to criticize ls though - I would imagine that no matter how small the operation (well aside of operations executed directly by the cpu), there's still the possibility to break it up into smaller parts. At some point convenience just trumps modularity. ls -lt is just more convenient than a (hypotetical) ls -t | sort | xargs fileinfo | display-fileinfo On a related note: Windows PowerShell comes closer to this ideal by being able to send arbitrary objects between pipes. This still has the side-effect though that single commands become much more awkward to use as they don't produce output that's usable on its own.
- cturner 14y agoI would imagine that no matter how small the operation (well aside of operations executed directly by the cpu), there's still the possibility to break it up into smaller parts. The scripting system could have been designed to make layering easier. Bourne descendants are ugly and awkward. Hence, unix evolved into a situation where ls had lots of features, whereas were there a better scripting system, we might have had a standard environment which you could introspect deeper and extend with more confidence.
- ori_b 14y ago> The scripting system could have been designed to make layering easier. How? sh sucks for anything involved, but I can't think of a way to simplify pipelines much.
- jcmhn 14y agoA line oriented xargs could have been built in from day one. A pair of shell variables honored by commands that let you specify output wrappers or separators would have been nice too. Think "-Q" in gnu ls, but universally supported and user-controlled.
- reddit_clone 14y agoHow would you make 'sort' do all the things that 'ls' can easily do? How do you sort a multi-column output by 3rd column?. You have to write some scripts for that.
- elwin 14y agosort -k 3
- tedunangst 14y agoThat works for the trivial case of the 3rd column, but not for everything ls can produce. There's no way to make sort work on dates as formatted by ls (which may or may not have years or hours, depending on how old) or sizes (which you may want to pretty print as human readable.) What sort option puts 1.0K, 1.0M, and 1.0G in the right order?
- betawolf33 14y agoWhile I do appreciate the point that ls is doing sorting that sort would struggle to manage, isn't this an argument for better functionality in sort? Someone pointed out that ls is really doing three things - listing files, sorting them and displaying them. That seems fairly accurate, and indeed it's convenient that it does. But it's not 'doing one thing'. I'm not saying that's necessarily bad (as I just said, it's convenient), but it's not hardline Unix-way purity. For practical reasons like those you raise, it makes sense for ls to handle its own sorting rather than leave it to the sort command, just as it handles its own formatting rather than let the user manage it. But those features _are_ something other than just reporting file listings, whatever the motivation for the feature creep. This convenient flexibility makes ls less single-task, less Unix-adherent but more generally useful on its own.
- masklinn 14y ago> There's no way to make sort work on dates as formatted by ls (which may or may not have years or hours, depending on how old) or sizes (which you may want to pretty print as human readable.) Both of which wouldn't be the job of a "do one thing and do it well" `ls`. That one would display dates as a UNIX timestamp and sizes as bytes. Aside from that, surely if ls can format information it's possible to reverse that formatting.