Slashdot Mirror


Essential UNIX Tricks and Tools?

Chris Lesner asks: "What handy UNIX tricks/tools do you use everyday? I'm asking for stuff that amazes your friends and makes you wonder how they use UNIX w/o them. Some simple examples include: job control (with fg, bg/&, jobs, Ctrl-Z); moving login sessions between machines with Screen for vt100 and VNC for X11 and using screen and VNC to share login session b/w users for demos etc.; using find, xargs -i and echo to build command strings which after inspection can be piped back though bash e.g. `find . -type f | xargs -i{} echo "cp {} {}.bak" | bash` I'm asking b/c my source for this kind of information has dried up as my UNIX skills have matured. I'm guessing other Slashdot readers have the same problem. By the way, if you think the examples I give are lame I challenge you to better them!"

185 comments

  1. Unix Power Tools by Bouncings · · Score: 2, Informative

    All the tricks you can cram into one of those big books, and more: Unix Power Tools -- O'Reilly's best book, IMHO.

    --
    -- Ken Kinder ken@_nospam_kenkinder.com http://kenkinder.com/
  2. Stupid shell trick by Bouncings · · Score: 2, Informative
    This is pretty simple, but on one of my machines I like to have the same directory location when I log in and out. If I'm in /home/foobar/monkey/dance, and I close my ssh connection, then I open a new one, I want to start in /home/foobar/monkey/dance. Here's how:

    In my .bashrc:

    cd "`cat ~/.lwd`"
    In my .bash_logout:
    pwd > ~/.lwd
    Pretty basic, but it does the job. One problem you have to watch for: scp'd files appear in your last working directory.
    --
    -- Ken Kinder ken@_nospam_kenkinder.com http://kenkinder.com/
  3. Impressed like a gnu under a 18-wheeler by Chinese+Karma+Whore · · Score: 2, Funny

    Well, seeing that my friends don't know much about UNIX, the following usually impresses them:

    1. Connect to home router (FreeBSD) w/ PuTTy SSH client
    2. smbclient to windows share (i.e. smbclient //empire/files -U jesus)
    3. mget files to /tmp
    4. run command shell on client box
    5. pscp files from router
    6. in the meantime, as a finishing touch, perl -e $#29%% something

    To impress normal people, you don't need to type anything fancy. Just type FAST, and as soon as a command returns, pretend to contemplate for a second, and then type your next command.

    1. Re:Impressed like a gnu under a 18-wheeler by Permission+Denied · · Score: 2
      To impress normal people, you don't need to type anything fancy. Just type FAST

      Actually, if you want to impress normal people, use tab completion, but don't tell them about it and don't make it obvious :) The looks you'll get are priceless.

      Now, on the same idea, one thing I no longer can live without: I use emacs for all my C development. The default emacs setup maps "M-/" to 'dabbrev-expand' which is immensely useful. This means that if I type "some_long_variable_name" in some file somewhere, the next time I only need to type "som M-/" and it does the Right Thing - even if "some_long_variable_name" is not to be found anywhere in the current file, it will search all open buffers. Extremely useful.

    2. Re:Impressed like a gnu under a 18-wheeler by Sitaram+Iyer · · Score: 1

      A simple trick that's both day-to-day-useful and gets even systems-literate folk (including my advisor) gaping is this:

      I have two machines on my desk; one main Linux box in text console mode, and one by-the-side windows box running cygwin and sshd. I use Lynx (by preference) on the Linux box for most of my text-only web browsing. But occasionally when I want graphics, I happily hit this magic key sequence, and *poof* an Internet Explorer pops up on the windows box.

      The trick is to have a special bookmark file under lynx multi-bookmarks; when you "add-bookmark" to this file, a little daemon shellscript polls it every second, grabs the URL, and runs "ssh windowsbox IEXPLORE.EXE url". No big shakes!

  4. Bash Completion Project by Bouncings · · Score: 4, Interesting
    One of the most useful gadgets I use is the bash completion project. It's a handy-dandy tool where tab-completion does more, oh, so much more than filenames. When I do a Debian apt-get install python-, I get a list of Debian packages to install starting with python-

    There's more fun too. It completes tons of crazy stuff. I'd check it out.

    --
    -- Ken Kinder ken@_nospam_kenkinder.com http://kenkinder.com/
    1. Re:Bash Completion Project by molo · · Score: 2

      FYI, tcsh has completion built in.

      Here's a few of my interesting ones:


      complete kill 'c/-/S/' 'p/*/`ps -x | awk \{print\ \$1\}`/'

      complete scp 'c;*@;`awk \{print\ \$1\} < $HOME/.ssh/known_hosts | sed -e s/,.\*\$// | uniq`;:;'

      complete ssh 'c;*@;`awk \{print\ \$1\} < $HOME/.ssh/known_hosts | sed -e s/,.\*\$// | uniq`;' 'p;*;`awk \{print\ \$1\} < $HOME/.ssh/known_hosts | sed -e s/,.\*\$// | uniq`;'


      Only annoyance is that you can't do multiple completion sets simultaneously. Maybe I should write a patch for that.. hmm...

      --
      Using your sig line to advertise for friends is lame.
    2. Re:Bash Completion Project by Anonymous Coward · · Score: 0

      FYI, tcsh has [programmable] completion built in.

      So does bash. That was the point of the post, see?

    3. Re:Bash Completion Project by costas · · Score: 2

      Tab completion is most complete in Zsh (pun intended). Z has a lot of extremely useful options builtin, and if everything else fails, it's easy to write your own.

  5. How about by Hard_Code · · Score: 5, Informative
    find . -type f | xargs -i{} echo "cp {} {}.bak" | bash
    find . -type f -exec cp '{}' '{}'.bak ';'
    --

    It's 10 PM. Do you know if you're un-American?
    1. Re:How about by Anonymous Coward · · Score: 0

      I'm a korn guy myself and I'm fairly sure that don't work (to lazy to log in and check) but IIRC find only subs the first occurrence of {} with the matched pattern, not any others.

    2. Re:How about by JabberWokky · · Score: 2
      I like piping to sed. Like:

      ls | sed 's/.*/mv "&" "&.foo"/'

      Or you can use find . instead of ls, with the usual type selectors. This gives two nice things - flexability, plus you can keep piping those puppies through additional sed filters to get what you want, postpending | bash at the last one. I also often use rev in the pipe chain and a sed s/ without a /g to modify the tail end of the strings.

      Hey, it might not be the least verbose, but sed can be quite a nice tool for really complex matching.

      --
      Evan

      --
      "$30 for the One True Ring. $10 each additional ring!" -- JRR "Bob" Tolkien
    3. Re:How about by Zapman · · Score: 4, Informative

      The slight problem with this (not in this case though) is you have the expense of forking and killing a lot of processes (equal to the number of files +1 for the find). On the mv or cp case, you can't (easily) get around it, but if you were to do:

      find . -type f -exec chmod 644 {} \;

      vs

      find . -type f | xargs chmod 644

      you'll find that the second runs amazingly faster, since it will group a lot of the commands together into 1.

      I did 'cd /usr/bin ; time find . -type f -exec ls {} \;' and got:

      find . -type f -exec ls {} \; 0.98s user 3.28s system 88% cpu 4.831 total

      I did 'cd /usr/bin ; time find . -type f | xargs ls -1 ' and got:

      find . -type f 0.00s user 0.02s system 31% cpu 0.063 total
      xargs ls -1 0.03s user 0.04s system 74% cpu 0.094 total

      That's a BIG difference, especially when you have a LOT of files.

      (NOTE: if I did a bare 'ls' with the xargs, the output would be different due to the way xargs works (man xargs for more details). It may or may not make a difference depending on how you're using the output. If you're shleping the whole line at once, it could make a huge difference... if you're spliting the lines by $IFS or something, then it's probably alright.)

      As for my contribution, I find these 3 find | xargs commands, wraped together in a script I call "makereadable" help me a LOT (for example, if you install from source, and the permissions get borked due to forgetting to set root's umask to 022):

      find . -type d -print | xargs chmod 755
      find . -type f -perm +0100 -print | xargs chmod go+x
      find . -type f -perm +0200 -print | xargs chmod go+r

      The first makes all directories 755. 99.99% of the time, that's what you want... just don't do it with /tmp (sticky bit will get wacked). The second finds all files that are executable by the user owner, and makes them executable to others too. The third finds what is readable to the user, and makes it readable too.

      If you want to do this with write permissions, you're probably doing something stupid you will regret later. Figuring out the command to do this is then left as an exercise for the reader. :-)

      --
      Zapman
    4. Re:How about by darkwiz · · Score: 2

      You seem to be missing the author's point. Most of the time, when you are about to do such an operation, you want to be sure that the commands to be executed are what you expect (for example, if this is going to do something destructive, such as remove files). By using the echo, you can preview the execution, check it off, and just rerun the last command with a pipe.

    5. Re:How about by dk379 · · Score: 1

      Using find ... -print | sed/awk/perl ... to generate commands rather than just using find ... exec is also better because you can preview commands list easily before executing, grep it, head it, etc.

      now, my favourite way to remove all files older than a month without getting bitten with file names containing newlines is

      find2perl /temp/dir -mtime +30 -type f -print0 | perl | perl -0 -ne unlink

      (yes, this is 3 perls working at once)

  6. chvt by NotoriousQ · · Score: 2, Insightful

    most linux newbies do not have a clue how their ctl-alt-f(x) works, and I have found this tool to be very useful on occasion.

    --
    badness 10000
    1. Re:chvt by ealar+dlanvuli · · Score: 1

      on occasion?

      I always have a minimum of 4 vt's and an X session active on my workstation, it's one of the reasons (behind perhaps 5-6 more) that I use nix over windows for daily tasks.

      --
      I live in a giant bucket.
    2. Re:chvt by TaxSlave · · Score: 1

      I always have a minimum of 4 vt's and an X session active on my workstation, it's one of the reasons (behind perhaps 5-6 more) that I use nix over windows for daily tasks.

      No kidding. Multiple text logins as well as the graphical display REALLY blows the minds of many of my winblows buddies. I think it's even MORE useful than my favorite useful tool, Opera's "open link in background."

    3. Re:chvt by Anonymous Coward · · Score: 0

      Really? Why is that better that just multiple workspaces with a bunch of xterms.

      Personally, I'd rather have 4 xterms that 4 vt's because I can switch between them a lot faster (due to the monitor resync when you switch between X and console)

      But whatever! To each their own! That's the real beauty of UN*X.

    4. Re:chvt by NotoriousQ · · Score: 1

      well the only reason for "on occasion" is that for four vt's the ctl-alt-fx is usually enough, it is just when you need more than the first 10 vts, I use chvt

      and it is also useful for scripting

      --
      badness 10000
    5. Re:chvt by ealar+dlanvuli · · Score: 1

      I personally find muti-window gui sessions ackward, but thats probably just me.

      Also I have a LCD so changing is only the time of loading the apropriate page from memory.

      --
      I live in a giant bucket.
  7. learn your find by molo · · Score: 1, Redundant

    `find . -type f | xargs -i{} echo "cp {} {}.bak" | bash`

    This is fugly and a waste.

    find . -type f -exec cp "{}" "{}.bak" \;

    --
    Using your sig line to advertise for friends is lame.
    1. Re:learn your find by autocracy · · Score: 2

      Yeah, and? It may not be the best example of using that with the `find` command, but dammit - it gets the point across!

      --
      SIG: HUP
    2. Re:learn your find by Anonymous Coward · · Score: 0

      In this particular case you are (nearly) right. HOWVER, there are times when xargs is a better choice, usually because it's faster. Take for example:

      find . -exec 'grep -l foo {}'

      versus

      find . -print | xargs grep -l foo

      The second will run MUCH faster. Why? Because xargs will shoot it a large list of files to grep, rather than one at a time. This means maybe a hand ful of grep invocation as opposed to 1 for every file. The speed difference (in my experience) is often orders of magnitude.

      In fact, xargs can be faster in the example provided by the poster as it can parallelize! (or at least GNU xargs can. Don't know about standard POSIX xargs or XPG4 xargs or the like.) RTFM for more details.

  8. Redundant bash call by Taral · · Score: 1

    Just use find . -type f | xargs -ixx cp xx xx.bak

    --
    Taral

    WARN_(accel)("msg null; should hang here to be win compatible\n");
    -- WINE source code

    1. Re:Redundant bash call by Anonymous Coward · · Score: 0

      still redundant btw:

      find . -type f -exec cp {} {}.bak \;

  9. One of my all time favorites by Froze · · Score: 3, Funny

    su root cd /; rm -rf *

    This one works really nice when your getting ready to do a fresh install, since it allows to vent all that pent up frustation with your now obsolete system :-)

    --
    -- The morphemes of your disquisition are ascertainable, but they have eschewed an ambit of transpicuous exposition.
    1. Re:One of my all time favorites by jcoy42 · · Score: 1
      su root cd /; rm -rf *

      I don't have the patience for that anymore:

      dd if=/dev/zero of=/dev/hda bs=512 count=1

      saves a lot of time. Works especially good on a box running any flavor of windows (use a *nix boot disk) :)
      --
      Never trust an atom. They make up everything.
    2. Re:One of my all time favorites by Anonymous Coward · · Score: 0

      Just verifiy that all your filesystems are mounted before executing the command.

  10. dusort by RGRistroph · · Score: 1

    In tcsh:

    alias dusort 'du -s \!* | sort -r -n | awk '"'"'{sum+=$1;printf("%9d %9d %s\n",sum,$1,$2)}'"'

    In bash (or zsh):

    function dusort ()
    {
    du -s "$@" | sort -r -n |
    awk '{sum+=$1;printf("%9d %9d %s\n",sum,$1,$2)}' ;
    }

    An example way to invoke it is "dusort /home/*", to see who the disk pigs are.

    1. Re:dusort by Phexro · · Score: 5, Interesting
      But why bother with the awk, when you can just do:

      $ du -s /path/to/wherever/* | sort -rn | head -10


      to get the top ten hogs in /path/to/wherever?

      Also, sometimes there are some big files, and you are only interested in the directories full of crap:

      $ du -s `find /path/to/wherever/* -type d` | sort -rn | head -10


      While we're on the subject, you can use this handy-dandy snippet to find the disk usage of one user in any part of the filesystem:

      $ find /path -type f -user jsmith -exec ls -l {} \; 2>/dev/null \
      > | awk '{ sum += $5} END { print sum }'
    2. Re:dusort by ealar+dlanvuli · · Score: 1

      what does 2>/dev/null actually do?

      I can't find it in a man page anywhere.

      --
      I live in a giant bucket.
    3. Re:dusort by metallidrone · · Score: 1

      2>/dev/null redirects error messages from the command (and anything else written to stderr, which is file descriptor 2) to the null device. It keeps error messages from showing up. When > is used alone, it implicitly redirects standard output, which is file descriptor 1 (so it's the same as 1>something).

      More interesting (and often obscure to new users) ways to redirect streams can be found here.

  11. "watch" command by Anonymous Coward · · Score: 2, Informative

    A lot of people don't know about the "watch" command

    watch -n1 df -h

    I always use hdparm to speed up IDE drives too.

    hdparm -c1 -d1 -A1 -a16 -m16 -u1 -S240 -W1 -k1 -K1 -X66 /dev/hda

    Of course there's aliases you can set to reduce keystrokes.

    alias ll='ls -l'

    pgrep and killall can be pretty useful.

    killall java
    renice 19 `pgrep java`

    I also have a button on my GNOME panel that runs xkill so I can click on a locked up GUI app and terminate it.

    If you have a multiprocessor box, you can do "make -j3" to run parallel builds.

    And updatedb and locate are really useful for finding files quickly.

    If you're trying to remember a command you've run before you can run this to find it quickly.

    history | grep 'command'

    They you can run !850 to run command #850 from your history for example.

    And you can run this to find GNOME packages.

    rpm -qa | grep gnome

    That's all I can think of for now. Other posters are welcome to point out faster ways to do any of the tips I've given. :p

    1. Re:"watch" command by Permission+Denied · · Score: 3, Informative
      If you're trying to remember a command you've run before you can run this to find it quickly.
      history | grep 'command'

      If you're using bash or zsh with the default emacs-like bindings, you can use ctrl-r to do an interactive backwards search in your history. Just keep hitting ctrl-r to search deeper back into the history.

      Also, the one thing I've noted a lot of intermediate Linux users don't know about, but find very useful upon investigation is job control. Even with good window management and multiple xterms, job control is very useful: I often have three or four jobs on each xterm. Also, a neat trick: with zsh or bash, "fg %-" goes back to the second-to-last job. You can use this to quickly suspend and switch between two jobs continuously (eg, after you've done it once, "ctrl-z, ctrl-p, enter" switches to the "other" job).

      Also, if there's a long command I recently executed, I usually won't search through history, but rather use the "!cmd" thing. Eg, if you've recently run "gcc -g -Wall -O3 -blahblah ..." a few commands ago, you can do it again by just typing "!gc" without searching. I guess it's really a question of preference, but I picked this up from the older gurus who were weaned on the monstrosity which is the C shell.

    2. Re:"watch" command by msouth · · Score: 2

      Eg, if you've recently
      run"gcc -g -Wall -O3 -blahblah ..." a few commands ago, you can do it again by just typing "!gc" without searching.

      It's safer, though more tedious, to do

      !gc:p
      and then, if it spits out what you wanted, do an up-arrow, enter to execute. Sometimes you forget that you did a "make clean" between now and the last time you did "make", for example.

      I wish a shell would pick up the vim history completion (or, I wish I knew which shell had already implemented it or how to access it or whatever). I want to be able to do

      !scp[up arrow] and have it look back through my history only at the stuff that starts with scp.

      --
      Liberty uber alles.
    3. Re:"watch" command by tzanger · · Score: 2

      A lot of people don't know about the "watch" command

      Indeed. I always did while true ; do clear ; date ; sleep 3 ; done when I needed to keep an eye on something.

      I never knew about the history command, although I use ^R in bash a whole lot.

      du -h --max-depth 1 /home | sort -rn is always a favourite of mine; add a | head -10 to grab just the top 10 (or whatever you prefer).

    4. Re:"watch" command by Anonymous Coward · · Score: 1

      If you're using bash or zsh with the default emacs-like bindings, you can use ctrl-r to do an interactive backwards search in your history. Just keep hitting ctrl-r to search deeper back into the history.

      And, after you've found it with ctrl-r, ctrl-o will execute the command and get the next command ready on the command line for you. Great for those "vi" "make" "./run" "gdb" "vi" ... sequences....

    5. Re:"watch" command by Chacham · · Score: 1

      make -j3

      Why isn't -j2 good enough for a two processor box?

      Thanx about watch and pgrep. Especially watch. I can't believed I never knew about that one.

      hdparm just scares me. I know it shouldn't. But after suffering an hd crash I don't want to mess with a working drive. Though should I ever add a new drive, I'll have to think about playing with it.

    6. Re:"watch" command by Permission+Denied · · Score: 2
      after you've found it with ctrl-r, ctrl-o will execute the command and get the next command ready on the command line for you.

      That is immensely cool. I've only been using it for five minutes, but I already can't live without it. You learn something every day.

    7. Re:"watch" command by Permission+Denied · · Score: 2
      watch -n1 df -h

      That reminds me of this trvial script I wrote once:

      fileprogress() {
      tput clear
      if [ $# -gt 1 ] ; then
      TOTAL=$2
      FMT='"\r%.2fM (%.2f%%)"'
      else
      TOTAL=1
      FMT='"\r%.2fM"'
      fi
      while true ; do
      ls -l $1 | awk "{printf($FMT, \$5/(1024*1024), (\$5 / $TOTAL)*100)}"
      sleep 1
      done
      }

      Then, when you're downloading some ISO, you can do "fileprogress some-image.iso" or "fileprogress some-image.iso 681574400" if you know the ISO is supposed to be 681574400 bytes when downloaded.

      I wrote that before I knew about "watch" and before scp clients had progress bars, so it was useful at the time, but I haven't used it in a while. Another cute thing is to define a function:

      beep() {
      echo -e '\007'
      xrefresh -solid white 2> /dev/null
      xkbbell 2> /dev/null
      }
      And then have it do that when the file is done downloading. Or do "/run/some/long/command ; beep" to let you know when your command is done.
    8. Re:"watch" command by phraktyl · · Score: 3, Informative

      If you do a set -o vi in bash or ksh you get the vi mode. Then you can hit <Esc>/^scp<Enter> to get the last command that started with `scp', and then hit n for next, as in vi. You can also do most of the rest of your vi commands.

      --
      Karma: Marginal (mostly due to the border around the website)
    9. Re:"watch" command by toast0 · · Score: 2

      with j3 it makes sure the box is (nearly) always has a compiler session for each cpu, it would probably be reasonable to run j2 for a single processor box as well...

  12. some stupid shell tricks by Permission+Denied · · Score: 5, Insightful
    I use zsh on all my non-Linux machines and just use the default bash on all my Linux boxes. First of all, I have one common .zshrc and .bashrc which all my machines get. The last line sources .zsh-local or .bash-local, where I keep all my machine-specific settings. This way, when I think of something clever to put in .zshrc, I can distribute it to all my machines at once.

    Here are some stupid shell tricks I've found moderately useful:

    1. bash: HISTCONTROL=ignoreboth, zsh: setopt HIST_IGNORE_SPACE; This means when you type a command with a space in front of it, it doesn't go in your history. Useful if you do something that you don't want others to see (eg, xv ~/.pr0n/*.jpg).
    2. The zsh FAQ has an entry which describes how to get your xterm title to describe all sorts of fun stuff. Here's a bash equivalent:
      # see the document /xc/doc/hardcopy/xterm/ctlseqs.PS.gz
      # on any XFree86 mirror; the only other cool thing you
      # can do is change the font with "ESC ] 5 0 ; fontname BEL"
      xtitle() {
      echo -ne "\033]2;$*\007" > /dev/tty
      }
      if [ "$TERM" = "xterm" -o "$TERM" = "xterm-color" ] ; then
      PROMPT_COMMAND="echo -ne '\017'"
      fi
      if [ "$TERM" = "xterm" \
      -o "$TERM" = "xterm-color" \
      -o "$TERM" = "rxvt" \
      -o "$TERM" = "cygwin" ] ; then
      settitle() {
      xtitle "`date +'%H:%M:%S'` - ${HOSTESS}: $PWD"
      }
      PROMPT_COMMAND="settitle ; $PROMPT_COMMAND"
      fi
    3. PATH management:
      pathdel() {
      PATH=`echo $PATH | sed -e "s/\(.*\)\(\:\)$1\(\:\)\(.*\)/\1:\4/g" \
      -e "s/^$1://" -e "s/:$1\$//"`
      }
      pathadd() {
      if echo $PATH | fgrep "$1" > /dev/null 2>&1 ; then
      :
      else
      if [ -d $1 ] ; then
      case $2 in
      "append") PATH=$PATH:$1 ;;
      "prepend") PATH=$1:$PATH ;;
      *) echo fixme ;;
      esac
      fi
      fi
      }

      pathdel '\.'

      if [ "$UID" -eq "0" ] ; then
      pathadd /sbin "append"
      pathadd /usr/sbin "append"
      pathadd /usr/local/sbin "append"
      for i in /usr/local/*/sbin ; do pathadd $i "append" ; done
      for i in /opt/*/sbin ; do pathadd $i "append" ; done
      fi
    4. This next one is absolutely necessary on any Linux machine:

      alias kilmoz='killall -9 netscape ; rm -f ~/.netscape/lock'
    5. Recursively do something for each subdirectory:
      dirs() {
      # follows symlinks, prints dot-dirs
      ls -A $@ | while read i ; do [ -d $i ] && echo $i ; done
      }
      recurse() { # DFS
      dirs | while read DIR ; do
      $@
      cd $DIR
      recurse $@
      cd ..
      done
      }
    Here's a dumb shell script that I call "randsort" - it prints out lines from stdin in a random order:
    #!/bin/sh
    randsort() {
    perl -e 'srand(time() ^ ($$ + ($$ << 15)));
    print sort {rand 10 <=> rand 10} <STDIN>;'
    }
    randsort

    This is useful 'cause you can do something like this:

    xv `ls -t ~/.pr0n/*.jpg | head -200 | randsort`

    and this randomization, for some reason, "feels" better than xv's -random option. It also has a number of "legitimate" uses. For instance:

    playm3u() {
    randsort < $1 | while read i ; do mpg123 "$i" ; done
    # heh - 'mpg123 -z --list "$i"' does same thing nowadays
    }

    Anyway, I got lots of stupid little crap like this. I try not to get too far into the customization stuff, as it's definitely a timesink and it's very important to be able to use a "standard" unix setup. For example, my .emacs is circa 80 KB (I'm big into Lisp), but I'm extremely proficient with vi, because there are lots of situations where emacs is not available, or vi is better-suited to the job.

    The lameness filter is such a piece of horseshit. I mean, are the editors deliberately trying to prevent us from discussing code and technical matters? You'll need to re-indent all the snippets above because of this.

    Lameness filter fodder:

    C este un limbaj de programare cu scop general ale carui caracteristici sint economia de expresie, structuri moderne de control al fluxului si de date, precum si un set bogat de operatori. C nu este un limbaj de nivel "foarte inalt", nici "mare", si nu este specializat vreunei arii particulare de aplicatii. Dar absenta in restrictii si generalitatea sa il fac mai convenabil si mai de efect pentru mai multe scopuri decit limbaje presupuse mai puternice. C a fost la inceput proiectat si implementat pe sistemul de operare UNIX pe DEC PDP11 de catre Dennis Ritchie. Sistemul de operare, compilatorul C si in mod esential, toate programele de aplicatii ale lui UNIX (inclusiv software-ul folosit pentru a pregati cartea aceasta) sint scrise in C. Compilatoare de C exista deasemenea si pe mai multe alte calculatoare, intre care IBM System/370 Honeywell 6000 si Interdata 8/32. C nu este legat de nici un hardware sau calculator anumit si e simplu de scris programe care se pot executa fara nici o modificare pe diferite calculatoare care au limbajul C implementat. Aceasta carte are drept scop sa-l ajute pe cititor sa invete sa programeze in C. Ea contine o initiere, pentru ca noii utilizatori sa poata incepe cit mai repede posibil, capitole separate pentru fiecare caracteristica majora, si un manual de referinta. Marea parte a textului nu se bazeaza atit pe expunerea de reguli si propozitii cit pe citirea, scrierea si revizuirea de exemple. In cea mai mare parte exemplele sint programe reale si sint complete si nu fragmente izolate. Toate exemplele au fost testate direct din text, care este intr-o forma citibila pe calculator. Pe linga faptul ca am aratat cum se utilizeaza efectiv limbajul, am incercat in plus, acolo unde era posibil, sa-l ilustram cu algoritmi utili si cu principii de bun stil in programare si proiectare sanatoasa. Aceasta carte nu este un manual introductiv de programare. Ea presupune anumite familiaritati cu conceptele. de baza din programare, ca variabile, instructiuni de asignare, bucle, functii. Cu toate acestea, un programator novice va fi in stare sa citeasca cartea si sa-si insuseasca limbajul, chiar daca ajutorul unui coleg cu experienta mai mare i-ar usura munca foarte mult. In experienta noastra, C s-a dovedit un limbaj placut, expresiv si adaptabil pentru o mare varietate de programe. Este usor de invatat si "se poarta bine" pe masura ce experienta in programare cu el creste. Speram ca aceasta carte va va ajuta sa-l folositi bine.

    C este un limbaj de programare cu scop general ale carui caracteristici sint economia de expresie, structuri moderne de control al fluxului si de date, precum si un set bogat de operatori. C nu este un limbaj de nivel "foarte inalt", nici "mare", si nu este specializat vreunei arii particulare de aplicatii. Dar absenta in restrictii si generalitatea sa il fac mai convenabil si mai de efect pentru mai multe scopuri decit limbaje presupuse mai puternice. C a fost la inceput proiectat si implementat pe sistemul de operare UNIX pe DEC PDP11 de catre Dennis Ritchie. Sistemul de operare, compilatorul C si in mod esential, toate programele de aplicatii ale lui UNIX (inclusiv software-ul folosit pentru a pregati cartea aceasta) sint scrise in C. Compilatoare de C exista deasemenea si pe mai multe alte calculatoare, intre care IBM System/370 Honeywell 6000 si Interdata 8/32. C nu este legat de nici un hardware sau calculator anumit si e simplu de scris programe care se pot executa fara nici o modificare pe diferite calculatoare care au limbajul C implementat. Aceasta carte are drept scop sa-l ajute pe cititor sa invete sa programeze in C. Ea contine o initiere, pentru ca noii utilizatori sa poata incepe cit mai repede posibil, capitole separate pentru fiecare caracteristica majora, si un manual de referinta. Marea parte a textului nu se bazeaza atit pe expunerea de reguli si propozitii cit pe citirea, scrierea si revizuirea de exemple. In cea mai mare parte exemplele sint programe reale si sint complete si nu fragmente izolate. Toate exemplele au fost testate direct din text, care este intr-o forma citibila pe calculator. Pe linga faptul ca am aratat cum se utilizeaza efectiv limbajul, am incercat in plus, acolo unde era posibil, sa-l ilustram cu algoritmi utili si cu principii de bun stil in programare si proiectare sanatoasa. Aceasta carte nu este un manual introductiv de programare. Ea presupune anumite familiaritati cu conceptele. de baza din programare, ca variabile, instructiuni de asignare, bucle, functii. Cu toate acestea, un programator novice va fi in stare sa citeasca cartea si sa-si insuseasca limbajul, chiar daca ajutorul unui coleg cu experienta mai mare i-ar usura munca foarte mult. In experienta noastra, C s-a dovedit un limbaj placut, expresiv si adaptabil pentru o mare varietate de programe. Este usor de invatat si "se poarta bine" pe masura ce experienta in programare cu el creste. Speram ca aceasta carte va va ajuta sa-l folositi bine.

    1. Re:some stupid shell tricks by ForceOfWill · · Score: 1

      4.This next one is absolutely necessary on any Linux machine:
      alias kilmoz='killall -9 netscape ; rm -f ~/.netscape/lock'

      heh, I have this in a key macro:

      killall -9 netscape-communicator
      rm -f .netscape/lock
      rm -rf .netscape/cache

      So all I have to do is get a shell and hit F11 on my spiffy gateway anykey :).
      --

      --
      Seeing is believing; You wouldn't have seen it if you didn't believe it.
    2. Re:some stupid shell tricks by bozoman42 · · Score: 1

      Except that "dirs" is the bash internal for showing your current directory stack (cf. pushd/popd). FYI.

    3. Re:some stupid shell tricks by Anonymous Coward · · Score: 0
      By the way for folks who use or admin tcsh do note that there is often a system wide cshrc file (on Solaris this may be in /etc/.cshrc depending on how stupid your admin is) that does something awful like:
      alias cd 'chdir \!*;prmpt'
      But if you use tcsh then remove that alias (unalias if you have to) and simply set:
      set prompt='%m:%/%# '
    4. Re:some stupid shell tricks by Permission+Denied · · Score: 2
      But if you use tcsh then remove that alias (unalias if you have to)

      I've encoutered too many stupid aliases set by administrators or venders/distributors.

      I always use 'unalias -a' as one of the first lines of my .{ba|tc}shrc and "unhash -am '*'" in my .zshrc. Definitely helps.

  13. Most important unix trick by dh003i · · Score: 2

    I'm sure everyone knows about this...except newcomers to UNIX.

    Its called nohup. Nohup is a command that will prevent your job from being terminated once you log out of your account. Always leaving those "please do not log out of this account" message to your co-workers? Well, that's all fine unless your co-workers are assholes. Also, even if they're not, you might want to do them a favor by doing this.

    What you do is type (wihout the quotes) "nohup " where is the command or program you want not to be interrupted upon logout. You can type in normal syntax.

    Another essential "command" if your at the command line and are going to start somethin which takes awhile is (w/o quotes) " but newcomers won't know them, nor will people who just download cygwin to use on their Windows OS, because some scientific applications require Linux.

    1. Re:Most important unix trick by Anonymous Coward · · Score: 0

      going to start somethin which takes awhile is (w/o quotes) " but newcomers won't know them

      Is that command some big secrect? :)

    2. Re:Most important unix trick by TaxSlave · · Score: 1

      Its called nohup.

      The big question I have is this. Why is it that, when I run "nohup python myscript.py &" and logout of my *bsd based webserver, my python scripts crap out?

      It's gonna be hard to get my program to generate updated html pages to match constantly updated data, if I can't get the dadburned script to stay running.

      This is an educational thread for me. My personal guru got a travelling job. (Hi Dave!)

    3. Re:Most important unix trick by dh003i · · Score: 2

      Sorry, I can't much help you there, since I don't use Python, Perl, or any other scripting languages aside from sed (alot) and awk (rarely).

      But here's my conjecture for starters. Sorry if this is typical, but its the first thing that comes to mind and the easiest to fix if wrong: (1) Either your version of nohup has a bug or is corrupted; (2) or your version of Python has a bug or is corrupted. I suggest you try "reinstalling" nohup and Python. If that doesn't work, check the BUG history on these things and try using a different version of each.

      I know, this is the typical help you get when you call your computer's support service:

      YOU: One specific feature of this program isn't working.

      THEM: Well, when did it start happening and what was the last thing you did before it started happening?

      YOU: I haven't changed anything, it just started happening.

      ...then he takes you through a lot of pointless bullshit which has nothing to do with solving your problem, but makes it seem like he's doing his job.

      THEM: Ok, sir, after performing 1000 useless tests, I've determined that your program is corrupted. Since re-installing it didn't fix it, I'm going to have to request that you delete your entire hard drive and reinstall everything.

      YOU: What? You want me to wipe my freakin' hard drive?

      THEM: Yes, sir, that appears to be the solution.

      YOU: Well fuck that. I could've "fixed" the problem that way.

    4. Re:Most important unix trick by toast0 · · Score: 3, Informative

      your python script may crap out due to the stdin stream disappearing (i know nohup redirects stdout and stderr, but i don't think it redirects stdin)

      you might try nohup python myscript.py /dev/null &

    5. Re:Most important unix trick by TaxSlave · · Score: 1

      you might try nohup python myscript.py /dev/null &

      Thanks. Except for the redirect that was left out, that was exactly what I needed. If I had mod points to spend (and could in this thread) I'd heap some on ya.

    6. Re:Most important unix trick by TaxSlave · · Score: 1

      I suggest you try "reinstalling" nohup and Python. If that doesn't work, check the BUG history on these things and try using a different version of each.

      Well, I have no control over all that crap. This is on my webserver, and the tech support for my webhost (who will go unnamed) isn't really all that helpful, except for fixing things I've broken. When I need a hint on how to do something, I have around 25% success rate in getting help. I've just about given up on getting slashcode running again on my service, because of the hose job VCPAN (Virtual Server CPAN wrapper) manages to do every time I try to install Bundle::Slash. They're not incompetent, but I wish they'd act like I was for a day.

      I've done tech support. When I did it, the goal was to help the customer get use of the hardware/software/service that they paid for. How times have changed.

    7. Re:Most important unix trick by toast0 · · Score: 2

      oops... the redirect was _supposed_ to be there, but i forgot to preview and/or use the html equivilent rather than just typing it in

    8. Re:Most important unix trick by Rob+Malda+on · · Score: 0

      hm

      You might ttry:
      nohup python myscript.py >python.out &
      and afterwards check the log file for errors

  14. poor man's ftp by PaulBu · · Score: 1

    one of the first 'tricks' I saw a more experienced
    guy to perform in good old days when I was a
    newbie... ;-)

    How he transfered a text file from one of his
    accounts (in Russia) to another one (in SUNY SB)
    when the source side did not allow ftp
    connections, telnet only?

    Well, 'more foo' in one xterm, 'cat > foo' in
    another, then click left-drag-move-click middle...

    Vas'ka, you might be reading this... HI! :)

    Paul B.

    1. Re:poor man's ftp by tchuladdiass · · Score: 2, Informative

      An easier way (and not needing xterms) would have been to use script:
      $ script
      Script started, output file is typescript
      $ telnet remotehost
      ...
      remotehost $ cat filename
      ...
      remotehost $ exit
      $ exit
      Script done, output file is typescript
      $
      Of course, instead of cat, use uuencode for a binary file...

    2. Re:poor man's ftp by Bert64 · · Score: 1

      sz/rz would be usefull too, assuming theyre installed on the remote host. There was a wrapper somewhere to allow sz/rz to work over ssh, and some telnet tools allowed zmodem send/recieve

      --
      http://spamdecoy.net - free throwaway anonymous email - avoid spam!
    3. Re:poor man's ftp by TheLink · · Score: 2

      For that sort of thing with binary files there's also uuencode, zmodem/xmodem/ymodem etc. Or also MIME encoding.

      Cheerio,
      Link.

      --
    4. Re:poor man's ftp by vinayski · · Score: 1

      One can do lots of interesting stuff using Expect.
      http://expect.nist.gov
      You good old friend can automate this cut-copy-paste-process-Of-ftpOverTelnet just by using Expect. Check out the website for the script which can do that. Basically Expectc allows you to control interactive programs using patterns in an expect script. What this means is that you could control two telnet sessions and exchange information without any user interaction.

  15. The pipe and the shell by Pauly · · Score: 3, Insightful

    I believe nothing is more impressive than knowing what you're doing with the world of UNIX command line tools and standard in/out on the pipe.
    I often show up Perl studs at work, creating similar functionality to their spaghetti code with a few lines of korn shell code in much less time.
    Shell programming with the pipe is the most powerful form of elegant simplicity I've ever seen in any computing.

    1. Re:The pipe and the shell by andy@petdance.com · · Score: 2

      Agreed on the value of pipelines, but if there's a radical difference in Korn vs. Perl code, then your Perl studs aren't so studly. They're prob'ly not using all of Perl's features.

    2. Re:The pipe and the shell by GeorgeH · · Score: 2

      Your Perl studs should read up on the -e, -n, -a and -p switches. I suspect they're not as studly as they think.

      --
      Why can't I moderate something "Wrong" or at least "Grossly Misinformed"?
  16. Not quite Unix... by Anonymous Coward · · Score: 0
    OK, so this is not actually a Unix thing, but it is very easy to impress people - esp. Windows-only people - by telling and showing them that your text editor also makes a great Mail/Newsreader, MP3-Player, Webbrowser, File Manager and Psychotherapist. Just the basic stuff every Emacs-user should know will suffice, but some Elisp wizardry surely will make every Windowsuser either hate Notepad or you, or both.



    This works with half Unix/Emacs-savy people as well, btw. It surely didn't harm my career to show my boss the eshell when he cursed cmd.exe and explorer.exe for being the moronic tools they are ;)



    If all else fails, people will at least be impressed that you can memorize all those funky keystrokes. That works with vi as well, of course.

  17. command line tricks by caca_phony · · Score: 2, Interesting

    Here is a trick for my favorite shell, es the Extensible Shell, a derivitive of plan9's rc shell. The es shell has the most logical set of syntax rules I have ever seen in a shell. It is also the only shell where I have figured out how to do automated file renaming (very handy). Here is a transcript of a session as an example:

    ;; touch 123
    ;; touch 124
    ;; touch 125
    ;; touch 126
    ;; touch 127
    ;; touch 128
    ;; touch 129
    ;; ls
    123 124 125 126 127 128 129
    ;; echo <={~~ (*) 1*}
    23 24 25 26 27 28 29
    ;; for (i = <={~~ (*) 1*}) {mv 1$i one$i}
    ;; ls
    one23 one24 one25 one26 one27 one28 one29

    this functionality is great for preventing name collisions when consolidating files from two directories into a single directory.

    as some explanation <={...} is like typing the return value of the command inside the braces, ~~
    returns the part of the second aruments matches that were expanded from the the first argument's glob.

    --
    ...and this lie crawls out of its mouth: 'I, the state, am the people.'
    1. Re:command line tricks by Permission+Denied · · Score: 2
      for (i = <={~~ (*) 1*}) {mv 1$i one$i}

      You do realize how sick that looks? :)

      How about just using any shell and having a shell function:

      rename() {
      # snarfed from Larry Wall
      perl -e '
      $op = shift;
      for (@ARGV) {
      $was = $_;
      eval $op;
      die $@ if $@;
      rename($was,$_)
      unless $was eq $_;
      }' $@
      }
      Then you could do your example as follows:

      rename 's/^1/one/' 1*

      Now that I think of it (programatic file processing), in college, I was a TA for a web programming course. One cute trick that impressed students was automatically generating thumbnails for images, like this:

      for i in *.jpg ; do convert -size 100x100 $i tn_$i ; done

      This impressed the hell out of the Windows and MacOS users, especially as I had never done that before and I came up with it after about thirty seconds (needed to look at the ImageMagick "convert" manpage). In Windows, if you have a thousand images and you want to create thumbnails, you buy a program to do that type of thing. Or, you hire a unix guy to do it for you :)

    2. Re:command line tricks by toast0 · · Score: 2

      actually microsoft's power toys for XP includes a mass resizer (the power toys are usually their most useful software)

    3. Re:command line tricks by 1010011010 · · Score: 2

      In other words, while buying XP, you also bought a special tool that resizes lots of images.

      It's nice that MSFT provided that program, but it's not in the same league as the flexibility provided by the unix command line.

      --
      Napster-to-go says "Fill and refill your compatible MP3 player", which is a lie. It's not MP3. It's WMA with DRM.
    4. Re:command line tricks by .smoke · · Score: 1

      that looks disgusting! :)

      in bash:

      for i in 1*; do mv $i one${i##1}; done

    5. Re:command line tricks by BJH · · Score: 1

      You know how long I've been looking for something that can do that? ;)

      Until now, I'd been doing things like:

      $ for i in *.txt;do mv $i `echo $i|sed 's/txt/htm/'`;done

    6. Re:command line tricks by scotch · · Score: 2
      % rename 1 one 1*

      % man rename

      For more sophisticated renaming, I use a small perl script similar to another reply.

      --
      XML causes global warming.
    7. Re:command line tricks by Anonymous Coward · · Score: 0

      Ya, those powertools are pretty nice. The original version of the powertools for XP had a pretty nice feature in it. It was removed from Microsofts website and a 'new' version has been put up instead. The feature was it let a Windows XP Home machine logon to a domain with the option that said, "Automatically logon to domain at startup". It now will just autolog you on locally, but not to any domains.

      http://www.afreego.com/Categories/Utilities/Syst em / 04120.php

  18. Eh... by jo42 · · Score: 2, Funny

    Does "FORMAT C:" count?

    1. Re:Eh... by Anonymous Coward · · Score: 0

      No.

    2. Re:Eh... by Anonymous Coward · · Score: 0

      Yes, killing a DOS/Windows partition is definatly a good trick. If only more people knew about that one... :)

    3. Re:Eh... by Anonymous Coward · · Score: 0
      now do that in (l)unix.
      mkfs
      ...shit what is the the device name.

      fall back method: use the installation cd.
  19. Other tools.. by stevey · · Score: 2

    I agree with everything that's already been said about knowing the value of pipe's and the standard tools.

    In addition to screen which has already been mentioned the two things that I've noticed impressing other people recently are, splitvt, and Emacs

    Emacs is impressive because in the hands of an expert you can do almost anything - and splitvt is a stunning program which will turn one shell into two - I highly recommend you check it out.

  20. Real shell timesaver... by stevey · · Score: 4, Informative

    The biggest single command which saves me time is 'cd -' which changes to your previous directory under bash.

    It doesn't sound terribly useful, but it is... Take my word for it.

    1. Re:Real shell timesaver... by Anonymous Coward · · Score: 0

      Wow, you must love pushd and popd then...

    2. Re:Real shell timesaver... by /cypher · · Score: 1

      Similarly, try pushd and popd. You can build up a stack of directories, which is especially intuitive for those of use used to stack programming... e.g. owners of HP calculators.

      --
      :-| have a day
    3. Re:Real shell timesaver... by hazyshadeofwinter · · Score: 1

      Wee! I'm never typing "cd $oldpwd" again!

      (catches self going esc-:wq. urk.)

      --
      Click here if you just like to click on shit.
    4. Re:Real shell timesaver... by bobdown2001 · · Score: 2, Funny

      But you simply can't forget about 'cd +' which takes you to the directory you're about to change to ;0)

      --
      Why do today what you can put off until tomorrow?
    5. Re:Real shell timesaver... by Tower · · Score: 1

      This is a real timesaver - especially when you find it useful to bounce between a couple of different levels of the same code, or a code dir and a doc dir, or testcase and functional code dirs (all without having too many shells open).

      --
      "It's tough to be bilingual when you get hit in the head."
  21. X mouse following by nelsonal · · Score: 1

    I wasn't as impressed when I first saw mouse pointer activation, but after using it for a while I get almost as annoyed when windows don't auto activate on mouse overs, as when I grab a non reverse polish calculator.

    --
    Degaussing scares the bad magnetism out of the monitor and fills it with good karma.
    1. Re:X mouse following by Yottabyte84 · · Score: 2

      What really annoys me is using a Windows box that doesn't auto copy-on-select and paste-on-middle-click.

    2. Re:X mouse following by toast0 · · Score: 2

      win95 had Xmouse activation via a powertoy from microsoft, and newer versions of windows have it via a registry setting (or tweakui)

    3. Re:X mouse following by Anonymous Coward · · Score: 0

      Yea, I was usingig it for a while, but there were some problems. Some windows programs do not expect such behavior and can get anoying.

    4. Re:X mouse following by Mr+Z · · Score: 1

      Can you also turn off autoraise? Move-to-focus is worthless for me if autoraise is on.

      --Joe
    5. Re:X mouse following by toast0 · · Score: 2

      yes, at least on xp, i never used x-mouse until recently, so i don't know if you can turn it off on older versions of windows (sorry)

    6. Re:X mouse following by Mr+Z · · Score: 1

      I remember trying a version of TweakUI on Win95 way back when (early 96). I seem to recall it had a disable mechanism, but it had the downside that it was permanent. (That is, it noted in the Registry somewhere that TweakUI had been disabled, and there was no option to re-enable anywhere.) I had thought there was a no-autoraise feature, but my vague recollection isn't very useful, obviously. That *was* 6 years ago.

      I think I'm going to have to get TweakUI for my windows machines at work and see how it's improved. If I can do no-autoraise (and turn it off for programs that get confused by it), I'll be a happy camper.

      --Joe
    7. Re:X mouse following by spongman · · Score: 2

      how do you replace the selection with the contents of the clipboard?

    8. Re:X mouse following by Yottabyte84 · · Score: 2

      That can be a problem... KDE has a thing that lets you select from previous clipboard contents which you'd have to use.

    9. Re:X mouse following by iangoldby · · Score: 1

      KDE3/Qt3 change things slightly. You can now use ctrl-C and ctrl-V to copy and paste independently of the current selection. The original behaviour using mouse buttons still works too.

  22. Fix for scp problem by matthewg · · Score: 2

    I believe that if you put the cd in .bash_profile instead of .bashrc, it will only be executed for login sessions - that is, for ssh, not scp.

  23. best grep trick ever by novarese · · Score: 5, Informative

    Instead of
    $ ps aux | grep foo | grep -v grep
    use
    $ ps aux | grep [f]oo
    The brackets will show up in the ps output but don't match your pattern, so your grep is automaticly excluded from your final output.

    1. Re:best grep trick ever by ealar+dlanvuli · · Score: 1

      that didn't work in csh, should I be doing something diffrently?

      --
      I live in a giant bucket.
    2. Re:best grep trick ever by the+hopthrisC · · Score: 1

      still better: use pgrep from the procps package.

    3. Re:best grep trick ever by toast0 · · Score: 2

      pgrep doesn't give the info that ps aux does... although i suppose you could do ps aux `pgrep blah` but that takes the fun out of it

    4. Re:best grep trick ever by Anonymous Coward · · Score: 0

      You can do the same thing with awk, without using the pattern trick:

      $ ps aux |awk '$11 ~ /foo/ {print $0}'

    5. Re:best grep trick ever by Anonymous Coward · · Score: 0

      uh, yeah, except the point is to *simplify*

    6. Re:best grep trick ever by Anonymous Coward · · Score: 0

      Sorry, but relying on nonstandard packages isn't "better".

    7. Re:best grep trick ever by Anonymous Coward · · Score: 0

      yeah, use a real shell.

    8. Re:best grep trick ever by TheGratefulNet · · Score: 2

      in tcsh:

      ps aux | grep \[f\]oo

      --

      --
      "It is now safe to switch off your computer."
    9. Re:best grep trick ever by phnx90 · · Score: 0

      quote the RE:
      ps aux | grep '[f]oo'

    10. Re:best grep trick ever by ealar+dlanvuli · · Score: 1

      thanks

      --
      I live in a giant bucket.
  24. Regular Expressions by sam+the+lurker · · Score: 3, Interesting

    Regular expressions aren't so much either a trick or a tool exactly, but you can use them with all the "good" tools.

    Get the book "Mastering Regular Expressions," by Jeffrey E. F. Friedl. http://www.oreilly.com/catalog/regex2/

    Read it slowly, a couple of pages every day. I didn't understand much of what he was trying to say until I read the book the second time.

    But why make up my own clever things to say... From http://www.oreilly.com/catalog/regex/desc.html "There can be certain subtle, but valuable, ways to think when you're using regular expressions, and these can be taught."

    I find that books that teach you how to think about problems and solutions are few and far between, and books that do it well are almost impossible to find. This books is one of those.

    Once learned regular expressions are one of those things that can profoundly effect the way you work. And once your there "you wonder how they [other people] use UNIX w/o them".

    1. Re:Regular Expressions by Permission+Denied · · Score: 4, Interesting
      Here's a cute little perl script that's useful for developping other perl scripts:
      #!/usr/bin/env perl
      $st = "\033[7m";
      $en = "\033[m";

      print "Enter regex: ";
      $re = <>;
      chomp $re;
      while () {
      print "Enter string: ";
      $s = <>;
      chomp $s;
      exit unless $s;
      $s =~ s/$re/$st$&$en/g;
      print $s . "\n";
      }
      This will highlight what parts of a string match a given regular expression. (I modelled this script on something I found in a Ruby tutorial).

      Also, for those learning regular expressions, I would highly recommend Introduction to Automota Theory, Languages and Computation by Hopcroft et al. This is a more mathematical/theoretical introduction to the subject. Regular expressions/automata are a very nice part of CS in that the basic theory is immediately applicable.

    2. Re:Regular Expressions by faeryman · · Score: 1

      i will second you recommendation. "Mastering Regular Expressions" is an excellent book.

      --


      ,
      faeryman
  25. That find command by dozer · · Score: 1

    ...wouldn't this be better in every way?

    find . -type f -exec cp -r {} {}.bak \;

    Find is your friend!

    1. Re:That find command by polymath69 · · Score: 1
      ..wouldn't this be better in every way?

      find . -type f -exec cp -r {} {}.bak \;

      Maybe; but it won't work with any version of find I've ever encountered, since {} is only expanded when it stands alone, with whitespace on both sides.

      And the -r seems out of place, too, since you've already selected objects of type file with your -type f directive. How are you going to recurse a file?

      --

      --
      I don't want to rule the world... I just want to be in charge of mayonnaise.
  26. sort | uniq -c | sort -rn by coyote-san · · Score: 3, Informative

    I consider this pretty basic, but I remember some junior coworkers blown away by

    <tt>something | sort | uniq -c | sort -rn</tt>

    On the coding level, I constantly use the regex library (with extended regular expressions) and profiling. There are a lot of places where a single well-designed RE can eliminate many lines of code, and profiling can help you ensure that you close all files you open, free all memory you malloc, etc.

    --
    For every complex problem there is an answer that is clear, simple, and wrong. -- H L Mencken
  27. debugging by Chacham · · Score: 2, Informative

    Two biggies for me are 2>a to send errors to a file named "a". And strace, when you really need to know what a prgram is doing.

    I couldn't live without strace. I had a font problem with X, strace told me where the problem was. xine wasn't playing sound, strace showed be that it was hitting the wrong entry in /dev/. The list goes on.

    For Debian users, apt-spy (requires installation of the package) to find fast sites for package downloads, and apt-listchanges to list what all the changes are.

    To wow silly Windows users, eject and aafire (part of aalib).

  28. Using w instead of finger by Anonymous Coward · · Score: 0

    I'm by no means an expert (I only know the basic commands), but one command I like to use is w.

    finger tells you who's on the system from what location. w, on the other hand, tells you who's on, how long the system has been up (without a reboot), what everyone else is doing, how long they've been idle, etc.

  29. My favorites by Deagol · · Score: 2
    These are not groundbreaking, but I do use them a lot.

    Locate files with a string in them. For example:

    find /etc -type f -exec grep -l hdparm {} \;

    Also, using tar across pipes to copy a tree without an intermediate tar file:

    tar cf - foo | (cd /bar ; tar xvf -)

    I also use it over ssh in cron jobs:

    tar cf - foo | ssh root@host "cd /bar ; tar xvf -"

    1. Re:My favorites by toast0 · · Score: 2

      why not use scp to copy the stuff instead of tar?

    2. Re:My favorites by Anonymous Coward · · Score: 0

      GNU grep has had a '-r' (recursive) for quite some time. So you can replace this:

      find /etc -type f -exec grep -l hdparm {} \;

      with this:

      cd /etc; grep -r hdparm *

      But you probably won't because as we all know, once you're used to doing something a certain way, there's really no reason to change...

    3. Re:My favorites by Anonymous Coward · · Score: 0

      That should be 'grep -rl' of course. My apologies.

    4. Re:My favorites by Anonymous Coward · · Score: 0

      What's wrong with this? (at least on platforms that offer the recursive flag)

      grep -rl hdparm /etc

    5. Re:My favorites by Phexro · · Score: 2

      Try:

      `tar cf - foo | ssh bar tar -C daz -xvf -'

      Which instructs tar to change into `daz' before performing any operations and eliminates the cd command.

    6. Re:My favorites by phnx90 · · Score: 0

      >Also, using tar across pipes to copy a tree without an intermediate tar file:

      >tar cf - foo | (cd /bar ; tar xvf -)

      Am i missing something, why not simply do cp -R foo /bar/

    7. Re:My favorites by Zapman · · Score: 2

      Deagol said:
      tar cf - foo | ssh root@host "cd /bar ; tar xvf -"

      Toast0 said:
      why not use scp to copy the stuff instead of tar?

      Tar's faster (for large scale directories). NOTE: This is on solaris that I did the following.

      We have 1 website with a BLOODY STUPID design. Part of it is that 1 filesystem has 13 MILLION files in it. Some of the directories have 10 or 15 thousand files in them. The idiots should have used a proper database. This is one of the few times under solaris that I wished for reiserFS. Anyway I was tasked to copy this fs over to another box (website migration).

      First time, tried rdist. 8 hours later, and about 20% complete, we tried something else (For grins, I did the same command later... rdist ended up taking 28 hours in total).

      Second time, rcp. 28 hours again.

      Third time, rsync. Took less time (about 10 hours).

      Fourth time, tar over rsh. Took 4 hours.

      Shlepping filesystems across machines with ssh and tar is really, really nice and quite fast. I highly recommend it.

      --
      Zapman
    8. Re:My favorites by toast0 · · Score: 2

      ahh, i see

      that is a good reason to use tar. I'll have to try to remember that.

      thanks

    9. Re:My favorites by BJH · · Score: 1

      What's wrong with that is, it won't work with Solaris's b0rked grep :(

    10. Re:My favorites by Darby · · Score: 2

      `tar cf - foo | ssh bar tar -C daz -xvf -'

      I've seen people do stuff like this with ssh a few times and I always wonder are you implying a -p password, have no password set, or does it just ask for a password (which would make it a no go for automated scripts).

    11. Re:My favorites by pne · · Score: 2

      find /etc -type f -exec grep -l hdparm {} \;

      I usually use something like this:

      find /etc -type f -print | xargs grep -l hdparm /dev/null

      Besides switching from the -exec option of find to using xargs (which means that you start grep not once per file, but once per bunch of files), this also uses the neat little trick of adding "/dev/null" to the list of files for grep to check. This isn't so important for grep -l, but if you want to print the matches that were found, then remember that grep will prefix each match with the filename if there are more than two filenames, but will show only the matching lines if there is only one filename. The presence of /dev/null ensures that there will always be at least two filenames in the grep command line -- so you'll always get the filename prepended to matching lines.

      --
      Esli epei etot cumprenan, shris soa Sfaha.
    12. Re:My favorites by Baz+Quux · · Score: 1

      A better answer is having an ssh identity key set up with no passphrase. When it comes to automated tasks, especially for privileged accounts, one can use the ~/.ssh/authorized_keys (or ~/.ssh/authorized_keys2 for ssh2) to lock down what hosts can or cannot use the key, what commands can be run, and enable or disable other ssh features as needed. That way, a passphrase-less key for running file transfers doesn't have to lead to an interactive shell.

      If I have to, say, automate an rsync of something from a remote box, I set up a new identity key just for that task, and use the -i flag in the local ssh command to specify use of that key. The remote box's authorized_keys file is setup to only allow my one local box to use that key, to only run that one rsync command, with only the arguments specified, and disables pty allocation, port forwarding, X11 forwarding, and agent forwarding. The authorized_keys entry looks like this (all on one line):

      from="my.workstation.bar",command="/usr/bin/rsyn c --server -vlogDtprx . /var/backups",no-pty,no-port-forwarding,no-X11-f orwarding,no-agent-forwarding 1024 35 1234567890[...the actual key...] backups to home

      man sshd and check out the authorized_keys section.

    13. Re:My favorites by Darby · · Score: 1

      Sweet. Thanks.

    14. Re:My favorites by Zule_Boy · · Score: 1

      I usually use something like this:

      find /etc -type f -print | xargs grep -l hdparm /dev/null


      man find(1), er, that is...

      find will take an arg -name where I could say:
      find /etc -type f -name hdparm -print
      and not use the unnecessary pipe/command. The -name arg will even do regex for ya.

    15. Re:My favorites by pne · · Score: 2

      Two different things. My command gives me a list of files under /etc containing the word "hdparm"; yours gives me a list of files under /etc called "hdparm". (Note that I used xargs grep and not plain grep -- just find ... | grep ... would indeed produce more or less what you describe -- though it would also find files called bahdparma, so more like -name '*hdparm*'.)

      (And nitpicking, -name doesn't do "regexes"; it uses globbing syntax (e.g. "*" is not "zero or more of preceding atom" but "zero or more of any character"; "." matches dot exactly, not "any character except newline", etc.).)

      --
      Esli epei etot cumprenan, shris soa Sfaha.
  30. Use && (was Re:My favorites) by Anonymous Coward · · Score: 0

    > tar cf - foo | ssh root@host "cd /bar ; tar xvf -"

    Use && to avoid dumping your crap in root if /bar
    does not exist:

    tar cf - foo | ssh root@host "cd /bar && tar xvf -"

  31. tcsh too by RevDigger · · Score: 1

    Hey, that works in tcsh too. Keen.

    - H

  32. xtail is essential by RevDigger · · Score: 2, Informative

    One of the first things I install on a new system is always xtail.

    It has nothing to do with X Windows.

    It works just like 'tail -f' but it can watch multiple files. You can even watch whole directories and it will notice when new files are created.

    http://www.unicom.com/sw/xtail/

    Very handy tool.

  33. CTAGS/scree/ssh/VIM by yod@ · · Score: 1



    Ctags and tab completion in VI as well as vsplit and CVS from VI .. these things make any programmers life easier.

    having a good bashrc/vimrc/muttrc will save you soo damned much time and typing.

    ssh keys and ssh-agent .. key auth is another time/productivity.

    Screen which has been mentioned is also somthing i cannot live without.

    also perl from the command line.

    one i use a lot:

    perl -i.bak -lpe 's///g;'

    replace whatever in your regex in whatever files
    backing up original files to file.bak

    --
    Sorry man I don't controll the aliens.
  34. A pretty handy command... by Anonymous Coward · · Score: 0


    For all non-hidden files in the present working directory: back the file up, then replace all
    instances of PATTERN with REPLACEMENT:

    $ perl -wpi.bak -e 's/PATTERN/REPLACEMENT/g' *

  35. Crazy stuff by devphil · · Score: 2


    I got tired of looking up all of the various -f* and -m* options in gcc. So the Bash completion project now knows how to complete gcc/g++ options, e.g.,

    gcc -fomit-fr<TAB>
    will complete, and
    gcc -f<TAB><TAB>
    will list possible completions, etc.

    Other shells have had programmable completion for a while now. It's nice to see this feature added to bash, and it's nice to see someone volunteering to collect the widespread completion functions.

    --
    You cannot apply a technological solution to a sociological problem. (Edwards' Law)
  36. or the obvious by 0x20 · · Score: 1

    for foo in *; do cp $foo `basename $foo .bak`; done

  37. One tool, two basic tips... by Pathwalker · · Score: 2
    First the tool - xtail . It's wonderful for watching a bunch of system/web logs on one terminal window.

    Next, two basic tips:
    1. control - \ sends SIGQUIT rather than SIGINT as control - c does. Useful for killing programs that do something besides exit when they get SIGINT (such as xtail).
    2. kill -ILL Simulates an illegal instruction - useful for killing tasks that ignore kill -KILL. I had to use this all the time to kill hung opnet and comnet simulations back in my networking class several years ago.


    One last thing, to address a major peeve I have with many scripts I find:

    Always use random names for temp files. Even if you don't want to use mktemp, please do something as simple as appending .$$ to the end of the file names. While this may not prevent someone trying to force a race condition, think of what would happen if two copies of your script were started at the same time if you didn't ensure that different instances of your script are using different temp files...
  38. Here's the useful one by devphil · · Score: 3, Informative


    After hacking around on *nix for years, everything in that book was old news to me. Well, almost everything. Out of 1000+ pages, here's the only trick I had never seen before:

    If you have a directory full of files that are precious to you, run "touch ./-i" in that directory. This creates an empty file named "-i". Why do this? Because:

    If you accidentally "rm *" there, the shell's default collating order while expanding the * will put the "-i" file first in the list, which will then be interpreted by rm as the interactive option, rather than a file.

    (I've never used this trick, but I can see how it would be much less hassle than trying to remember to chmod u-w a precious file every time you get done working with it. Refinements on the technique include hardlinking the "-i" file to different directories instead of touch'ing it more than once, to save on inodes.)

    (This also assumes that the user is already familiar with any of the half-billion methods of actually deleting a file whose name starts with '-'. This is /the/ most frequently asked FAQ in comp.unix.*.)

    There. Now you know the most interesting trick in the whole book, and you don't need to spend the sixty bucks. :-)

    --
    You cannot apply a technological solution to a sociological problem. (Edwards' Law)
  39. .tar.bz2 by hazyshadeofwinter · · Score: 1
    A simple thing, this, but I've seen lotsa install instructions recommend this:
    bunzip foo.tar.bz2
    tar -xvf foo.tar

    when you can just do this:
    bzcat foo.tar.bz2 | tar xv
    --
    Click here if you just like to click on shit.
    1. Re:.tar.bz2 by T-Punkt · · Score: 1

      Or:
      tar xpvzf foo.tar.bz2

      (I personally prefer using the p flag as well...)

    2. Re:.tar.bz2 by FreeForm+Response · · Score: 1

      tar xpvzf foo.tar.bz2

      This will fail. Use j instead of z to handle instead to handle bz2 files.

      Why is it j? Who knows? :)

    3. Re:.tar.bz2 by T-Punkt · · Score: 1
      This will fail. Use j instead of z to handle instead to handle bz2 files.

      Not on my system.

      % file mozilla-source-1.0rc3.tar.bz2
      mozilla-source-1.0r c3.tar.bz2: bzip2 compressed data, block size = 900k
      % tar xpvzf mozilla-source-1.0rc3.tar.bz2

      mozilla/
      mozilla/CVS/
      mozilla/CVS/Root
      mozill a/CVS/Repository
      mozilla/CVS/Entries
      mozilla/CVS /Tag
      mozilla/README/
      mozilla/README/CVS/
      [...]
      Why is it j? Who knows? :)

      Well, no idea why your tar uses such weird options. Let me guess - is your system some kind of GNU/linux? (Then you have the answer ;-)

    4. Re:.tar.bz2 by BJH · · Score: 1

      On mine, tar takes the -y flag to expand .bz2 files...

    5. Re:.tar.bz2 by delorean · · Score: 1
      because so many systems are different.

      That won't work under the ubernix (Solaris in my opinion).

      But, this will work on nearly all:
      bunzip file|tar xfvp -

      Or for gzip:
      gunzip file -c|tar xfvp -

      Gotta have that dash. Gotta have dash-c with gunzip.

      --
      "You may all go to hell and I will go to Texas"
      Sen. Davy Crocket to US Congress, Nov. 1, 1835
  40. Simple timesaver by ShaunC · · Score: 2
    One of my favorite shortcuts in bash/csh is !$ which is expanded to the last argument you typed on the previous command line. If you often issue multiple commands on the same argument, e.g. to manipulate the same file several different ways, this can save you a ton of keystrokes.

    I also find myself using this shortcut when I'm tracking down spam:
    $ nslookup bestmortgageratesintheworld.com
    $ whois !$
    $ traceroute !$
    Beats typing the argument over and over - especially when it's long - and it's faster than hitting the up-arrow and editing the previous command. Hope someone finds this useful, I've already pulled a few great tips out of this thread myself.

    Shaun
    --
    Thanks to the War on Drugs, it's easier to buy meth than it is to buy cold medicine!
  41. I'm surprised by Neillparatzo · · Score: 2, Funny

    I'm surprised no one's yet mentioned the command to "read mail, real fast".

    1. Re:I'm surprised by Mr+Z · · Score: 2, Funny

      Or, to read mail from relatives and friends you met on Slashdot:

      rm -rf /.

      Works best if you're root.

      --Joe
  42. good scripting by chegosaurus · · Score: 1

    My tips for becoming a good scripter, which I think is *essential* for a good Unix admin. (BTW my shell of choice is always Korn, pref. ksh93)

    Remember that shell is a proper programming language. I use loops and stuff on the command line all the time - not just in scripts.

    Don't use awk when you can use cut etc. etc. You will learn a lot about scripting by trying to be efficient.

    Learn how to use sed and vi *properly*. If you use vim, vim.sourceforge.net has loads of useful tips.

    Learn Korn shell's ${!}, ${#}, ${x:y:z} type constructs.

    Don't depend too much on OS (especially Linux) specific commands. It can make you lazy and you can look like a klutz when you have to use a different Unix.

    Sensible use of functions can simplify large shell scripts enormously.

    Don't be afraid of creating and using new file descriptors - they can be invaluable.

    Become a regex guru.

    On the command line, remembering that $_ is the final argument of the last command saves a ton of typing.

    To get yesterday's date use TZ=GMT+24 date (assuming you're on GMT)

    lsof is an incredibly useful tool - learn it and carry a copy around with you.

    For showboating, nothing impresses the uninitiated like proficient use of a line editor. ;-)

    A function I often use in scripts which produce a lot of output is this;

    underline() {
    echo $* | sed -e 'p;s/./=/g'
    }

    which easily produces nice, clear underlined headings.

    A neat but not particularly useful Korn shell trick, (don't know if it works in bash) is

    cat < /dev/tcp/127.0.0.1/xx

    which lets you read output from port x on the local host.

    Other stupid tricks are things like using echo * instead of ls, and all the "funnies" like drink bottle; opener.

    1. Re:good scripting by BJH · · Score: 1

      Be careful there - under csh, $! is the last parameter of the previous command, and $_ is the current shell, I think...

    2. Re:good scripting by chegosaurus · · Score: 1

      You're right, of course. That's why I pointed out at the top of my post that I use ksh. ;-)

    3. Re:good scripting by BJH · · Score: 1

      Yeah, I realized that ;)
      All I wanted to point out was that it could have different effects under different shells.

    4. Re:good scripting by Anonymous Coward · · Score: 0

      cat /dev/tcp/127.0.0.1/xx? That's really cool. What system are you running on? I don't know of a *nix that supports that syntax, nor of a ksh version that implements it internally. I would like to find one, or even documentation on one.

  43. No text editor by ptaff · · Score: 1
    One day my buddy had to recreate a lost passwd file, with no editor... was pretty proud to solve it with echo:
    echo "root:x:0:0:root:/bin/bash" > /etc/passwd
    One time I had to delete a whole tree which had become infested with (zillions?) of files. rm -r * complained about "too many arguments"
    for i in `ls -1`; do rm $i; done
    solved it.

    1. Re:No text editor by Zapman · · Score: 2

      Good solution in a pinch, but that's what xargs was built for.

      Say ls would give

      aaa aab aac aad aae aaf ...... cqd cqe .... zzz

      find . -print | xargs rm

      would make a bunch of rm commands, exactly as long as would be permitted (either by line length, or argument numbers). So, instead of 1 rm per file, you have 1 rm per files. Much more effecient.

      --
      Zapman
    2. Re:No text editor by t · · Score: 2

      Or even easier "rm -r borkeddir; mkdir borkeddir;" why bother with special tricks? This is also the easiest way to get rid of wackily named files.

    3. Re:No text editor by odyrithm · · Score: 0

      also you can use a redirect i.e.

      cat >file

      control+c

      which I find much better for knocking out multi line text when I dont have an editor.

      --
      moo
    4. Re:No text editor by ejasons · · Score: 1

      Some user suggested using find . -print | xargs rm

      I see a lot of tips here that use "find" with "xargs". However, most of these (including the quoted one), won't handle filenames with spaces properly. I know that most Unix people don't have filename with spaces in them, but you don't want your careful "cleanup" script to remove the wrong file because some Macintosh user has (maliciously or not) saved such a file.

      Anyway, the proper way, on a modern unix, is to use:

      find . -print0 | xargs -0 rm

  44. pushd, popd by Tom7 · · Score: 2

    Pushd and popd made my day when I learned about them. I now never use unix without 'em!

  45. Unix Guru's Univers by GeorgeH · · Score: 3, Informative

    UGU offers a Unix tip of the day at http://www.ugu.com/sui/ugu/show?tip.today - that should keep you in tips for a while.

    --
    Why can't I moderate something "Wrong" or at least "Grossly Misinformed"?
  46. replace string in multiple files by worf_mo · · Score: 1
    I use this to replace a string in multiple files:

    for f in `find /my/resumes -name "*.txt" `; do mv $f $f.bak; sed 's/beer/water/g' $f.bak >$f; rm -f $f.bak; done

  47. Archiving script by rycamor · · Score: 1

    Here is a little script I use to create directory snapshots. I used Perl, but it would be easy enough to do it an any shell scripting environment:

    #!/usr/bin/perl -w
    $dir_in=shift(@ARGV);
    chomp($dir_in);
    $dir_in =~ tr/\///d;
    print "Archiving $dir_in/* ...\n";
    use POSIX qw(strftime);
    $now_string = strftime "%m-%d-%Y_%H%M", localtime;
    $tarname=$dir_in."_".$now_string.".tar.gz";
    $execstring="tar -zpscf ".$tarname." ".$dir_in."/*";
    print($execstring);
    system($execstring);
    print("\n");


    What this does is a create a standard Gzipped tarfile of a directory tree, preserving attributes, etc... and naming this archived file with the date and time appended to the directory name. Let's say this script is saved in /usr/local/bin as "myarch". I can be browsing anywhere, such as my /home/rycamor directory, and I can just enter "myarch mail" to archive the /home/rycamor/mail directory, saving it as /home/rycamor/mail_06-02-2002_1615.tar.gz (the command it executes is simply "tar -zpscf mail_06-02-2002_1615.tar.gz mail/*")

    For any directory where you are doing something questionable, and you might want to create several backups as you go, this is a nice quick way to keep track of changes.

  48. Re: make -j3 by pknut · · Score: 1

    A while back I downloaded the latest CVS version of gaim (20020402) and decided to have a play around to see which maximum number of jobs gave the best compile times. My box is a dual celeron 550 with a modest 128MB of RAM. Results are as follows:

    time make -j1
    real 4m6.294s
    user 3m27.700s
    sys 0m48.820s

    time make -j2
    real 2m50.786s
    user 3m36.000e
    sys 0m49.190s

    time make -j3
    real 2m46.357s
    user 3m30.230s
    sys 0m49.410s

    time make -j4
    real 2m45.729s
    user 3m31.810s
    sys 0m50.790s

    Switching to two or more maximum jobs gave a huge difference in compile times. Above -j2, there wasn't too much difference, with -j3 giving the (marginally) fastest compile times. Overall, multiple jobs are your friend if you hate waiting around longer that you have to for make jobs to finish.

  49. strace == truss on Solaris by Mr+Z · · Score: 1

    Incidentally, if you like strace on Linux, the truss command under Solaris is very similar.

    For really complex problems, be sure to look into the -o and -f flags.

    --Joe
  50. two "words". by Wakko+Warner · · Score: 2

    ssh -R.

    - A.P.

    --
    "Remember when the U.S. had a drug problem, and then we declared a War On Drugs, and now you can't buy drugs anymore?"
  51. What I can think of right now: by crucini · · Score: 3, Interesting
    1. Tcpflow - read contents of tcp traffic in real time. Great for watching browser/webserver interactions.
    2. Netcat - connect Unix pipes to TCP sockets. Should have been part of Unix since the advent of TCP/IP. Great for rigging a temporary "server" to see if a client is connecting as advertised: nc -lp 80.
    3. X Resources (as seen in ~/.Xdefaults) - you can make xterms really dark, even when running colored apps like mutt, with dark Xresources like: XTerm*color9: #690000 - man xterm for meanings of color0-15.
      xrdb -merge .darkXres to use.
    4. Xmessage - useful in crontabs to remind you of periodic things - like remembering to go home. With the right params, it can take over the whole screen, which is hard to ignore.
    5. perl -pi.bak -e's/chocolate/vanilla/g' *.recipe - change a bunch of files, leaving backups.
  52. Detect IP if not tunneled by Rob+Malda+on · · Score: 0

    What about this little script I run at login...

    if [ "$DISPLAY" != "" ] ; then
    TTY_NAME=`tty | sed -e s/'\/dev\/'//`
    IPNUM=`who | grep $TTY_NAME | awk '{ print $6 }' | sed -e s/'('// | sed -e s/')'//`
    DISPLAY=$IPNUM:0
    export DISPLAY
    fi

    It automagically detects if I'm tunneling X through ssh.

  53. Aliases are cool by famebait · · Score: 1

    I tend to use aliases quite a lot to ease any routine trasks that pops up, especially for swithing between aløternative environment variable settings.
    One really useful tool is therefore an alias to save the current alias list to an alias file. The file is sourced from my .cshrc. Use awk to insert the preceding 'alias' and any quotes you might need.

    I used to think 'cd = pushd' was really handy, but after a while of having the equivalent of a 'back' button, I started missing 'forward', so I hacked up a couple of script replacements for pushd and popd that lets me do that. Never got around to debug them properly after they did most of what I need, so I'm not posting them.

    not exactly rocket science, but:
    For some shells, 'ls = ls -F' is indispensible.
    ll = ls -l is a classic. I tend to also define
    la and lla. Guess what they do.

    --
    sudo ergo sum
  54. LinuxLookup by _randy_64 · · Score: 1

    Probably posting too late for anyone to read this, but here goes. I used to write the Tip of the Week column for LinuxLookup. It hasn't been updated since September, but there's still a year's worth of tips on interesting ways of using various Unix commands, shell tricks, and other tools. The last columns I wrote and the archive of past articles are all here.

  55. chmod, jot, grep, *, ls, less by realdpk · · Score: 3, Interesting

    To make all of your files and directory world-readable with one command:

    $ chmod -R go+rwX .

    (the X is the key, you don't have to worry about executables vs. directories)

    Need a list of numbers of letters or anything? Check out jot (on FreeBSD, maybe on others):

    $ jot 3
    1
    2
    3

    $ jot -w %c 3 65
    A
    B
    C

    (This is especially useful with for loops - "for i in `jot 20`;do touch foo.$i;done" will generate foo.1 through foo.20. Extra hint: -w %02d will give you a leading zero on 0-9)

    grep's -A, -B, and -C flags can be very useful. Using them, you can have grep display the lines immediately before and/or after a match.

    If you have a lot of files in a directory, so many that "*" complains of the argument length, bash (and probably other shells) can let you get around this by changing your command to one using a for loop. It won't be as fast, but it won't churn for several minutes only to tell you it won't work. ;)

    instead of:

    $ rm *

    do

    $ for i in *;do rm $i;done

    You can get a list of all files in your directory excluding . and .. with 'ls -A'.

    One of the most annoying things about 'less', at least in Debian and probably other dists, is that it clears the screen when you exit. Ugh! You can fix this problem by setting PAGER='less -X' in your environment.

    1. Re:chmod, jot, grep, *, ls, less by Zule_Boy · · Score: 1

      If you have a lot of files in a directory, so many that "*" complains of the argument length, bash (and probably other shells) can let you get around this by changing your command to one using a for loop. It won't be as fast, but it won't churn for several minutes only to tell you it won't work. ;)

      instead of:

      $ rm *

      do

      $ for i in *;do rm $i;done

      Normally this wont get you very far. The problem is that when your shell tries to expand a *, wether it is in rm * or a for x in * it expands too big and doesn't get everything. Remember that your program, lets say rm doesn't get '*' as ARGV1, it gets the first arg expanded from the shell, ARGV2 is the second, and so on. You have to be clever and use find/xargs.

      Cheers

  56. More sed tricks by Anonymous Coward · · Score: 0
    A few more sed tricks:

    sed '/^$/d' to delete blank lines

    sed 's/ *$//' to remove trailing spaces

    sed 1d to delete the first line of something

    Google turned up a couple sed faqs:

    http://www.dreamwvr.com/sed-info/sed-faq.html

    http://www.faqs.org/faqs/editor-faq/sed/

  57. One of my favorites by Bretski · · Score: 1

    I love 'while' loops from the command line. For instance, to monitor disk space every second:

    tcsh% while 1
    while? df -k
    while? sleep 1
    while? clear
    while? end

    Also good to monitor who's logged in with "who", processes "ps ..|grep xxx" or whatever.

  58. My bag of tricks by hebble · · Score: 1


    # Make less behave more pleasantly:
    export LESS=FMqai

    # Control behavior of all incarnations of make without command line arguments (convenient for multiprocessors):
    export MAKEFLAGS='-j 2 -l 2.5'

    # Start lynx with the freshmeat.net search for the given string:
    fm() {
    lynx "http://freshmeat.net/search/?q=${1} useful for scripting languages:
    vw() {
    vim `which $1`
    }

    #!/usr/bin/perl -w
    # Here's a (probably too-long) Perl script to manipulate my command history. It filters out all but the most-recent duplicates; this means I get to keep more of my elaborate commands for later.
    use File::Copy;
    my $origfilename = shift;
    my $infilename = "$origfilename";
    while (-e $infilename) { $infilename .= ".old" }
    move($origfilename, $infilename);
    open IN, $infilename;
    open OUT, ">$origfilename";
    my %inlines;
    my $whichline = 0;
    while () { $inlines{$_} = $whichline++; }
    foreach (sort { $inlines{$a} $inlines{$b} } keys %inlines) { print OUT; }
    close OUT;
    close IN;

    #!/usr/bin/perl
    # This one is nice; you give a regex argument and a replacement argument, and a list of files.
    # For each file in the list, rename the file to the result of the regex substitution.
    # e.g., sedmv 'NIN' 'Nine_Inch_Nails' NIN*.mp3
    use File::Copy;
    my $pat = shift;
    my $repl = shift;
    my @files = @ARGV;
    foreach my $f (@files) {
    my $newf = $f;
    $newf =~ s{$pat}{$repl}g;
    move($f, $newf) if $f ne $newf;
    }


    And of course there's my user-monitoring program.
    It alerts you to logins, logouts, idle time, 'mesg' status, and 'w'hat people are doing, as things happen, for a specified buddy list or all users, in color, with very little load on the system.

  59. Korn Shell Script to find last day of month by elvism · · Score: 1

    Here is a script I put together to find the last day of the month using 'cal' -useful for checks in a cron job for only the last day of the month.

    #!/bin/ksh
    integer count=0

    cal | while read A B C D E F G
    do
    ((count+=1))
    if ((count>3)) ; then
    if [[ -z $A ]] ; then
    break
    elif [[ -z $B ]] ; then
    lastday=$A
    break
    elif [[ -z $C ]] ; then
    lastday=$B
    break
    elif [[ -z $D ]] ; then
    lastday=$C
    break
    elif [[ -z $E ]] ; then
    lastday=$D
    break
    elif [[ -z $F ]] ; then
    lastday=$E
    break
    elif [[ -z $G ]] ; then
    lastday=$F
    break
    else
    lastday=$G
    fi
    fi
    done

    print "$lastday"
    exit 0

  60. ^R & emacs by tkwire · · Score: 1

    Ctrl-R in Bash helps a good bit. It's a reverse search just as within emacs. It searches for the last command matching what you type in.

    Speaking of, I find it very helpful that Bash recognizes a lot of emacs commands, eg Ctrl-A, etc.

    Not much, but helpful for me

  61. suspend in bash by Anonymous Coward · · Score: 0

    One of my favourites not mention yet is using "suspend" to suspend a bash session and then continue it with "fg". E.g. log in, "su" to root (creates a new shell), do something, then "suspend" the root shell going back to regular user, and then "fg" back to the root shell without typing a password. Alleviates the annoyance of typing the root password for every small chore you need done as root...

  62. searching for commands in shell history by dk379 · · Score: 1
    tcsh (which is now `csh' in FreeBSD) has default bindings ESC-p and ESC-n to search for commands
    starting with already typed prefix, i.e.


    make clean
    make depend all
    size kernel
    make c<ESC>p


    would find `make clean'
  63. stileget.sh by Anonymous Coward · · Score: 0

    Now that you've got a way of disabling bash_history, you might want to try the following script...

    #!/bin/sh
    for i in $(seq $3 $4)
    do wget $1"$i".$2
    done

    Its pretty simple. Usage: stileget filename extension start number end number.

    I won't tell you what it does, but try:

    ./stileget.sh http://static.stileproject.com/p00p/north wmv 1 9
    ./stileget.sh http://static.stileproject.com/p00p/ssaint wmv 1 5
    ./stileget.sh http://static.stileproject.com/p00p/analz wmv 1 6
    ./stileget.sh http://static.stileproject.com/p00p/tina wmv 1 4
    ./stileget.sh http://static.stileproject.com/p00p/bal wmv 1 6
    ./stileget.sh http://static.stileproject.com/p00p/bal mpg 1 6

    Good isn't it? You'll need xine or mplayer to exploit the full power of this excellent Unid shell script. You can get more filenames, extensions, start numbers and end numbers at www.stileproject.com

    1. Re:stileget.sh by Permission+Denied · · Score: 2
      You know, it's funny that you mention this, because I have something very similar (which I use for the same purpose :). Problem is, FreeBSD doesn't ship with an 'seq' (my pr0n box went from Linux to FreeBSD a couple years ago). Well, that's another trivial script (~/bin/seq). Now, another problem is that some web sites name their products using leading zeros (which I kind of like because 'xv ~/.pr0n/suzy*' works as expected instead of 'xv ~/.pr0n/suzy?.jpg ~/.pr0n/suzy??.jpg'). Well, this is unix: problem found, problem solved:

      #!/usr/bin/env perl
      # ~/bin/seq0 - like seq, but with leading zeros
      # requires both arguments
      $i = $ARGV[0];
      $j = $ARGV[1];
      $l = length($j);
      while ($i <= $j) {
      printf("%0${l}d\n", $i);
      $i++;
      }

      Just for fun, I now rewrite this in Bourne:

      #!/bin/sh
      i=$1
      j=$2
      l=`awk "{print length($j)}" < /dev/null`
      while [ $i -le $j ] ; do
      printf "%0${l}d\n" $i
      i=$((1 + $i))
      done

      If you must relate this to the original story, this is fun because with unix, over the years you keep accumulating all these trvial scripts you write. One script uses something else you wrote a year ago, which in turn uses some shell function which in turn uses some small C program you keep in ~/bin. These add up and make a cozy little environment all your own.

  64. learn your grep by delorean · · Score: 1
    Both are nice-- but how do you know what file you found that string in? Eh?


    Throw in a dev null, duh!

    find . -type f -exec grep "find this" {} /dev/null \;


    or xargs if you prefer. Anyway-- you gotta have another "file" to grep for grep to tell you what file was the text was found in.

    --
    "You may all go to hell and I will go to Texas"
    Sen. Davy Crocket to US Congress, Nov. 1, 1835
  65. Copy file hierarchies by Corporate+Gadfly · · Score: 1
    How about using tar to move file hierarchies while preserving permissions and ownerships.
    cd fromdir; tar cf - .| (cd todir; tar xfBp -)
    --
    Corporate Gadfly
    Jonathan Archer: the most beaten up Enterprise captain in Star Trek history