bash

Move a series of images into directories matching the image names

Snippet

I had a series of files (*.png) that I wanted moved into individual directories of the same name, sans extension.

# one line
for f in *png; do fn=$(basename $f); d="${fn%.*}"; echo $d; mkdir $d; mv $f $d; done
 
#
# Expanded with explanations
# 
 
# list of files
for f in *png; do
 
  # get the file name without the path
  fn=$(basename $f); 
 
  # get just the filename root, without the extension
  d="${fn%.*}"
 
  # output to screen so that we know something is going on
  echo $d
 
  # make the directory
  mkdir $d
 
  # move the file into the directory
  mv $f $d
 
# close the loop
done

bash notes

Snippet

Notes on bash

# length of a variable
size=${#myvar} 
 
# confirming the length
$ echo $size
# => 11
 
# use in an if statement
if [ ${#size} < 4 ]; then
  echo "mystr can't be fewer than 4 characters"
  exit
fi
 
barf=1
if [[ $barf ]] then; 
  ...
fi
 
if [[ ! $id ]]; then
  echo "doesn't exist"
else
  echo "exists. $id"
fi

redirect stderr to stdout

Snippet

setting up bash aliases

Snippet

edit the .bashrc and add these lines

alias d='dirs -v'
alias hg='history | grep $1'
alias pu=pushd
alias psg='ps -ef | grep $1'
alias purge='rm *~'
alias svndiff='svn diff --diff-cmd=`which diff` -x -w $1'
 
export HISTFILESIZE=3000
 
umask 002

bash for loop

Snippet

Loop over bash files

for file in $(ls *.mt1); do mv $file $file.txt; done;

Pages