
Awesome but commonly unknown Linux Commands - kezzah
http://www.anchor.com.au/blog/2011/08/awesome-but-often-unknown-linux-commands-and-tools/
======
thristian
If you're running a modern Linux desktop, you should have a "notify-send"
command which will cause a message to be displayed by your desktop-
environment's pop-up notification system (on Ubuntu, this command is in the
libnotify-bin package). I have two scripts I keep around which I call "notify-
success" and "notify-failure":

notify-success:

    
    
        notify-send --icon=gtk-dialog-info Success! "$*"
    

notify-failure:

    
    
        notify-send --icon=gtk-dialog-warning Failure! "$*"
    

When I want to run a long-running command and don't want to have to keep
checking on it, I'll do something like this:

    
    
        make -j3 && notify-success "Build complete" || notify-failure "Build failed"

~~~
xtacy
On a mac, you could also use growlnotify.

    
    
        growlnotify -m "Hey I am done!" -t "Done!"

~~~
LiveTheDream
Also useful is the `-s` option to make the notification sticky

    
    
        growlnotify -s -t 'Done!' -m 'And I'm not going away until you click me'

------
rdtsc
All these article links should just end up here:

<http://www.commandlinefu.com/commands/browse>

you can sort by date, #(votes) and also search

The top all-time popular one is:

* run last command as root: $ sudo !!

~~~
nuclear_eclipse
To be fair, that's not even a command; that's a shell expansion that
automatically substitutes your previous command in place of the !! before
executing it.

~~~
lanstein
Thank you for another '...and this is why I love HN' moment.

~~~
Nick_C
Just be aware it may not execute the command you think. man bash for
HISTIGNORE. Personally I never use it with sudo; it's too dangerous.

~~~
nuclear_eclipse
This is also why I disabled HISTIGNORE in both bash and zshell. zshell also
allows me to press TAB to expand the command in-place before I press enter, so
I can still use the shortcut without the danger.

------
user24
One I use all the time is this:

$ pwd

/Users/me/Sites/foo

$ pushd .

$ cd /

$ pwd

/

$ popd

$ pwd

/Users/me/Sites/foo

Or in English: If you're in a directory that you need to leave but you know
you'll go back there in a minute, use "pushd ." to save that directory. Then
go off and do whatever you need to, and when you need to return to the saved
directory, use "popd" to take you straight back there.

~~~
nfm
You can also use `cd -` to return to the previous directory.

~~~
footle
git "borrowed" this idea too. You can use `git checkout -` to checkout the
last thing you checked out; very handy when merging.

~~~
nosignal
Python sort of did too, you can use _ to access the last-returned variable in
the interpreter.

    
    
        [testfunc(x) for x in testlist]
    
        <successful result>
    
        success = _
    
        print _
    
        <successful result>
    

It's useful when using the interpreter as a calculator, or when you're bashing
out some calculation you can never remember how to do properly.

~~~
brettnak
The same thing also works in irb.

    
    
        >> "foo"
        => "foo"
        >> bar = _
        => "foo"
        >> bar
        => "foo"

------
danieldk
On Linux I very regularly use 'strace', to see what a program is currently
doing (in terms of syscalls), what files were opened/read/written/closed, etc.

~~~
thomasknowles
strace is the Daddy of debugging especially when you can attach it to an
already existing program using it's PID. I have solved so many permissions
errors problems with strace where the application itself just failed without
logging anything.

~~~
nz
Similar experience here, except that I've solved similar issues (and far more
complicated issues) with DTrace instead of strace. The range of information
that DTrace can retrieve about a running application surpasses anything that
strace can retrieve. DTrace can retrieve information on a systemic scale,
whereas each strace instance operates on a single process. And the overhead is
significantly lower with DTrace.

For many of the systems applications I design for Illumos, I've used DTrace
probes as a way of logging very frequent events, on demand (to avoid frequent
IO). All of the events that _must_ be logged for the application to function
properly, are logged and fsync'd.

