Friday, 15 March 2013

Looping/iterating through files - in Linux, Bash Command Line/Shell


if [ "$1" == "" ]; then
  echo "Syntax: $0 filenames"
  echo "(filenames can be any (positive) number of files)"
  exit 1
for file in $@
  [ -f $file ] && echo "Full Path: $file - Base Filename: $(basename $file)"
  # note: Full Path will only contain a path if the input arguments contain paths.
  # basename will always strip the path, if any
  # the [ -f $file ] bit filters out files only, so we don't process directories

No comments:

Post a Comment