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.
(Useful) Stupid Unix Tricks?
|
Log In/Create an Account
| Top
| 2362 comments
(Spill at 50!) | Index Only
| Search Discussion
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
rm -rf / (Score:5, Funny)
rm -rf /
Re:rm -rf / (Score:5, Funny)
Parent
Re:rm -rf / (Score:5, Funny)
puercopop@localhost ~ $ which which
which: no which in (/usr/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/4.1.2:/usr/qt/3/bin:/usr/games/bin)
Burn her anyway!
Parent
Re:rm -rf / (Score:4, Funny)
Parent
Re:rm -rf / (Score:5, Funny)
$ touch woman
But I always get this back:
$ touch: woman: Permission denied
:(
Parent
Re:rm -rf / (Score:4, Funny)
Try doing it in your own home for better results..
Parent
Re:rm -rf / (Score:5, Funny)
Parent
Re:rm -rf / (Score:5, Informative)
Pshaw! All 1337 sysadmins just live as root!
Yes, but all the really stupid ones do as well.
Parent
Re:rm -rf / (Score:5, Insightful)
Parent
Re:rm -rf / (Score:5, Insightful)
No. People who run as root all the time are either n00bs or morons.
N00bs if they have never spoken the words, "Oh sh*t!" after running a command;
Morons if they have.
Parent
Re:rm -rf / (Score:5, Insightful)
Parent
Re:rm -rf / (Score:5, Funny)
Thank you.
There's two kinds of real UNIX Admins.
1. Those who have yelled "Oh Shit!"
2. Those who will.
Parent
Re:rm -rf / (Score:4, Funny)
Parent
Best Dilbert (Score:5, Funny)
Dilbert: I think you mean Unix. Yes we have a lot of Unix machines here.
PHB: Oh... [pauses and thinks]... If the company nurse comes by, tell her I said, "never mind."
Parent
Re:rm -rf / (Score:5, Informative)
I don't get it. You told him to give rm an absolute path, it doesn't matter where he was.
Parent
Re:rm -rf / (Score:5, Funny)
e.g.
A: "Type rm -rf
B: "OK"
A: "Next..."
B: "Wait it's not done yet"
A: "?"
A: "!"
Seems like people should be more careful about product directory names
Don't call your directories stuff like "star" or "slashdot" if you ever might need to get people to remove them over the phone.
Parent
Re:rm -rf / (Score:4, Funny)
You have a definition for a "short lunch"?
What the hell kind of sysadmin ARE you?? You certainly don't wear the same brand of suspenders us REAL techies do.
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)
Along that line are pushd and popd. pushd <dir> changes to the specified directory and pushes it onto a stack of directories; popd changes to the directory at the top of the stack and removes it. There are commands for manipulating the directory stack but I don't know or use them.
With zsh, and I think with Bash as well, you can setopt AUTO_PUSHD and setopt PUSHD_SILENT and then cd behaves like pushd.
(Both of these commands, along with cd -, work in the Windows command interpreter too.)
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:4, Informative)
or if you're a vi freak like me:
set -o vi
escape (enters command mode)
k to scroll up
/ to search
Parent
Re:Show attached block devices (Score:5, Informative)
vi foo.c
make
^Rvi^O^O^O^O^O^O^O^O^O^O^O^O^O^O
and so on.
Parent
Re:Show attached block devices (Score:5, Informative)
Organizing them all is left as an exercise for the reader.
Cakewalk. Put the following in ~/.bash_profile or ~/.profile
For ubuntu:
SHELLID=(echo `tty` | sed 's!/!.!g')
HISTFILE=$HISTFILE$SHELLID
Logout. Log back in. Bada-bing.
Parent
Re:Show attached block devices (Score:5, Informative)
I think you missed a '$' before the open parenthesis, it should be:
SHELLID=$(echo `tty` | sed 's!/!.!g')
Also, you could replace "echo `tty`" with simply "tty":
SHELLID=$(tty | sed 's!/!.!g')
Parent
Re:Show attached block devices (Score:4, Funny)
.py or it didn't happen.
Parent
Re:Show attached block devices (Score:5, Informative)
Your data is corrupt: only the current sig is ever shown.
Parent
Re:Show attached block devices (Score:5, Informative)
sftp - i really shouldn't need to explain this.
I much prefer sshfs. Diff doesn't work so well over ftp ;)
Parent
Re:Show attached block devices (Score:5, Informative)
tail and head - tail -f is a lifesaver
I use tail -F, which is the same as tail -f, but works on non-existent files. Useful when tailing log files from programs that start a new log file every time it runs. Using tail -F in this case, you can just leave tail running while you start and restart the program overwriting the log file.
Parent
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.
Parent
Re:Show attached block devices (Score:5, Funny)
Oh my ghod. This is considered informative? Who let all the PFYs in?
Parent
Re:Show attached block devices (Score:5, Informative)
I actually like less better than tail -f. If you less a file, and then hit SHIFT-F it will tail the file, but you can break out of it and scroll around and search for terms. Very handy while looking at log files.
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:Show attached block devices (Score:4, Funny)
It's shorter you want eh? How about...
Aaaand that's my cue to go find something to do away from the computer.
Parent
Re:Show attached block devices (Score:5, Funny)
On the contrary, shorter IS something to be proud of.
Cool! Thanks! I'll tell my boyfriend that next time!
Parent
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.
Parent
Crashed My Laptop! (Score:5, Funny)
My system booted up fine, so of course the first thing I wanted to do was make it happen again.
Thirty seconds after the output finished and I'm looking at the garbled POST screen again. My laptop finished booting, I ran the command a third time before coming to tell Slashdot and
Parent
Bah, subtlety: (Score:5, Insightful)
Parent
Re:Bah, subtlety: (Score:5, Funny)
@$**& well we know it works in cygwin too.
Parent
Re:Bah, subtlety: (Score:5, Interesting)
:(){ :|:& };:
Parent
Re:rm -rf / (Score:5, Insightful)
Parent
Re:rm -rf / (Score:5, Funny)
Mediocre minds think alike. Great minds are unique.
My thoughts exactly.
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)
OMG OMG What did I do. What did I do. I am so fired. I am so EFFIN fired.
Ha, I didn't get fired when I did it. It was about ten years ago now, I had a linux dualboot setup on a test PC and didn't need the linux install any more.
I thought, "I've always wanted to try rm -r /" so I did. About four seconds later it dawned, with an "oh shit" that I still had the dos/windows 3.11 partition still mounted read write.
Fortunately, I didn't permanently lose anything between good backups and Norton Disk Doctor.
Parent
Re:rm -rf / (Score:5, Insightful)
Fortunately, I didn't permanently lose anything between good backups and Norton Disk Doctor.
Yes, you do need good backups whenever you are running Norton products. Good idea.
Parent
Re:rm -rf / (Score:5, Funny)
Parent
Re:rm -rf / (Score:5, Funny)
I keep getting Windows cannot find 'rm' when I type that in. Am I doing it wrong?
what the hell. I had karma to burn.
Parent
Re:rm -rf / (Score:5, Funny)
[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live"
Parent
Re:rm -f /lib/libc* (Score:5, Funny)
You want subtle?
ln -f /bin/rm /usr/bin/diff
Parent
Useful tricks. (Score:5, Informative)
I can't live without svn. Svn is a revision control repository, usually used for source code. What makes it really powerful is that you can _easily_ have a history of everything that has changed in a file and when. On my systems, I keep /etc in svn, plus bind's zone files, plus all the non-image web content, and the "Network Documentation" folder.
Second trick, rsync. I use it to backup my home directory to another box. Very nice when you go through a hard drive/year.
Screen -x was my next pick, but somebody already mentioned it.
"echo ProtocolKeepAlives 120 >> /.ssh/config" No more dropped ssh sessions because of stupid nat boxes.
su -u Username -s .. become Username, but keep the current shell. Good for diagnosing permissions problems when the user has a /bin/false shell. (named/www-user/backup/etc).
A little awk goes a long way. Not the big-bad-I-am-a-programming-language-awk, but the smaller-friendlier extract one or two columns of text from something awk. ex. awk '{print $2}' prints the thing in the second column. Add -F the field separator tool and it gets really useful.
Better example. Here is a postfix log line.
Nov 5 16:27:19 pdc postfix/smtpd[13601]: 92B3F499C25F: client=exprod5mx254.postini.com[64.18.0.49]
Here is the awk to extract just the message id. awk -F': ' '{print $2}'
And here is the "I didn't get this message your mailserver must have eaten it" disprover. It searches the maillog for every message from or to a given address and extracts the full email transaction for that message id.
grep -i user@domain.com /var/log/maillog | grep smtpd | awk -F': ' '{print $2}' | sort -un > temp.fil && grep maillog -f temp.fil
Next trick, back ticks. `` Back ticks substitute the output of a command within a command.
Ex. Name a file after the date. echo "hi" > `date +%Y%M%d`.txt
On the subject of dates. date -d'yesterday' or 'last week' or '-4 hours' can be handy.
Last one. Loopback nat with Iptables, so you can access local hosts by their external ip. (Instead of setting up split dns.)
iptables -I POSTROUTING -s $local_network -d $local_network -j SNAT --to $lan_interface
My local network is 192.168.0.0/24 and the netfilter lan ip is 192.168.0.1, so that becomes...
iptables -t nat -A POSTROUTING -d 192.168.0.0/24 -s 192.168.0.0/24 -j SNAT --to 192.168.0.1
-ellie
Parent
Re:Useful tricks. (Score:5, Insightful)
Don't use backticks unless you are stuck with bourne shell. Use $() instead:
echo hi > $(date +%Y%M%d).txt
They have two benefits over backticks: you can nest them and they are easier to see.
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)
Hello?
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
Re:rm -rf / (Score:5, Funny)
/dev/random to the dsp device ? I prefer to the midi device, then it sounds like someone throwing a drumkit and grand piano down the stairs at the same time.
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
Re:Well (Score:5, Informative)
You only need a tiny bit of entropy to seed the pseudorandom number generator in /dev/urandom. Once it's seeded you get all the pseudorandom numbers you want. /dev/random gives you truly random numbers, and is highly dependent on the amount of entropy the system has. It will block if you run out of entropy, urandom will not.
Parent
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!
Parent
session-sharing with screen -x (Score:5, Insightful)
screen is awesome.
Re:session-sharing with screen -x (Score:5, Funny)
In a similar vein, back in the days of floppies you could have endless fun ejecting other people's disks from Sun workstations. They put it in, you eject it. They put it in, you eject it. Repeat till you get bored or it looks like they're about to do a 'who'.
Parent
Re:session-sharing with screen -x (Score:5, Informative)
I simply put eject and "eject -t" in a loop and go look in the server room -- the hyperactive server is the one I was looking for.
Parent
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, Funny)
True story.
A friend and I help admin the computers in my (former) high school. Due to security the doors were locked during off hours, and I'm not a frequent helper so I don't have the keys, but my friend did.
There was once when I was basically stranded in the computer lab, and my friend was in the server room (where the cell phone signals didn't reach). I don't know what he was doing at that time, but "walls" (on the linux machines) and "net sends" (to the Windows servers) didn't seem to work, so I ran a script to open and close the CD tray hoping to catch his attention.
I got a message asking "wtf are you doing?" a few minutes later :)
Parent
Re:There is this part ... (Score:5, Funny)
True story:
I was in a class learning how to install and run a network management program (this was mid-1990s), and the class had a set of IRIX systems to work from. There were six systems to work on, so some of the students had to pair-up.
The class was pretty-much done, and we were waiting to be dismissed.
Suddenly, I get a message on the console of my system stating that the system would reboot in five minutes. (This was the system default.) I went in, found and killed the shutdown process, then checked the logs to see who kicked it off. It was the two clowns in front of me. I went into their system (there was no real security here, if you haven't figured that out. The systems weren't on the Internet, so it wasn't a big deal) and set the system to init state 0 (this reboots the system immediately, for you guys that are oblivious to init states).
They looked at each other, they looked at me (I was just sitting there, looking at their reaction), and they looked at each other again. One said, "See? I TOLD you we shouldn't have f*d with him!"
Heh. :)
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
Re:There is this part ... (Score:5, Funny)
Years ago in college we had a few Sun 3 workstations, and all of us CS types had logins on them of course. I also worked for the university computing department as did one of my fellow CS students. Often I'd be working on the Sun 3 when he'd log in and run his "xeyes from hell script", which would essentially open a bunch of xeyes with random geometries.
Usually, when he was doing this he was sitting in another office working on a DOS machine running NCSA Telnet, which had the awesome feature that it would run an FTP server when you were using telnet.
So, I would go to another machine, figure out his IP address using "w", and ftp to his desktop. Then I'd type the killer: "cd a:".
With that, it would use the old BIOS call, which in the absence of a floppy would literally lock up the machine and prompt you to stick a disk in.
It gets better (or, worse if you're Colin). After he'd scrape up a floppy and stick it in, I would get the prompt back letting me know he'd found one. No problem: "cd b:".
There was no second floppy on that particular machine, so the BIOS would emulate two floppies and keep track of which was which. So he had to run and grab another to make the machine usable again. At that point, I could lock the machine up at will simply by cd'ing to the other floppy. And he would find me, and use "write" to ask for a truce.
Funny thing is, it was really easy to turn off the ftp server. I guess I can let that cat out of the bag now.
Parent
grep and awk (Score:4, Insightful)
Find / Grep (Score:3, Informative)
I find a combination of find/grep to be pretty useful. Not sure about how unknown it is, but I do know that several UI-based linux admins around our office don't know about "stacking" commands. (I know, I know, one would think they would be mutually-exclusive
find . | grep [string]
-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: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
Re:grep -R (Score:4, Funny)
BTW: How does one ork a cow?
Very, VERY, carefully.
Parent
On to the pranks! (Score:4, Funny)
I once worked with an admin that wrote a program that wrote directly to a users terminal and prompted in the same way "write" did. One notable exception is that he let you put whatever username@hostname you wanted.
I got quite a few requests from "yourmom@pronindustry.com" to chat...
One Upped (Score:4, Informative)
For example, I did these to some of my favorite people:
banner "PORN HERE" > /dev/pts/4
echo "All files deleted." > /dev/pts/3
cat dictionary.list > /dev/pts2
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, Funny)
Apparently in Unix you can connect to another computer - get this - *without having to be at the screen* to see it! I think the software was developer by a company called Shell or something. You use 'shhhh' to use it, which I think tells the computer to basically quieten down its TCP communications so it can get ready for the connection to the other machine. I'm probably not doing it justice but you have to see for yourself.
It's not as good as Windows though because you still have to use typing to do things, and then it's only very boring things like deleting files. :-( I haven't found Solitare or Pinball yet. Hopefully Shell will invent a remote desktop program soon though so we can replace Remote Desktop!
Parent
Re:Tab (Score:5, Funny)
Seriously, I was giving a presentation at a LUG meeting a few years ago, and during a break, some guys came up to me and said "We know you can't type that fast. How do you do that?"
Parent
Re:Tab (Score:4, Informative)
funny! I just chanced on this blog entry [wordpress.com] about ZSH. It sounds really intriguing and apparently has great tab completion.
Parent
Re:Tab (Score:4, Informative)
Long story short, I know no single person who really used zsh for more than a week and went back to their old shells. By "really" I mean "steal a zshrc from somewhere" or "read the docs". The feature list is too long to even begin, but suffice it to say that once you are used to zsh, you can not imagine why you ever used anything else. Yes, it's that amazing.
Get "From Bash to Z Shell" if you like dead trees. Subscribe to the mailing list & join the IRC channel if not.
Find me under my username on freenode or send a PM via /. if you want a quite extensive zshrc which does loads of neat stuff.
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, Funny)
[...] a gift from the dieties.
Are they the ultra-slim super humans I keep hearing about? Or am I confusing them with deadly cocktails??
Parent
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
Re:I never knew that command (Score:5, Funny)
Pretty much anything in here: http://www.openbsd.org/cgi-bin/man.cgi [openbsd.org]
Parent
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
Listing directory contents without the ls command (Score:4, Informative)
I discovered if you give the echo shell command an asterisk as a parameter, it dumps out the file names of the current directory. (The sad thing is I had a practical use for this when a less-than-clueful-collegue deleted the /bin directory, leaving the system without an ls program).
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
A simple search (Score:3, Informative)
Works like a charm for finding a file containing a keyword. Another one I often use is:
Human readable disk space:
Track down where your space is going:
Re:A simple search (Score:4, Informative)
Your
is fine for non-GNU UNIX grep.
If you have GNU grep, then
Most systems will GNU grep will also have an rgrep command which is the same as 'grep -r'.
But the find approach allows much more sophisticated searches. Such as:
find . -name \*.xml -not -user root -exec grep -l keyword {} \;
(search all .xml files that aren't owned by root)
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
Screen (Score:4, Funny)
A sys admin was recently surprised that I didn't use screen. My explaination was that all that C-x stuff reminded me too much of using Emacs.
Moderators are free to mod this Flamebait or Insightful, depending on personal bias.
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:4, Informative)
http://www.math.umn.edu/systems_guide/putty_xwin32.html [umn.edu]
First result of X-Forwarding on Google.
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
Re:X-forwarding (Score:4, Informative)
That means that hypothetically I could be looking at porn via my home computer
If you're going to do that, make sure you run firefox with the -no-remote flag. Otherwise it will detect that you're in an SSH session, and just open a new window on your local machine.
Parent
Re:X-forwarding (Score:4, Insightful)
They're probably slack jawed and astonished at how primitive and slow it is compered with a remote desktop connection.
And let's be honest, the real good bit is not that you can forward X but that you can forward anything you want, for instance, I will ssh into our gateway server and forward the RDC port from our Exchange server because it's quicker and easier than our VPN connection. RDC runs much faster over ssh than X does, btw.
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.
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)
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!
directory stack (Score:4, Informative)
The advantage of the directory stack over "cd -" is that the directory stack always remembers where you last were. "cd -" only remembers until you change directories again.
In tcsh (I don't know other shells), you can do directory stack substitution. =0 is current directory, =1 is one up, =2 is two up, and so on.
I also use bindkeys to bind Control-G to 'dirs -v' so I can look at the directory stack with ease, even in the middle of a command.
Personally, I think directory stack commands are the least-known, but most useful feature in tcsh.
Parent
Re:directory stack (Score:5, Informative)
In tcsh (I don't know other shells), you can do directory stack substitution. =0 is current directory, =1 is one up, =2 is two up, and so on.
In bash, it's ~0, ~1, ~2, etc.
Parent
One word: (Score:5, Funny)
Showers
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:This one always surprises people for some reaso (Score:5, Informative)
Parent
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
Seen on a friend's T-Shirt (Score:4, Funny)
Re:Seen on a friend's T-Shirt (Score:4, Funny)
yeah, but first you will need to
find /bin/laden
Parent
Paste your code to /dev/tty for upload (Score:3)
Done!
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, ...
dmidecode (Score:3, Informative)
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 ;-)
sudo !! (Score:3, Informative)
Finding where your disk space went. (Score:4, Informative)
du -s * | sort -n -- similar to above, find the biggest files & subdirectories of the current dir
du | xdu -- only when you're in X, obviously. Better grain than above, with the ability to drill down into subdirectories
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
A nice tip from the OSX world (Score:4, Informative)
My previous place of employment was a Mac shop, where I discovered the wonderful pbcopy and pbpaste commands. Why they aren't a standard part of every X windows distribution, I'll never know, but they are damned handy.
What they do is allow you to read and write from the cut-and-paste buffer from the command line. "pbpaste" will print the currently copied text to stdout, while "pbcopy" will replace the buffer with stdin.
Fortunately, there are some third-party X equivalents for this, such as xsel [freshmeat.net] or xclip [freshmeat.net], which can be adapted to work in the same way.
Rougly equivalent:
pbcopy
xsel -i --clipboard
xclip -in -selection clipboard
pbpaste
xsel -o --clipboard
xclip -o -selection clipboard
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.
Re:grep --color (Score:4, Informative)
Parent
find, dc, sed, dd pipes (Score:3, Informative)
The find command is definitely a good tool to be familiar with. Also, I've done a lot of really wacky stuff with sed in the past. (and sed experience can help you to work with ed and even ex, for those times the system has crashed so hard that's all you've got) (though I don't think I've had that happen since I left Ultrix).
dc and bc are good things at times. Really, I do a whole lot of really complicated manipulation of data with the various utilities, sometimes all in one long pipeline and sometimes in multiple complicated stages. An example of that is where I'll often take a du output, use sed to convert G, K, and M to the proper amount of zeroes (or maybe there's a du option to do that automatically, I forget), awk out the 1st column and print each as "$1 +" with no returns, echo a "0" at the end, and pipe the whole thing through bc, to get a "grand total." (maybe that's a bad example, but the ([do something]; echo 0) | bc is definitely something I've done a lot over the years.
I used to have aliases to call dc to do radix conversion (like echo "2 o 1337 p" | dc to get "10100111001"). (there's also a great .sig line out there that does some kind of crazy dc stack program to print out an ascii message, that I wasted a good chunk of time figuring out on paper to understand how it worked).
Another great trick I've been using for 20 years is dd piped through ssh, to copy a local hard drive image over the net to another machine, or vice-versa (well, okay, 20 years ago it was rsh). (like boot off a live CD, "(ssh remote cat /my/image.dd) | dd of=/my/dev" to rebuild a local drive from a remote backup.
There are lots of other things, way too many to write here. I'm sure there's a website out there somewhere.
Oh, and another great one from the days when I'd get files with untypable characters in the filename -- "ls -i" to get a files inode, then "find . -inum [inode] -exec rm {} \;" to delete that (or mv {} newname to rename it). Not sure I need that much any longer, but at the time it was VERY useful.
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.
Shell history tricks (Score:5, Informative)
There are a whole bunch of "history" tricks, to recall old commands without using the mouse.
When I started college, I studied the shell's man page until I knew them all. Some are so obscure I have forgotten them.
Generally, these involve an '!' character in some way.
Here are a few I use:
!! # run again the last command that was run
!9 # run again the command with history number 9
!v # run again the last command that started with a 'v'
!vi # run the last command that started with "vi"
!?foo? # run the last command that had the string "foo" anywhere in it
diff oldfile newfile
mv !$ !^ # same as "mv newfile oldfile"
# !$ is last arg of previous command, !^ is first arg
ls foo bar baz
rm -f !!* # same as "rm -f foo bar baz"
# !!* repeats all arguments from previous command
There are actually some baroque tricks that recall a previous command and perform a search-and-replace on it, but for anything that complicated I just recall the line and edit it. The baroque tricks would have been pretty darn cool back in the paper teletype days, though.
By the way, the Bash shell can be configured to edit command lines using vi or Emacs commands. I described how to do it in an article I wrote for Linux Journal magazine. It's the last section, "vi or Emacs Mode in the Shell".
http://www.linuxjournal.com/article/8361 [linuxjournal.com]
Oh, not exactly a history trick, but here's something I use all the time: /tmp/files /tmp/files # edit list to include just the files I want /tmp/files`
ls -1 >
vi
rm `cat
# `cmd` inserts the standard output from cmd into the command line as if you typed it
ls -1 > /tmp/files /tmp/files :%s+.*+mv & /some/directory/path/&+ /tmp/files
vi
# edit list to include just the files I want
# now run this command:
# save file and quit vi
source
This moves the chosen files to "/some/directory/path". The breakdown of the vi command is as so:
: # invoke "ex mode" for search and replace command
% # run the following command on every line of the file
s # do a search and replace
+ # use a '+' for the command delimiter, so I won't have to backslash escape '/' chars in the path
.* # all characters on the line /some/directory/path/& # replace "foo" with "mv foo /some/directory/path/foo"
+ # end the match pattern, begin replace pattern
# & refers to the match pattern, thus all chars on the line
mv &
Takes less time to do it than to explain it!
The above is perhaps overkill if all the files are going to the same place. It's great if you want to send some files one place, some to another, because you can just edit the destinations until it looks right.
steveha
SSH (Score:4, Informative)
I'd say most people don't know how to use SSH very well...
Stop typing passwords for every system: ssh-keygen, ssh-auth and ssh-add.
Transferring files both with scp/sftp and ssh user@host "cat file" > file, and the like.
Changing encryption algo for significantly improved speed, eg. -c arcfour
Enabling/disabling compression for internet/intranet. -C
An $HOME/.ssh/config file to map names to IP addresses, specify the default user names for each host, toggle compression per host, enable/disable port forwarding, keepalive, etc.:
host webserver
ForwardX11 no
ForwardAgent yes
Compression yes
hostname slashdot.org
port 2100
user cmdrtaco
And parenthesis and backticks seem to be going out of fashion in short order... Too bad, since they're quite time-saving: mkdir `date +%Y`
I love controlling the power and security... (Score:3, Funny)
Time warp (Score:5, Informative)
Pipe dream! (Score:3, Interesting)
Using pipes is one of the most useful things I've found.
In my signature, a simple HEADER checker:
Testing your microphone input:
Harassing someone by sending your mic input to their soundcard output:
Back up something over the network to a file.
Decompress something that you don't have the space to do on either server or computer, or just don't want to waste time.
A few non-pipe related tips:
Wine prefixes - a method to create unique Wine directories to separate local 'wine' setups.
If you ever have a game crash, you may have noticed that X doesn't automatically switch back to your default resolution, this can be done with:
Hope this was useful to someone.
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.