Unix, Linux & the command line

A few things

Shells

C Shell syntax

Other useful commands

Some examples:

Execute the line containing "frodo" in file "~/.xsession":

  `grep frodo ~/.xsession`

Find out what user ees1wc is up to:

  ps aux | grep ees1wc

Find out who user ees1wc is:

  finger ees1wc

This example uses the quotes to handle filenames that have spaces in them:

  foreach file (*)
    chmod +x "$file"
  end

Why will this one sometimes not work?:

  foreach file (*)
    chmod +x $file
  end

Take a line of space-separated words, sort them and list them on a single line:

  cat - | sed 's/ /\n/g' | sort | fmt

Compile, & run *if* compilation is successful:

  make prog && prog -a 1 -b 3

Do the same on another machine:

  rsh cvplws79 "cd `pawd` && make prog && prog -a 1 -b 3"

Compile another directory in subshell, returning to current directory:

  (cd ../dir2 && make)

Find all the pdf files in this directory and its children, and create postscript versions in the corresponding directory:

  foreach f ( `find .  -name "*.pdf"` )
    pdf2ps $f $f:r.ps
  end

Find all the subdirectories recursively in the current directory and add group write permission, and list them:

  find . -type d -exec chmod g+w {} \; -print

Create an alias to remove "core" files from the current directory tree:

  alias  decore   'find . -name core -exec \rm {} \;'

Use it to remove "core" files from another tree (e.g. the one above) without changing the current directory:

  ( cd .. && decore )

List all the tasks of a given name:

  vi tmp&
  ps aux | grep " vi " | grep -v grep

Print their PIDs:

  ps aux | grep " vi " | grep -v grep | cut -c 9-14

and kill them:

  foreach p (`ps aux | grep " vi " | grep -v grep | cut -c 9-14`)
    echo $p
    kill $p
  end

NB The spaces round "vi" are important!

Multiple grep expressions:

  vi tmp&
  vi &
  ps aux | egrep ' vi | vi$' | grep -v grep 
  ps aux | egrep '\<vi\>' | grep -v grep

Listing all the /vol/vssp partitions:

  ypcat -k amd.vol | grep vssp | sort | cut -d " " -f 1 | cut -d / -f 2

Running a set of (test) programs and listing the output of only those that failed:

  foreach f ( * )
    ./$f >& /tmp/logg
    if ($status) then
      echo ">>>> $status $f <<<<"
      cat /tmp/logg
    endif
  end

Put the last example into a script file:

  #!/bin/tcsh -x
  foreach f ( * )
    ./$f >& /tmp/logg
    if ($status) then
      echo ">>>> $status $f <<<<"
      cat /tmp/logg
    endif
  end

Replace relative paths with absolute ones in selected symbolic links:

  foreach f (libopencv_*)
    if (-l $f) then
      set str = `ls -g $f | cut -d" " -f9`
      if (`echo $str | cut -b1` == "/") then
        echo "Ooops.  $str"
      else
        echo $f $PWD/$str
        \rm $f
        ln -s $PWD/$str $f
      endif
    endif
  end

Find broken links in the current directory tree:

  find . -type l -xtype l

Find broken links in the current directory tree + any trees symbolcally linked to it:

  find -L . -type l

Bill Christmas
Last modified: Mon Sep 9 11:28:13 BST 2013