Meijian An's Tips on Linux/Unix

 

  1. Automatically set DISPLAY environment.
  2. Transform pdf (or ps) to any image format.
  3. Several useful alias.
  4. Replace a string in multiple files.
  5. "Back" in linux.
  6. Remove a file with special character.
  7. Mixed order when using wildcards of [a-z] or [A-Z].
  8. Get RGB values of raster image.
 

Tips

  1. Automatically set DISPLAY environment.
    (1) Bash shell in Linux (put below in .bashrc):
    if [ -z ${DISPLAY:=""} ]; then
        DISPLAY=`who am i | awk '{print $6}' |tr -d '()'`
        if [ -n "$DISPLAY" ]; then
            export DISPLAY=$DISPLAY:0.0
        else
            export DISPLAY=":0.0" # fallback
        fi
    fi
    (2) Cshell in Unix (put below in .cshrc)
    setenv DISPLAY `who am i | awk '{print $6":0.0"}' |tr -d '()'`
  2. Transform pdf (or ps) to any image format.
    Use pdftops (not pdf2ps) to transform pdf to ps. Transforming to other format, gs is necessary. Below is an example transforming pdf file to png format:

          file=input #assuming "input" is the prefix of the pdf file.
          gs -sDEVICE=ppmraw -sOutputFile=- -sNOPAUSE -r150x150 -q ${file}.pdf -c showpage -c quit | pnmcrop| pnmtopng >${file}.png

    By the same way, you can transform pdf (or ps) to any image format supported by gs. All available devices (formats) can be shown in the help of gs ("gs --help").

  3. Several useful alias.
    Below alias can be putted in ~/.bashrc or ~/.profile:

          cp='cp -i'
          rm='rm -i'
          mv='mv -i'
          l='ls -l'

  4. Replace a string in multiple files.
    If replace "oldstring" in all fortran files (*.f) by "newstring", run:

          find . -name "*.f" | xargs perl -pi -e 's/oldstring/newstring/g'

  5. "Back" in linux.
    In c shell of unix, alias "back" can be used to return the preview directory. In Linux,

          cd -

    can do it too.
  6. Remove a file with special character.
    If a file (e.g. -aa) with initial character "-", it can be deleted by:

          rm -- -aa

    For other special characters, use "\" or quotation marks ("").
  7. Mixed order when using wildcards of [a-z] or [A-Z].
    When you want to show all files which are named with a lower-case character at the beginning, you are often suggested to do as:

          ls [a-z]*

    However, sometimes it may not work. The problem is resulted by your LANG (see Weirdness with BASH wildcards). The solution is to run:

          export LC_ALL=C

  8. Get RGB values of raster image.
    Transforming a (jpg, png, etc.) image to pnm, then get ascii RGB color values in ASCII format. Below is an example to get RGB value from a png image (input.png):

          pngtopnm input.png |  pnmnoraw > input.rgb

    By the similar way, you can get RGB color values in ASCII from any raster image.