I think that in most systems dynamic tracing will eventually replace a
significant portion of the logging functionality that people code into their
applications.

Either way, if you think strace is sweet, give DTrace a spin.

Some helpful links:

[0] A video demostration of DTrace, by the creator of DTrace.

[0] <http://www.youtube.com/watch?v=6chLw2aodYQ>

[1] A post I wrote, demonstrating DTrace. Similar posts can be found on the
blog's dtrace-addict page.

[1] [http://nickziv.wordpress.com/2011/04/08/adventures-of-a-
dtra...](http://nickziv.wordpress.com/2011/04/08/adventures-of-a-dtrace-
addict-part-0/)

[2] A wiki that contains DTrace examples for various languages and system
facilities. Some examples may be Illumos-centric.

[2] <http://www.solarisinternals.com/wiki/index.php/DTrace_Topics>

[3] DTrace's home. Contains blogs by the engineers behind DTrace.

[3] <http://www.dtrace.org>

UPDATE: Meant to reply to parent's parent.

~~~
tangothedog
systemtap is another utility in this vein that's worth checking out

<http://sourceware.org/systemtap/>

~~~
nz
I already checked it out and the timer probes don't work at all.

On one system, I couldn't even invoke kernel functions.

On another system, the kernel panicked as soon as I executed `stap -e ...`.

Basically unusable, at this point.

I hope it improves because developers on linux could really benefit from
dynamic tracing.

------
snorkel
watch is rather handy:

    
    
        # watch packet counters
        watch ifconfig
    
        # watch tcp connections
        watch netstat -plan
    
        # watch the load average
        watch uptime
    
        # watch directory contents
        watch ls -l

------
burgerbrain
Meh, I was hoping for at least _semi_ obscure things like the 'moreutils'
utils.

Speaking of which, many people don't know about the 'moreutils' utils. Check
them out ;)

~~~
kachnuv_ocasek
I see sponge is somehow useless, how is `| sponge /etc/passwd` different from
`> /etc/passwd`.

~~~
ominous_prime
The redirect happens before the command is executed, so if you're trying to
read from /etc/passwd, it's already been overwritten. Sponge will buffer the
output and write the file after the first command has executed.

~~~
kachnuv_ocasek
Ah, excuse my ignorance.

------
Symmetry
_pstree_ prints out a tree of all the running processes, arranged by who
spawned who.

