Archive for the ‘bash’ Tag

[bash] format date

It happens often a script I have the needs to append a simple timestamp in the format of YYYYMMDDHHMMSS to a generated file.

It’s very easy to get it to work in pure bash, without perl script and similia. Just use the native date command with his format option. Easy as drinking a glass of water, the command is (for the format above)

$ date +”%Y%m%d%H%M%S”

for the full detail of the format arguments, refer to the man page.

A simple usage in script is

#!/bin/bash
set -e

NOW=`date +”%Y%m%d%H%M%S”`
FILE=my-cool-file-${NOW}.txt

echo $FILE

watermark for the edivad-suite

Released a new version of the edivad-suite which include a script for making a watermark on the image. As usual it works on opened image, single file or directory.

From now you can also find, browsing the SVN, a bash script I use for sequentially renaming (actually a cp) all files in a directory, starting from a provided number, and a desired file extension.

[bash] counting lines

It happened that I needed to count all lines of code in all java/jsp files of a project. What faster than bash can do this?

$ find -type f -iregex ‘.+\.j\(sp\|ava\)$’ -print0 | xargs -0 wc -l

However this wont skip the blank lines.

[Bash] Rename files sequentially and padding 2

I’ve updated my last script in order to be able to manage different file extensions provided as parameters.

[Bash] Rename files sequentially and padding

Maybe I’ve just reinvented the hot water. But I needed it and it took me less time to write it on my own than searching for the solution.

The task was to take all files in a directory and copy them into an output directory renaming them sequentially starting from a provided number. So files will be something like 0000.jpg, 0001.jpg, …, 0035.jpg, etc.

Less words as usual :)

#!/bin/bash
# copy all files in the directory to an output one renaming them
# sequentially.
#
#          GNU LESSER GENERAL PUBLIC LICENSE
#               Version 2.1, February 1999
#
# Copyright (C) 1991, 1999 Free Software Foundation, Inc.
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#
# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
#

OUT_DIR=out
PAD_LENGTH=5

if [ -z "$1" ] || [ -z "$2" ] ; then
   echo "./move.sh <number-to-start-from> <extension>"
   echo " "
   echo "./move.sh 12 JPG"
else
   # testing output directory exist. if not create it.
   if [ ! -d ${OUT_DIR} ] ; then
      mkdir ${OUT_DIR}
   fi

   COUNTER=$1
   EXT=$2

   for file in *.${EXT} ; do
      OUTFILE=$(printf "%0${PAD_LENGTH}i\n" "${COUNTER}")
      cp -v $file ${OUT_DIR}/${OUTFILE}.${EXT};
      let COUNTER++;
   done
fi

Here is the source code (pdf) for easy read/copy/paste.