Sometimes I’m just so glad I know shell. I mean things like
find . -type f -print0 | xargs -0 -n 100 ls -1sk | sort -n | tail -10
For the uninitiated: the above command lists the ten largest files under the current working directory (including subdirectories). How do the Windows power users do stuff like this? PowerShell? Python programs? Can you even write one-liners in Python?
On a related note, it always pains me to watch people browse to a file in a file manager GUI. You click through a bunch of folders and stare at each list of file names for a second before finding the right one, click on it, and by the time you’ve found your file the coffee’s gone cold. With the shell, it’s “sTABnTABkTABsTABn” and you’re done in a fraction of the time. Hot coffee. Why can’t you do that in the file browser GUI?
The shell is like a dishwasher. When I was a just poor undergrad CS student living off macaroni and ketchup “borrowed” from my roommate, I couldn’t afford a dishwasher. There were more important things to spend money on, such as beer, science fiction, and macaroni. At the time, washing dishes the old-fashioned way didn’t seem so bad. In fact, I coped without a dishwasher for a long time after I would have been able to just buy one. Now I wouldn’t dream of doing all my dishes manually.
If you liked this, click here to receive new posts in a reader.
You should also follow me on Twitter here.
Comments on this entry are closed.
{ 7 comments }
Python programs? Can you even write one-liners in Python?
Sure you can:
File sizes are in bytes, not 1k blocks though.
It would be nice if some Python module would provide a set of higher level file system operations, similar to what the typical shell utilities provide, in order to make this sort of code significantly shorter.
Perhaps something like that exists already?
Getting a nice list of files under a directory was probably the ugliest part of working with the default library, so yes, there is room for better stuff. There would still be a syntactic problem even with the better library though, the order in which the functions appear is reverse from the way they are in the pipe,
op1 input | op2 | op3 | op4
becomesop4(op3(op2(op1(input))))
.Turns out there’s an evil trick which can make Python use a pipe-style syntax though:
Actually, you don’t even need to wrap the pipe parts into the operator overloaded objects, just start the expression with something that keeps wrapping the results of the operations:
And it looks like I still haven’t figured out how to format multiline code in comments…
Risto, that’s pretty cool stuff. This makes it really easy to combine generators and normal functions over values. Much more elegant than your first version!
For code, it seems best to just surround it with <pre> and </pre>.
The Windows Explorer equivalent for “sTABnTABkTABsTABn”is “sENTERnENTERkENTERsENTERn”. I’m pretty sure they have something similar in Konqueror or Nautilus or whatever. There was a pretty handy way of doing the file size thing in Windows XP’s Explorer, but it doesn’t seem to work in Vista.