Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
Unix Operating Systems Software Linux

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

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

(Useful) Stupid Unix Tricks?

Comments Filter:
  • Stupid Tricks (Score:2, Interesting)

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

    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.

  • by Anonymous Coward on Wednesday November 05, 2008 @04:52PM (#25649027)

    People here are so used to using some drag and drop gui that they didnt know you could ftp from a cli....

  • by hexadevil ( 1351505 ) on Wednesday November 05, 2008 @04:56PM (#25649147)
    I personally like all the little command shortcuts you can use in Bash, such as command searches using history modes (!?, Ctrl+r, etc.), command replacement using ^search^replace, last known argument using 'Alt+.'. That sort of stuff. There's tons of it out there, most of which I'm not too familiar with.
  • by cain ( 14472 ) on Wednesday November 05, 2008 @04:56PM (#25649153) Journal

    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)

  • by yttrstein ( 891553 ) on Wednesday November 05, 2008 @04:57PM (#25649171) Homepage
    Though I really have no idea why:

    find /* >> biglist
    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)

    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.

  • by khasim ( 1285 ) <brandioch.conner@gmail.com> on Wednesday November 05, 2008 @05:03PM (#25649337)

    On Windows, tabbed completion grabs the FIRST entry that matches.

    On Linux, tabbed completion lists ALL the entries that match.

  • by Anonymous Coward on Wednesday November 05, 2008 @05:07PM (#25649419)

    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)

    by multipartmixed ( 163409 ) on Wednesday November 05, 2008 @05:08PM (#25649441) Homepage

    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)

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

    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)

  • by kenholm3 ( 1400969 ) on Wednesday November 05, 2008 @05:15PM (#25649623) Homepage Journal

    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

    EXAMPLE
    % script -t 2> timingfile
    Script started, file is typescript
    % ls
    <etc, etc>
    % exit
    Script done, file is typescript
    % scriptreplay timingfile

  • Re:X-forwarding (Score:1, Interesting)

    by Anonymous Coward on Wednesday November 05, 2008 @05:15PM (#25649633)

    For those of us that are new(ish) to the Linux world, would you be willing to describe the commands/process?

  • by Anonymous Coward on Wednesday November 05, 2008 @05:18PM (#25649711)

    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)

    by Eravnrekaree ( 467752 ) on Wednesday November 05, 2008 @05:30PM (#25649983)

    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.

  • 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 gknoy ( 899301 ) <gknoy@NOsPAM.anasazisystems.com> on Wednesday November 05, 2008 @05:45PM (#25650391)

    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)

    by Anonymous Coward on Wednesday November 05, 2008 @05:48PM (#25650485)

    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)

    by mr_mischief ( 456295 ) on Wednesday November 05, 2008 @06:02PM (#25650943) Journal

    I often prefer

    du -sh *

    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.

  • by dwye ( 1127395 ) on Wednesday November 05, 2008 @06:29PM (#25651657)

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

  • by mollymoo ( 202721 ) on Wednesday November 05, 2008 @06:34PM (#25651781) Journal

    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)

    by Nathanbp ( 599369 ) on Wednesday November 05, 2008 @07:03PM (#25652467)

    :(){ :|:& };:

    :() defines a function named : with no arguments. { :|:& } is what the function does. :|: calls itself twice (with a pipe between the two), and the & at the end runs it in the background as a new process. The ; finishes off that command, then the last : runs the function, starting the fork bomb (as each run starts 2 new processes, each of which starts 2 new processes...).

  • by Anonymous Coward on Wednesday November 05, 2008 @07:20PM (#25652861)

    eject cdrom is great for locating poorly labeled machines too!

  • by dwheeler ( 321049 ) on Wednesday November 05, 2008 @07:35PM (#25653129) Homepage Journal
    If you begin your scripts with:

    #!/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 /bin, so this is VERY portable.) This also makes it easy to try out new interpreters (load a test version's binaries in ~/python-beta, add that first to that PATH, and now the test version's interpreter is used.) This does have the extra cost of starting up /bin/env first, but often that's not a big deal.

    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)

    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 Rotting ( 7243 ) on Wednesday November 05, 2008 @08:37PM (#25654051)

    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"

  • by IWannaBeAnAC ( 653701 ) on Wednesday November 05, 2008 @09:09PM (#25654477)
    I also stuck to tcsh for a very long time, even after reading that document. Then one day, I finally sat down and finally learned how to do redirections properly in bash, and how to set environment variables ('export', instead of 'setenv'), and I never looked back. Bash is so much better, I can't believe now how long it took to make the change. Aside from much better redirection syntax, other bash-isms I often use are $(command) syntax as an alternative to backticks, for i in {1..10}; do .... ; done loops, and lots of other stuff. But all of the shells have a syntax that to me seems very old fashioned and confusing. I wish there was a much more modern shell, perhaps with a python-like syntax, or even just a cleaned up bash. I never remember all the places where you need to insert spaces after reserved words in bash.
  • by sjames ( 1099 ) on Wednesday November 05, 2008 @10:08PM (#25655153) Homepage Journal

    Log in to the Sparc next to an unsuspecting suit and do "cat bark.au >/dev/dsp"

  • Re:rm -rf / (Score:2, Interesting)

    by bm_luethke ( 253362 ) <luethkeb.comcast@net> on Thursday November 06, 2008 @01:08AM (#25656823)

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

  • by Antique Geekmeister ( 740220 ) on Thursday November 06, 2008 @02:26AM (#25657363)

    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.

  • by Lodewijk ( 3307 ) on Thursday November 06, 2008 @02:42AM (#25657469) Homepage

    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)

    by Anonymous Coward on Thursday November 06, 2008 @02:52AM (#25657515)

    Thinking about it: you're searching for nothing, from nothing. What do you think you're going to find?

  • by yahyamf ( 751776 ) on Thursday November 06, 2008 @05:14AM (#25658309)
    do ssh sunworkstation eject instead. You won't show up in `who`
  • by Allen3 ( 968993 ) on Thursday November 06, 2008 @06:03AM (#25658553)
    Using tar to copy a directory tree can be fun, but for true amazement you need to combine it with a remote shell to copy an entire directory tree to a different machine without any intermediate media:

    cd dir; tar cf - . | rsh remote_machine "cd dir2; tar xvpf -"
  • Comment removed (Score:3, Interesting)

    by account_deleted ( 4530225 ) on Thursday November 06, 2008 @06:53AM (#25658815)
    Comment removed based on user account deletion
  • sudo !! (Score:3, Interesting)

    by moosesocks ( 264553 ) on Thursday November 06, 2008 @06:16PM (#25667531) Homepage

    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.

  • by ketilf ( 114215 ) on Friday November 07, 2008 @06:21AM (#25673355) Homepage

    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
    grep a ./1 ./2
    grep a ./5 ./6
    grep a ./7 ./8
    grep a ./3 ./4
    grep a ./9
    $ find . -type f -print0 | xargs -P 5 -n 2 -0 echo grep a
    grep a ./1 ./2
    grep a ./3 ./4
    grep a ./5 ./6
    grep a ./7 ./8
    grep a ./9

  • Re:cat! (Score:2, Interesting)

    by etwills ( 471396 ) <william_towle.yahoo@co@uk> on Friday November 07, 2008 @07:48AM (#25673917) Homepage Journal

    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

The one day you'd sell your soul for something, souls are a glut.

Working...