(Useful) Stupid Unix Tricks? 2362
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?
Stupid Tricks (Score:2, Interesting)
One command I really like is,
"du -s * | sort -n"
This lists the size and name of every file or folder in a directory and orders them from smallest to largest.
ftp from a command line (Score:0, Interesting)
People here are so used to using some drag and drop gui that they didnt know you could ftp from a cli....
I like the fun little Bash stuff (Score:2, Interesting)
search and replace in files (Score:3, Interesting)
Search and replace inplace in files, using perl:
perl -pi -e "s/searchme/replaceme/g" *
In all .cpp anh .h files:
perl -pi -e "s/searchme/replaceme/g" `find . -name \*.cpp -o -name \*.h`
Or if you're a bash person:
perl -pi -e "s/searchme/replaceme/g" $(find . -name \*.cpp -o -name \*.h)
This one always surprises people for some reason (Score:3, Interesting)
find
grep -i $SOMETHING biglist
Actually that hasn't impressed anyone in a while, come to think of it. At least not since Apple figured out what a find index is.
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.
Surprised that it does it correctly. (Score:2, Interesting)
On Windows, tabbed completion grabs the FIRST entry that matches.
On Linux, tabbed completion lists ALL the entries that match.
htop, dd, wmctrl stuff (Score:1, Interesting)
Various apps have nice little things that are useful at one time or another, so to name a few:
htop (better top) - has a nice feature that does a strace on a live process by pressing s when the process is selected
dd (everyone knows this one) - while doing a large transfer, you can send it the USR1 signal and it will display the transfers' progress (bonus points for doing something like watch -n 5 killall -USR1 dd)
wmctrl (interactions with WMs) - useful in all kinds of scripts, like looping every few seconds and searching for a specific window title then running some commands based on that (did someone IM you while you're AFK?, make keyboard's NUMLOCK blink if the screen's sleeping)
etc. etc.
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*
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)
script (and scriptreplay) (Score:2, Interesting)
Whenever I want to know _what_ I did, I'll fire up script and capture my keystrokes.
If your system has it, you can "watch" yourself with scriptreplay
Re:X-forwarding (Score:1, Interesting)
For those of us that are new(ish) to the Linux world, would you be willing to describe the commands/process?
back-i-search (zsh) reverse-i-search (bash) (Score:1, Interesting)
This is usually one people don't know about. Try Ctrl+R in (at least) bash or zsh and type something. Ctrl-R again to browse. You're welcome.
Re:Talk / DD / Mount (Score:2, Interesting)
What I like is being able to choose for myself whether to have a GUI tool or use the CLI tool. Which tool I use depends on what i am doing. Often GUIs can save time. But its nice to be able to SSH in from somewhere else and just issue a few commands. I prefer to give people the choice on how much or how little hand holding they want. I think GUIs are great but Gnome philosophy is misguided, actually restricting user freedom out an insulting notion that they wont understand some features. The key is to make it so software works out of tbe box, and does not need configuration, but then to allow people to configure as little or as much as they like, and allow everything to be done by both CLI and GUI, and designing systems to be expert or user friendly as one wants. This can mean putting advanced configurations settings in an "advanced" tab for instance, instead of not including them. A feature that an advanced user might need does not have to be excluded to not confuse new users, just placed in an advanced area where expert users can get to it. Gnome has always been too inflexible and brain damaged. Like Torvalds, I stay away from it as I cannot configure it to do what i want. The system is a useless memory hogging waste of space that tries to dictate its developers preferences on you rather than let you determine how it should work. I prefer to be able to configure software the way I want it.
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
Re:Show attached block devices (Score:3, Interesting)
history - use it with grep if you forgot what you did
I rather wish that history tracking played nicely with multiple shells open at once. It never seemed to track what I'd done. I'd say,"what was that thing I got two weeks ago? I know used wget .... but can't find it!..."
history|grep wget
Strange, nothing shows. I know I did it, and my history file contains things that are older than 2 weeks.
Is there something I should be doing instead?
Re:rm -rf / (Score:1, Interesting)
I personally enjoy doing this:
touch -- -rf
This creates a file called -rf that is a bit tricky for the uninitiated to delete. Using rm with tab completion will not help and using quotes will not help. Furthermore, using a wildcard (like *) will not help. The "-rf" is interpretted as command line option to rm. The solution is predictable if you knew how it was created:
rm -- -rf
Re:A simple search (Score:3, Interesting)
I often prefer
It gives me the human-readable total for several subdirectories of the current total, summed up as total recursive usage for each. Then I can look deeper at the subdirectories that are actually causing the problem, perhaps without the -s.
Re:Show attached block devices (Score:3, Interesting)
> Strange, nothing shows. I know I did it, and my history file contains things that are older than 2 weeks.
If you run multiple shells, they sometimes over-write each others changes when you exit them, so the history from the left shell is lost because you then exited the right-hand shell.
You might try using multiple history files. In Korn shell, and probably bash shells, the HISTFILE environment variable can be changed, and it takes immediate effect. Organizing them all is left as an exercise for the reader. I have never tried doing this for a C shell variant, but I would be surprised in there was not something similar.
And, of course, you know that you can edit the history files, as long as you are not adding to them, at the time, to clear out when you misspelled a directory in the middle of a long path, and called the same command 10 times until you determined what went wrong the first time, or other space wasters.
Re:Show attached block devices (Score:5, Interesting)
du -cks
OK, it's not a trick or very obscure, but it is a useful set of flags and it spells the name of an animal. Which is cool, if you need to get out more. I need to get out more.
Re:Bah, subtlety: (Score:5, Interesting)
:(){ :|:& };:
Re:There is this part ... (Score:3, Interesting)
eject cdrom is great for locating poorly labeled machines too!
Using the "right" interpreter with env (Score:5, Interesting)
#!/bin/env python
and replace "python" with whatever your script interpreter is, then you can have the script automatically use whatever interpreter is first on your PATH. This is especially nice if you're "not sure where the interpreter executable is", e.g., it might not be in "/usr/bin" - so this helps portability. (The POSIX standards GUARANTEE that "env" is in
Yes, this is a bad idea if the attacker can control the PATH & this is security-relevant. But you can't securely run most interpreters directly anyway, so that's usually not relevant.
Re:Well (Score:5, Interesting)
Heck, I used that command in combination with 'at' to act as a makeshift alarm clock[..]
You mentioned it only in passing, so I thought I'd draw a little more attention to it. The 'at' command is a really handy way to automate one-off tasks that many people seem to miss. The interface is neat too, understanding plain English time specifications.
I've often seen people add a one-off task to a crontab, then try and forget to remove the entry once it's run!
Re:Show attached block devices (Score:3, Interesting)
sftp...
Why use that when you can be funny and:
cat file | ssh user@remotehost "cat > new_filename"
or maybe tar up a directory on the fly and send it to the remote host for fun?
tar cvzf - directory_to_tar | ssh user@remotehost "cat > tarfile.tar.gz"
Re:Show attached block devices (Score:3, Interesting)
Re:session-sharing with screen -x (Score:3, Interesting)
Log in to the Sparc next to an unsuspecting suit and do "cat bark.au >/dev/dsp"
Re:rm -rf / (Score:2, Interesting)
I wrote cluster administration software that scaled roughly into the 5000 node size per cluster (or homogeneous set of nodes - didn't really have to be a cluster).
Of course, any cluster administration software has a "parallel shell" (even back when I wrote such a thing).
When I gave tutorials I always used some form of "with great power comes great responsibility" (and that exact quote after the first Spiderman movie) slide and spent a few minutes talking about it. The very next slide simply said "cexec --all rm -rf . /" (--all ran on *all nodes* and *all clusters* in your configuration file - and keep in mind this was a DoE/DoD project so there literally were people with that many nodes).
You could always tell who the "real" sysadmins were - instant response and no need to talk much about the slide :)
If you ever want to try out some really big "oh shit" moments work in scalable cluster software administration - not only do you ruin one machine but now you do so with thousands and with efficient start up and execution times you can do so at roughly .00001 extra seconds per node! By the time you realize "oh shit" the command is run and you are dead in the water - yea!!! (rm was one that I pulled out for it's own functionality simply because of that - some people thought that silly until they accidentally did the above).
Re:Show attached block devices (Score:3, Interesting)
I prefer:
du --max-depth=1 | sort -n
This handles hardlinks among the directory better, and will also report any .files or .directories hidden at the top level.
Paying attention to dotfiles is very valuable.
Re:There is this part ... (Score:2, Interesting)
I love doing:
wget -qO- http://cdimage.debian.org/debian-cd/4.0_r5/i386/iso-cd/debian-40r5-i386-netinst.iso [debian.org] | cdrecord
Straight from the net to my cd-burner. Try to do that with Windows...
Re:rm -rf / (Score:1, Interesting)
Thinking about it: you're searching for nothing, from nothing. What do you think you're going to find?
Re:session-sharing with screen -x (Score:3, Interesting)
Tar to copy directory trees (Score:2, Interesting)
cd dir; tar cf - . | rsh remote_machine "cd dir2; tar xvpf -"
Comment removed (Score:3, Interesting)
sudo !! (Score:3, Interesting)
sudo !!
Repeats your previous command as the root user.
Very useful if you typed out a big long command, but forgot to run it as root.
I miss one great command (Score:2, Interesting)
find and xargs is an absolutely brilliant combination. I can't live without it.
find . -type f -print0 | xargs -P 4 -0 echo grep -H WHATEVER
searches for the string WHATEVER in all regular files, weird-filename-safe. xargs runs 4 parallel greps, to make use of all those CPUs and cores! Here's two test runs, note the changing order:
$ find . -type f -print0 | xargs -P 5 -n 2 -0 echo grep a ./1 ./2 ./5 ./6 ./7 ./8 ./3 ./4 ./9 ./1 ./2 ./3 ./4 ./5 ./6 ./7 ./8 ./9
grep a
grep a
grep a
grep a
grep a
$ find . -type f -print0 | xargs -P 5 -n 2 -0 echo grep a
grep a
grep a
grep a
grep a
grep a
Re:cat! (Score:2, Interesting)
cleartool ci -nc `cat`
A pipe (as I hint at [slashdot.org] elsewhere) through while ... read would reduce that:
cleartool lsco -cview -me -r -s | while read FILE ; do cleartool ci -nc ${FILE} ; done