Various shell commands

A collection of various shell commands that don't really fit into a specific category.

  • Write in hexadecimal using echo (useful for scripting)
    echo -ne "\xab\xcd\x01" > /tmp/somefile
  • Generate a file of a specific size
    dd if=/dev/zero of=empty.out bs=1M count=num_mb
    dd if=/dev/urandom of=random.out bs=1M count=num_mb
  • Create thumbnails for a set of images
    for i in `ls`; do convert -contrast -geometry 25%x25% $i small_$i; done
  • Wait for a specific program to exit, then run a command (script)
    #!/bin/sh
    
    PROGNAME=$1
    CMD=$2
    
    while [ `pgrep $PROGNAME | wc -l` != '0' ]; do
          echo -n '.';
          sleep 5;
    done
    
    echo
    $CMD
    
  • Use colored 'ls'-output
    On most systems I know, colored ls output is the default. However, I encountered a system that only had coloring in zsh and not in bash. Even though the LS_COLORS environmental variable was correctly set, it did not work. The easiest solution I found was to set an alias:
    alias ls='ls --color=auto'
  • Highlight search terms with grep (but only if functionality present)
    Set the following in your local profile:
    [ `grep --help | grep -c color` -gt 0 ] && GREP_OPTIONS="--color=auto"
    export GREP_OPTIONS
  • Fix problems with line-wrapping on bash
    On an older system I experienced problems with line-wrapping in bash. Long lines wrapped to the beginning of the line instead of to the next line. This can be fixed by setting the correct terminal type in the $TERM environment variable. This variable was set to 'xterm'; switching it to 'interix' worked for me.
    export TERM=interix

    IMPORTANT NOTE: this change breaks the Home, End, PageUp and PageDown key usage in vi/vim. I worked around this issue by setting aliases:

    alias vim='TERM=xterm vim'
    alias vi='TERM=xterm vi'
  • Colored login prompt
    On this same older system the bash prompt was still set to the old 'bash-3.00$'. I modified this to show
    [user@host path]$ with some simple coloring scheme by setting the following PS1 variable:
    export PS1="[\[\e[0;32m\]\u@\h \[\e[0;35m\]\W\[\e[00m\]]\\$ "
  • Temporarily increase the size of /tmp
    If your /tmp directory is not a physical partition on disk, but rather a tmpfs filesystem that utilizes RAM memory, you can dynamically increase or decrease its size by remounting it.
    e.g. to set the size to 1 GiB:
    mount -o remount,size=1G /tmp