------
sciurus
Anchor's blog is worth subscribing to if you're a sysadmin. The technical
content is good, and they can be merciless yet amusing when writing about
software that fail to meet their standards of good taste (see
<http://www.anchor.com.au/blog/tag/fail/>).

------
julius
A ridiculously unknown tool is "expect". Automates anything. Saved me hours of
typing.

Wrote about it (use case + example) here: [http://www.chocobrain.com/Advanced-
terminal-automation-with-...](http://www.chocobrain.com/Advanced-terminal-
automation-with-expect/4NRKAvM2NWZqai6a3CoDQe)

------
michaelcampbell
xargs. Too few people know xargs. Every time I see a "find" with "-exec" in
it, my soul cries a little bit. (Yes, sometimes it might be necessary, but in
the vast majority of cases not.)

~~~
planckscnst
find -exec is almost always faster and more secure than xargs.

~~~
michaelcampbell
I'd like to hear some justification, cites, or examples for either of these
assertions.

~~~
planckscnst
I was mis-remembering the "faster" part. It is slower unless you have a
version that supports the "-exec {} +" option. However, for security and
robustness, see this:

[http://stackoverflow.com/questions/896808/find-exec-cmd-
vs-x...](http://stackoverflow.com/questions/896808/find-exec-cmd-vs-xargs)
[http://www.gnu.org/software/findutils/manual/html_node/find_...](http://www.gnu.org/software/findutils/manual/html_node/find_html/Security-
Considerations-for-xargs.html)

------
helwr
related:

[http://www.quora.com/What-are-some-lesser-known-but-
useful-U...](http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-
commands)

[http://www.quora.com/Linux/What-are-some-time-saving-tips-
th...](http://www.quora.com/Linux/What-are-some-time-saving-tips-that-every-
Linux-user-should-know)

[http://www.quora.com/What-are-the-most-useful-swiss-army-
kni...](http://www.quora.com/What-are-the-most-useful-swiss-army-knife-one-
liners-on-Unix)

------
bdr
In the 'top' family, iotop is extremely useful for identifying bottlenecks. It
shows you the I/O rate per process, which wasn't even possible until a recent
kernel.

------
losvedir
iftraf sounds interesting. I was looking for just such a program the other
day.

I was sitting there and noticed with my iStat monitor that I was uploading
something at 250 KB/sec. I closed Chrome and eventually all running programs
in the doc, yet it still continued.

I tried to find out WHAT was uploading that, but to no avail. Any suggestions
for tools? I ended up trying iftop, lsof -i, and netstat to get a glimpse, but
it stopped before I could get to the bottom of it.

~~~
Game_Ender
Try "nethogs" found it at:
[http://www.commandlinefu.com/commands/view/2849/triple-
monit...](http://www.commandlinefu.com/commands/view/2849/triple-monitoring-
in-screen#comment)

~~~
sciurus
Nethogs is great when you need to determine what processes are transferring
data or how much they're transferring. I got curious how it works, so I ran it
through strace and looked through the source. /proc/net/tcp lists all
established TCP connections. It includes local and remote addresses and ports
and the inode for the socket. Nethogs sniffs traffic and associates it with
its entry in /proc/net/tcp. It takes the inode from there and scans through
/proc/ _/fd/_ looking for the file descripter that has that inode to determine
which process has the socket open. Once it finds the process it adds it to a
table of inode to process id mappings so it doesn't have to scan through /proc
again the second time a packet for that connection comes through.

------
dpapathanasiou
lsof is also great for monitoring network activity: you can have it tell you
about all the tcp/udp connections, as well as who's attached to which port.

~~~
mmphosis

        lsof -i

------
atsaloli
Here are the tools I find indispensable as a sys admin:

\- screen for accessing servers

\- lshw for seeing Linux server hardware config

\- cfengine for automating my system administration

\- atop as an advanced top

<http://www.atoptool.nl/downloadatop.php>

atop uses color to show when a subsystem goes over warn/critical threshold. it
can be run in present time, or can be used to go back in time and "play back
the tape".

------
evmw
"at" is very handy for scheduling one-time scripts/commands

    
    
      echo my_script.py | at 4am tomorrow
    

then list queued jobs with

    
    
      atq
    
    

note: its not enabled by default on mac os x, see

    
    
      man atrun

------
Jach
While we're mentioning tools that break the "prog | xargs .." pattern, I've
found _ack_ to be pretty useful. ( <http://betterthangrep.com/> )

------
ffffruit
Also good - bashmarks - directory bookmarks for the shell.

[http://www.huyng.com/bashmarks-directory-bookmarks-for-
the-s...](http://www.huyng.com/bashmarks-directory-bookmarks-for-the-shell/)

------
moe
I think there's a critical one missing in this list:
<https://github.com/busyloop/lolcat>

------
helwr
related: [http://www.quora.com/What-are-some-lesser-known-but-
useful-U...](http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-
commands)

------
uggedal
qmv from renameutils will bring up $EDITOR with all files in the current
directory in two columns. Edit some filenames and save. The files will be
renamed accordingly.

~~~
phaker
'rename' uses arbitrary perl expressions, including regexes. from the manpage:

    
    
      rename 's/\.bak$//' *.bak

------
joelthelion
No autojump? ;-)

------
zobzu
im surprised when i see all this as if any of those super basic commands or
programs were rare ;-)

------
molecule
> Want to kill all processes being run by a given user? Issue a pkill -U
> USERNAME; sure beats the hell out of...

similarly: killall -u USERNAME

OFWGKTA

------
dicroce
strace

nuff said.

