Forgot your password?
typodupeerror
Unix Operating Systems Software Linux

(Useful) Stupid Unix Tricks? 2362

Posted by timothy
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?

This discussion has been archived. No new comments can be posted.

(Useful) Stupid Unix Tricks?

Comments Filter:
  • rm -rf / (Score:5, Funny)

    by Anonymous Coward on Wednesday November 05, 2008 @04:50PM (#25648981)

    rm -rf /

  • Well (Score:5, Funny)

    by Anonymous Coward on Wednesday November 05, 2008 @04:50PM (#25648993)

    Well.

    dd if=/dev/zero of=/dev/sda1

    • Re:Well (Score:5, Interesting)

      by FooAtWFU (699187) on Wednesday November 05, 2008 @05:00PM (#25649233) Homepage
      Not quite the same, but in a similar vein, cat /dev/urandom > /dev/dsp

      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.

      • Re:Well (Score:5, Interesting)

        by Technonotice_Dom (686940) on Wednesday November 05, 2008 @08:24PM (#25653851)

        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!

  • by FooAtWFU (699187) on Wednesday November 05, 2008 @04:50PM (#25648995) Homepage
    (used in my company for doing the agile/extreme "pair programming" think with a remote devloper, among other things).

    screen is awesome.

  • by 140Mandak262Jamuna (970587) on Wednesday November 05, 2008 @04:50PM (#25648997) Journal
    ... Generally people are surprised by the fact that you could type some strange incantations into a black window like awk grep etc and make the computer do things without touching the mouse. Yeah, some are surprised by that thing.
    • by Forty Two Tenfold (1134125) on Wednesday November 05, 2008 @05:13PM (#25649571)

      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!

      • by sydneyfong (410107) on Wednesday November 05, 2008 @06:27PM (#25651635) Homepage Journal

        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 :)

        • by Timex (11710) * <smithadmin @ g m a i l.com> on Thursday November 06, 2008 @08:45AM (#25659559) Journal

          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. :)

    • by infinite9 (319274) on Wednesday November 05, 2008 @05:42PM (#25650289)

      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

      • by Trailer Trash (60756) on Wednesday November 05, 2008 @08:08PM (#25653621) Homepage

        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.

  • grep and awk (Score:4, Insightful)

    by yakmans_dad (1144003) on Wednesday November 05, 2008 @04:52PM (#25649031)
    So much easier for me to use than perl. I presume the modern unix user prefers perl.
  • by Anonymous Coward on Wednesday November 05, 2008 @04:52PM (#25649049)

    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...

  • Tab (Score:5, Informative)

    by computersnstuff (799942) on Wednesday November 05, 2008 @04:53PM (#25649053)
    I'm sure everyone at some point is surprised of tabbed completion.
    • Re:Tab (Score:5, Funny)

      by Anonymous Coward on Wednesday November 05, 2008 @04:57PM (#25649179)
      I'm sure everyone at some point is surprised of tabbed completion.

      Woah! Got any more?



      (yes, I'm being sarcastic)
      • Re:Tab (Score:5, Insightful)

        by jcam2 (248062) on Wednesday November 05, 2008 @05:14PM (#25649585) Homepage

        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.

      • Re:Tab (Score:5, Funny)

        by jez9999 (618189) on Wednesday November 05, 2008 @05:55PM (#25650713) Homepage Journal

        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!

      • Re:Tab (Score:5, Funny)

        by Bandman (86149) <bandman AT gmail DOT com> on Wednesday November 05, 2008 @05:59PM (#25650843) Homepage

        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?"

    • Re:Tab (Score:5, Informative)

      by Craig Davison (37723) on Wednesday November 05, 2008 @05:05PM (#25649363)

      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.

    • Re:Tab (Score:5, Funny)

      by gEvil (beta) (945888) on Wednesday November 05, 2008 @05:12PM (#25649541)
      I just tried using this in Word. Instead of finishing the word I was typing, it kept on moving the little "insertion line" thing to the right. I already filed a bug report, but do any of you have a quick fix?
  • by PingXao (153057) on Wednesday November 05, 2008 @04:53PM (#25649059)

    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.

  • X-forwarding (Score:5, Informative)

    by mikeb (6025) on Wednesday November 05, 2008 @04:53PM (#25649079) Homepage

    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)

      by BigJClark (1226554) on Wednesday November 05, 2008 @04:58PM (#25649195)

      ... or even funnier, is how long (as in decades) we've been able to do that.
    • Re:X-forwarding (Score:5, Informative)

      by Chris Pimlott (16212) on Wednesday November 05, 2008 @05:30PM (#25650003)

      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.

  • Talk / DD / Mount (Score:5, Informative)

    by p14-lda (517504) on Wednesday November 05, 2008 @04:54PM (#25649105) Homepage
    People seem to be losing the ability to use all the older manual ways of doing things.

    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)

    by JustinOpinion (1246824) on Wednesday November 05, 2008 @04:57PM (#25649157)

    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)

    by MMC Monster (602931) on Wednesday November 05, 2008 @04:57PM (#25649169)

    Showers

  • Job control. (Score:5, Informative)

    by Craig Davison (37723) on Wednesday November 05, 2008 @04:59PM (#25649221)

    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

  • by James Youngman (3732) <jay@@@gnu...org> on Wednesday November 05, 2008 @05:18PM (#25649699) Homepage

    Assuming you already know the simple stuff like how to use shell quotes correctly, what you can do with ps and top, ...

    1. Using awk '$3 ~ /foo/ { bar }' to grep just one column of a file
    2. reset
    3. find . -blah -exec quux \+
    4. Adding : to the front of complex commands you just typed but realise you don't want to execute yet so that they get into your shell history
    5. Meta-T in Bash for swapping arguments
    6. find . -printf X | wc -c for counting files (since find |wc -l would miscount files with newlines in the name)
    7. set, shift and implicit shell loops (for without in)
    8. "${foo:-bar}" and similar
    9. "${x%%.ext}.newext"
    10. comm -3 <(sort long) <(sort short)
    11. unalias rm
  • by pieleric (917714) on Wednesday November 05, 2008 @05:22PM (#25649805) Homepage

    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 ;-)

  • by 44BSD (701309) on Wednesday November 05, 2008 @05:39PM (#25650217)

    echo '[q]sa[ln0=aln256%Pln256/snlbx]sb3135071790101768542287578439snlbxq'|dc

  • grep --color (Score:5, Informative)

    by krappie (172561) on Wednesday November 05, 2008 @05:39PM (#25650219)

    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)

    by pak9rabid (1011935) on Wednesday November 05, 2008 @05:44PM (#25650347)
    lsof is a LIFE SAVER for trying to find what's still using something in a mounted resource when trying to unmount something. For example:

    lsof /mnt/myMount

    That will list which processes have anything under /mnt/myMount open

    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 /dev/snd

    That will list what processes are accessing any of your ALSA sound devices.
  • Shell history tricks (Score:5, Informative)

    by steveha (103154) on Wednesday November 05, 2008 @05:53PM (#25650653) Homepage

    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:
    ls -1 > /tmp/files
    vi /tmp/files # edit list to include just the files I want
    rm `cat /tmp/files`
    # `cmd` inserts the standard output from cmd into the command line as if you typed it

    ls -1 > /tmp/files
    vi /tmp/files
    # edit list to include just the files I want
    # now run this command: :%s+.*+mv & /some/directory/path/&+
    # save file and quit vi
    source /tmp/files

    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
    + # end the match pattern, begin replace pattern
    # & refers to the match pattern, thus all chars on the line
    mv & /some/directory/path/& # replace "foo" with "mv foo /some/directory/path/foo"

    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

  • Time warp (Score:5, Informative)

    by SchmellsAngel (1020963) on Wednesday November 05, 2008 @07:41PM (#25653227)
    cal 9 1752

"Just think of a computer as hardware you can program." -- Nigel de la Tierre

Working...