

Linux command line techniques: IO redirection and piping - kwellman
http://blog.interstellr.com/linux-command-line-tricks-you-need-to-know-io

======
stevelosh
This is all pretty basic stuff. One thing that always bites me is trying to
use sudo to redirect to a protected file. This doesn't work, because the shell
is doing the redirection, not sudo:

    
    
        sudo cat ~/something.txt | grep 'hello' > ~someoneelse/result.txt
    

You can get around this by using sudo to run a new shell that does the
redirection:

    
    
        sudo sh -c "cat ~/something.txt | grep 'hello' > ~someoneelse/result.txt"

~~~
ax0n
sudo cat ~/something.txt | grep 'hello' | sudo tee ~someoneelse/result.txt

or use "| sudo tee -a" (append) in place of >>

------
russell_h
Not to diminish the value of the article (which seems well written and
probably useful to some), but do these really count as "tricks"? That seems
analogous to calling variable assignment a C programming "trick".

~~~
silentbicycle
Indeed. If you don't know these "tricks", _you don't know Unix._ That's
probably why it says "tricks you _need_ to know".

This probably comes from learning Unix via graphical interfaces, such as GNOME
on Ubuntu, OS X, etc., and blog posts by other people with the same
background.

Kernighan and Pike's _The Unix Programming Environment_ is probably still the
best place to learn this stuff.

~~~
nodata
I think most people don't know Linux now. I'm basing this on the nose-diving
quality of replies to technical Linux questions and problems over the past
couple of years.

Normally it goes something like this:

A: I have problem X with Y.

B: Oh I don't know anything at all about Y, but if you migrate to Z I bet you
won't have the problem any more.

A: Okay I'll do that. Thanks!

or:

A: I have a problem X with Y.

B: Oh did you try (completely unrelated irrelevant thing)

A: No, but I will. Thanks!

Seriously.

~~~
silentbicycle
On average, the BSD community is quite a bit more clueful. Not because the
Linux community is dumb, but because Linux is quite a bit more _visible_ \-
Complete newbies are dramatically more likely to use a newbie-friendly Linux
distro such as Ubuntu than FreeBSD, OpenBSD, etc. (The BSDs seems to get more
users who already have experience with Linux or commercial Unices.)

Unfortunately, Linux newbies searching for help may run across bad advice from
people with barely more experience, creating a feedback loop.

------
astrange
Is there a *nix programmer who doesn't know this? This article is hardly
worthwhile to anyone vaguely competent.

------
nodata
These aren't tricks. These are the things you learn on the very first day of
using a *nix system. How this is being voted up I will never know.

~~~
th0ma5
I wonder if HN will wind up being sort of like Slashdot in the sense that the
experienced people on the site will continue to make the site rich of such
experience, but will be accommodating to those not experienced in whatever
area, and slowly, over time, the inexperienced will continue to exert
influence. Mind you, not as a collective whole, but as an ongoing wave of new
people (those inexperienced people will become experiences either though
reading the site, or just going along as usual). So could this cause a slight
dumbing down of the site? Or will the two groups (veterans vs. inexperienced)
just read different articles?

~~~
nodata
I'd like HN to stay experienced only. I'm not sure how that can happen. Maybe
new members votes count less, like wise or experienced peoples' advice counts
for more.

The alternative is to do what reddit does: subreddits. Or do what reddit
doesn't do: automatic filtering (essentially per-user subreddits based on past
voting).

------
jmhobbs
As far as redirection goes, this skipped the least known bit (IMHO), closing
descriptors.

    
    
        jmhobbs@katya:~$ cat file-that-doesnt-exist
        cat: file-that-doesnt-exist: No such file or directory
        jmhobbs@katya:~$ cat file-that-doesnt-exist 2>&-
        jmhobbs@katya:~$

------
cellularmitosis
'cat christmas_list.txt | sort > christmas_list.txt'

the above is wrong. that will clobber the file.

------
rexyo
Very nice, thanks for sharing

~~~
thingie
Not really, it's a fraction of the basic functionality of bash, which is not
to be learned from a mosaic of random blogposts, but from something more
complete. If you want something "more special", try, for example, Unix Power
Tools book.

~~~
rexyo
Of course its just basics, its a good start for beginners. It reminds me of
the stuff I used to teach to junior programmers wanting to explore Linux.. Im
already an advanced Linux user, but there's always something you haven't come
accross yet... Thats one of the things I like best about Linux in general and
bash.

