I was expecting to see the first comment in here complaining about his use of 'cat', as in all of the examples his second argument could've easily taken a filename argument..
sort order.*
is surely more elegant than
cat order.* | sort
which is fair enough, however, as it happens, i generally do end up using 'cat' in the way he's used it.. for such small jobs nobody can be genuinely worried about the overhead, and it comes down to a matter of taste..
personally, i find that using 'cat output | $command' helps to separate out the 'logic' of what i'm doing, if that makes sense..
also, again, purely as a matter of taste
i'd prefer
egrep 'Hardcover|Kindle'
over
grep "\(Kindle\|Hardcover\)"
EDIT: (as alexfoo has pointed out, this isn't a proper AND as it worries about the order.. my bad, still useful though :D )
and as a sidenote, something i only found recently, but which is quite useful, a logical AND with egrep looks like
[ EDIT - Two replies as original post had been edited by the time I posted the first. ]
> and as a sidenote, something i only found recently, but which is quite useful, a logical AND with egrep looks like
>
> egrep 'Hardcover.Kindle'
That's not a true logical AND since it won't pick up an entry with the text "Kindle Hardcover". Only entries with the word "Hardcover" eventually followed by "Kindle". To cover both cases you'd need:-
egrep 'Hardcover.*Kindle|Kindle.*Hardcover'
(Of course, someone will now show how this can be done in even fewer characters).
I find the "cat foo | .." in the beginning and "| cat > bar" at the end form more regular.
While iterating on a command line, it keeps things uniform, rather than switching between "sort foo" and "tai64nlocal < foo" and "ffmpeg -i foo", by which I mean: different programs take their input in different ways. You can normalize by making each take standard input, and feed the chain with a "cat".
I can understand liking the regularity but in production code or web examples it shouldn't be done because of the overhead. However, your example doesn't make sense.
If sort, tai64nlocal, and ffmpeg are all happy to read stdin so you can do
Minor nitpick from the man page: "egrep is the same as grep -E. fgrep is the same as grep -F. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified."
You can do that sending while you're waiting for the disk to provide said GiBs. I also believe that useless uses of cat are often acceptable for readability (many novices are not familiar with redirecting standard input, particularly not as the first thing on a command line).
Who said the GiBs need to be fetched from disk; they could already be in RAM. Even if not, it's still adding many system calls and context switches when the CPU could be doing other things; the machine isn't running just this one thing.
It's not that simple anymore... In modern CPUs, NOPs are discarded by the decoding units so they never occupy the exeuction units. If decoding BW is not saturated (and most often, it's not), NOPs are indeed "free".
> If the original title begins with a number or number + gratuitous
> adjective, we'd appreciate it if you'd crop it. E.g. translate
> "10 Ways To Do X" to "How To Do X," and "14 Amazing Ys" to "Ys."
> Exception: when the number is meaningful, e.g. "The 5 Platonic Solids."
One I learned for the first time the other day is 'paste'. Good for people like me who never fully grokked awk, it joins lines from separate files into a single file.
Say you have two files, one with lines of numbers:
1
2
3
... and one with letters:
A
B
C
$ paste numbers letters
1 A
2 B
3 C
Want CSV?
$ paste -d, numbers letters
1,A
2,B
3,C
Or, with '-s' you can join lines from inside the same file. For instance, you can sum numbers:
$ paste -sd+ numbers
1+2+3
$ paste -sd+ numbers |bc
6
(Thanks to a Stack Overflow post somewhere for suggesting that one!)
Useful example: the total resident memory size of all chromium processes:
Hmmm, is this HN worthy? These commands are so basic that I don't expect any HN-reader using Unix systems not to know those.
What about some slightly less basic ones that I'd think would be useful for many people.
# less with syntax highlighting
alias less="/usr/share/vim/vimcurrent/macros/less.sh"
tailf # tail -f, but better & shorter
mtr # check your connection (ie., traceroute with more info)
htop # nice a colorful process listing (better than top)
locate # search files by name -- that late-night disk thrashing is useful after all!
I ask the programmers on my team, some of whom are quite junior and unexperienced, to send weekly status reports with an overview of what they did in the previous week, what they expect to do in the next, and other. Other is usually made up of areas of concern and interesting things that have made it onto one's radar. (You can put basically whatever you like in the other section. I'm still hoping somebody will send a joke.)
Since I wouldn't ever ask them to do something I wouldn't do myself, I send these reports too. This link will go in my miscellany section this week. I think it'll be useful, and I wouldn't have ever found it or even considered including something like it had it not shown up on HN.
Because the vim macro changes all settings so it behaves just like less, including making the file effectively read-only, and without the overhead of loading all the plugins.
I think it's obligatory that someone writes 'strace' in threads about articles like this, so here goes. strace is fantastic for debugging certain categories of problem.
Absolutely, especially for those who do network programming under Unix/Linux. Combined with lsof, it can help a lot in troubleshooting issues such as deadlocks.
I was surprised and a little disappointed that `join(1)` didn't join the list!
With the files in the example (order.out.log, order.in.log), to join every record on it's id, you would do something like:
$ join -j 2 order.out.log order.in.log
111, 8:22:19 1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:22:20 Order Complete
112, 8:23:45 1, Joy of Clojure, Hardcover, 29.99 8:23:50 Order sent to fulfillment
113, 8:24:19 -1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:24:20 Refund sent to processing
I thought to learn something useful for programing (debugging, program runtime analysis, etc.). But instead the article is just about the generic commands cat, sort, grep, cut, sed, uniq, find and less. It is not really development related.
I disagree. They may not be obviously related to coding, though they'll probably end up being useful at some point anyway... But they're definitely useful for working with logs, working with datasets, working with config files, and a host of other development-related tasks. Just yesterday I was on a Windows machine and dearly felt the loss of sed and uniq. I have a task lined up for today to either find Windows alternatives or install msys.
You and I do, but surely you know a web dev or two that struggles to read log files meaningfully or doesn't know how to grep their html source for something?
Send the article along, it's not always about you...
I have found it to be surprisingly useful. Nobody uses all the commands but remembering that something like zcat exists can be extremely useful. Also remembering to pipe through sort before sending through uniq is helpful as well.
less +F filename does the same thing, but loses the following when the logfile gets rotated. Does the same thing happen to tail -f? (edit: alexfoo answered that tail -F tracks the file properly)
one benefit to less +F is that you can cancel the following and read normally in less.
Those are, indeed, 8 commands every developer should know. Calling them "Linux Commands" is a little weird - I don't think there's anything there not specified in POSIX, and I think they all appear on OS X and other unix systems.
Regardless of the actual content, I applaud the "storytelling" way of presenting content. With a flow of examples that tie into each other the reader is given background context and can say to him/herself "I've had that problem before!", as I found myself doing.
lsof -i always gets left out of these lists. It's really handy for quickly seeing what daemons are running (under which user) and on what interfaces they're bound to.
~$ help eval
eval: eval [arg ...]
Execute arguments as a shell command.
Combine ARGs into a single string, use the result as input to the shell,
and execute the resulting commands.
Exit Status:
Returns exit status of command or success if command is null.
personally, i find that using 'cat output | $command' helps to separate out the 'logic' of what i'm doing, if that makes sense..
also, again, purely as a matter of taste
i'd prefer
over EDIT: (as alexfoo has pointed out, this isn't a proper AND as it worries about the order.. my bad, still useful though :D )and as a sidenote, something i only found recently, but which is quite useful, a logical AND with egrep looks like