Slashdot Log In
(Useful) Stupid Unix Tricks?
Journal written by *no comment* (239368) and posted by
timothy
on Wed Nov 05, 2008 03:48 PM
from the sed-is-underrated dept.
from the sed-is-underrated dept.
So the other day I messaged another admin from the console using the regular old 'write' command (as I've been doing for over 10 years). To my surprise he didn't know how to respond back to me (he had to call me on the phone) and had never even known you could do that. That got me thinking that there's probably lots of things like that, and likely things I've never heard of. What sorts of things do you take for granted as a natural part of Unix that other people are surprised at?
Related Stories
[+]
(Useful) Stupid Vim Tricks? 702 comments
haroldag writes "I thoroughly enjoyed the recent post about Unix tricks, so I ask Slashdot vim users, what's out there? :Sex, :b#, marks, ctags. Any tricks worth sharing?"
[+]
News: (Stupid) Useful Emacs Tricks? 412 comments
Count Fenring writes "Since the Vi version of this question was both interesting and popular, let's hear from the other end of the spectrum. What are your favorite tricks, macros, extensions, and techniques for any of the various Emacs? Myself, I like 'M-x dunnet' ;-)"
[+]
(Useful) Stupid Regex Tricks? 516 comments
careysb writes to mention that in the same vein as '*nix tricks' and 'VIM tricks', it would be nice to see one on regular expressions and the programs that use them. What amazingly cool tricks have people discovered with respect to regular expressions in everyday life as a developer or power user?"
[+]
(Useful) Stupid BlackBerry Tricks? 238 comments
This discussion has been archived.
No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
Full
Abbreviated
Hidden
Loading... please wait.
rm -rf / (Score:5, Funny)
rm -rf /
Bah, subtlety: (Score:5, Insightful)
Parent
Re:Bah, subtlety: (Score:5, Funny)
@$**& well we know it works in cygwin too.
Parent
Re:rm -rf / (Score:5, Insightful)
Parent
Re:rm -rf / (Score:5, Funny)
OMG OMG What did I do. What did I do. I am so fired. I am so EFFIN fired.
Parent
Re:rm -rf / (Score:5, Funny)
Parent
Re:rm -rf / (Score:5, Funny)
Parent
Re:Show attached block devices (Score:5, Informative)
screen - too useful, run apps in a virtual console which you can attach, deattach and share
cd `pwd -P` - Jump into the real directory (from a linked directory).
history - use it with grep if you forgot what you did
strings - just show the printable strings from a file
tail and head - tail -f is a lifesaver
sftp - i really shouldn't need to explain this.
file - do magic stuff
Hope that's some help.
Parent
Re:Show attached block devices (Score:5, Informative)
Change to your previous directory. It's great for going from dev to test environments from time to time.
Parent
Re:Show attached block devices (Score:5, Informative)
pushd/popd , change directory saving the old one on a stack.
Parent
Re:Show attached block devices (Score:5, Informative)
This shows all attached block devices (it also errors like crazy, hence the | more)
blockdev --report /dev/* | more
Redirect stderr much?
Parent
Re:rm -rf / (Score:5, Funny)
rm -rf /
wtf??? (do not try this at home)
Really? What does it do? Think I'll try it and s
CARRIER LOST
Parent
Re:rm -rf / (Score:5, Funny)
Parent
Re:rm -rf / (Score:5, Funny)
cat /dev/urandom > /dev/dsp
Parent
Re:rm -rf / (Score:5, Funny)
sleep 8h; cat /dev/urandom > /dev/dsp
That's my alarm clock.
Parent
Well (Score:5, Funny)
Well.
dd if=/dev/zero of=/dev/sda1
Re:Well (Score:5, Interesting)
Sometimes a quick white-noise machine is relaxing. Heck, I used that command in combination with 'at' to act as a makeshift alarm clock when I was just moving into my first apartment and had forgotten my only other electronic device with an alarm (my cell phone) at the office.
Parent
session-sharing with screen -x (Score:5, Insightful)
screen is awesome.
There is this part ... (Score:5, Funny)
Re:There is this part ... (Score:5, Funny)
I once made my friends' jaws hit the floor when I burned a cd for them - from console.
And once I had this strange feeling that something was wrong with the CD drive of a machine I was working at in the console until I realized I was opening and closing the CD tray on a machine in another room!
Parent
Re:There is this part ... (Score:5, Interesting)
I do both microsoft and unix development. This has led to some interesting situations. I wrote a mathematical parser in c#/.net that could process math expressions at runtime using Regex to get tokens from the expression. The regex wasn't too bad. But after a code review, my pointy-haired manager made me comment each symbol in the regex. 40 lines of comments to describe 1 line of code.
As a consultant, VI is my absolute favorite tool. Not on unix projects, on microsoft projects. It always happens eventually. Someone needs to modify a file in a way that screams for regex search with replace, but is a nightmare in visual studio or some other windowy editor. So I have them stand behind me while I write an long, arcane-looking regex line in VI. When I press enter, the entire file instantly morphs into exactly what they want. I can think of no better way to justify my exorbitant bill rate. lol
Parent
Tab (Score:5, Informative)
Re:Tab (Score:5, Funny)
Woah! Got any more?
(yes, I'm being sarcastic)
Parent
Re:Tab (Score:5, Insightful)
You'd be surprised how often I have seen experienced programmers manually type out long commands or directory paths, instead of using tab completion. Sometimes I have to restrain myself from ripping the keyboard from their hands and using tab to enter the path myself in a 10th of the time.
Parent
Re:Tab (Score:5, Informative)
With bash, you can even get tab completion for hostnames. Try this:
ssh user@l[tab]
Everything after the @ is filled in from /etc/hosts.
Parent
Re:Tab (Score:5, Funny)
Parent
I never knew that command (Score:5, Insightful)
And I've been administering Linux systems for awhile now. Step back for a moment and you'll find that "man pages" and "info" are actually a pretty awful way to distribute documentation. As a supplement they'd be fine, but as the main source of information on how to use many commands... not so much.
Re:I never knew that command (Score:5, Insightful)
This is only true because people write such terrible and incomplete manual pages.
The original Bell Labs man pages completely described the system from the point of view of an administrator or user. The only better documentation was the source.
The current blight of wimpy, inaccurate and incomplete man pages seems to originate from the GNU developers who insist on using the terrible "info" crap, writing huge volumes of text with no real content, and the tradition is continued by Linux developers who generally provide little or no man page documentation -- presumably in the hope that users of their software will be tempted to ask questions on various mailing lists where they can be ritually disemboweled for displaying such a lack of understanding and disturbing the peace of the cognoscenti who have much more important things to do than answer questions of mere users of their software.
Parent
X-forwarding (Score:5, Informative)
I've seen Windows people go slack-jawed in astonishment as I ssh to the other side of the world and run X programs over forwarding.
Some refuse to believe it, others shake their heads and walk away.
Re:X-forwarding (Score:5, Insightful)
Parent
Re:X-forwarding (Score:5, Informative)
You could easily have an entire Ask Slashdot just on ssh, perhaps the greatest unix command ever invented.
One of it's many great uses is creating secure tunnels:
ssh user@remotehost -L123:example.com:456
Open a tunnel on your local machine, port 123, to example.com, port 456, via the remote host
ssh -R lets you go in the opposite direction (tunnel from remote end to local end), but if your application supports SOCKS, it's even easier:
ssh user@remotehost -D8080
Creates a secure tunnel supporting the SOCKS protocol.
Parent
Talk / DD / Mount (Score:5, Informative)
On the older systems, talk was a great utility.
dd, device duplicator / disk destroyer
mount, what I can't have a desktop icon?
also managing disk volumes and the old conventions of /opt, /u, /usr, /usr/local
This new fangled Linux craze with all of the UI tools is feeding it. Redhat is training admins that are dependent on a given release of their enterprise software (which I am a huge fan of) but not teaching them how it works under the hood.
How about slirp? scp?
The one ray of hope seems to be a new generation hacking their bsd and linux based (iPhone/Android) phones and having fun in a somewhat embedded (but full blown) *nix environment.
cd - (Score:5, Informative)
In terms of navigation directories efficiently, I find that "cd -" is often forgotten (changes directory to your previous directory). I personally find it very useful, and couldn't live without it!
One word: (Score:5, Funny)
Showers
Job control. (Score:5, Informative)
fg, bg, kill, Ctrl-Z, &. Learn it. Know it. Live it.
Even if they do know about job control, I've seen people look for a background job with ps, and then kill it using the PID. In most shells you can just do kill %, e.g. kill %1
short list of shell tips (Score:5, Informative)
Assuming you already know the simple stuff like how to use shell quotes correctly, what you can do with ps and top, ...
Share mouse and keyboard (Score:5, Informative)
When I pop up with my laptop to discuss with a colleague, after a while I might do on their computer:
xhost +mylaptopname
and on my laptop I do:
x2x thecomputername:0 -west
Then suddenly my mouse can go over the two computers, my keyboard works on both as well, and I can even copy-paste between the two computers. It looks like the two computers got united. In a flash, newbies get a new idea of what means unix and X ;-)
My personal fave (Score:5, Funny)
echo '[q]sa[ln0=aln256%Pln256/snlbx]sb3135071790101768542287578439snlbxq'|dc
grep --color (Score:5, Informative)
grep --color
For some reason, many people are greatly surprised when they figure out that grep will highlight matches for them.
lsof (Score:5, Informative)
lsof
That will list which processes have anything under
It's also useful to find who's accessing what device. For example, say you're trying to listen to an mp3 and Amarok bitches about the sound device not being available. In that case, you could do something like this (assuming you're using ALSA):
lsof
That will list what processes are accessing any of your ALSA sound devices.
Re:rev (Score:5, Funny)
The rev command has got to be one of the most useless Unix commands I've ever come across. It's almost as if someone's first c program somehow got taken up as a part of standard Unix! Maybe in the days before sed and awk and perl it had some function in pipes that I can't grok, but nowadays other than making hints for video game websites I can't imagine what it's for.
Unhackable encryption of course.
Parent
Re:rev (Score:5, Interesting)
I sometimes use rev to sort text by the end of the line, not the first. This is often useful when comparing two similar file structures.
For example:
$ wc -l foo/* bar/*
6 foo/dead.letter
86 foo/xorg.conf
6 bar/dead.letter
54 bar/xorg.conf
$ wc -l foo/* bar/* | rev | sort | rev
86 foo/xorg.conf
54 bar/xorg.conf
152 total
6 foo/dead.letter
6 bar/dead.letter
(Yes, I'm aware you can use sort -k to specify the sort key, but this is quicker and easier)
Parent
Re:Listing directory contents without the ls comma (Score:5, Informative)
You're not giving echo an asterisk as a paratemer. You're giving the shell an asterisk, which it dutifully expands. echo (which in this case is a shell builtin, but it doesn't have to be then just echoes them back.
This isn't some echo peculiarity. It works for anything, even commands that don't normally take files, or even with files that look like switches (conversely, if you want to treat all subsequent arguments as files, not switches, most programs have a '--' switch):
$ ls
a -l b c
$ ls *
-rw-r--r-- 1 marcansoft users 0 2008-11-05 21:58 a
-rw-r--r-- 1 marcansoft users 0 2008-11-05 21:58 b
-rw-r--r-- 1 marcansoft users 0 2008-11-05 21:58 c
$ ls -- *
a -l b c
In the second example, ls sees "ls a -l b c" and takes -l as a switch instead of a filename.
Parent
Re:Listing directory contents without the ls comma (Score:5, Funny)
Parent
Re:This one always surprises people for some reaso (Score:5, Informative)
Parent
Re:grep -R (Score:5, Interesting)
Hell, I think it's probably a GNU extension, because it's still not in Solaris.
I think rgrep appeared around BSD 4.4, though.
Oh well. I still surprise people with backticks. *sigh*
Parent
-exec as a test (Score:5, Informative)
One great feature of find that many people are unaware of is that you can use -exec as a test, not just as an action. For example, this is equivalent to your command above:
find . -exec grep -q {} \; -print
The "-print" action is only executed if the -exec command returns success.
You can do a lot of handy things with this. Here's a real-world example from earlier today. I wanted to change the mime-type of all the xml files in my svn repository from "application/xml" to "text/xml":
find . -name \*.xml -exec sh -c "svn propget svn:mime-type {} | grep -q application/xml" \; -exec svn propset svn:mime-type text/xml {} \;
Parent
Re:Find / Grep (Score:5, Funny)
Parent
Re:A simple search (Score:5, Informative)
Xargs is much more fun with complex data processing. e.g.
Convert all PSDs to PNGs:
Parse out and sort column 2 from a semicolon delimited file:
Oh, I almost forgot about one of my favorite tricks. Count the number of items:
It even works when the list of items has oddities. e.g. I had a list where every other line was blank. So I needed to count n/2 the value. Except that one of the blank lines wouldn't copy, so I actually needed (n+1)/2.
Want to make sure your sig is under 120 characters? Type "wc -c" in, paste it into your terminal, then press CTRL-D. Instant character count.
Ah, all the fun stuff you can do with Unix tools.
Parent
Re:A real time saver! (Score:5, Informative)
I didn't think I needed to say this, but I just showed someone this and they thought it was a legitemately helpful command...
Parent