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

3 of 185 comments (clear)

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

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