Showing posts with label piping. Show all posts
Showing posts with label piping. Show all posts

Sunday, March 18, 2007

Executing a command on multiple files with FIND

Okay, so apparently, the FIND command is so powerful that you can even tell it what to do with the files when it finds them. I always used to find multiple files, translate newline characters to null characters, then pass each argument to XARGS as in:
find ./ -regex ".*\.mp3" | tr \\n \\0 | xargs -0 -n 1 md5sum > mp3checksums.md5

But, you can also say:
find ./ -regex ".*\.mp3" -exec md5sum {} \; > mp3checksums.md5

Where, the {} implies to insert an individual argument into that part of the exec string, and the semicolon (escaped so it's not picked up by BASH) ends the -exec agrument.

Sunday, February 04, 2007

Shell Script to Parse Command Line Arguments

Problem: I needed a script that took just file names and translated large JPEG's into smaller or thumbnail images. The problem was the convert command, part of ImageMagick, needs the specific output file name.

Solution: The following script.
#!/bin/bash

if [ ! -d Thumbs ] ; then
mkdir Thumbs;
fi

while [ $# -ge 1 ]; do
TARGET=`echo -n "$1" | sed "s/.jpg/t.png/" | sed "s/.JPG/T.PNG/" | sed "s/.jpeg/t.png/" | sed "s/.JPEG/T.PNG/"`;
if [ -e $TARGET ] ; then
echo "Refusing to overwrite $TARGET. ";
else
echo -n "Converting $1 to Thumbs/$TARGET: ";
convert $1 -resize 128x128 Thumbs/$TARGET;

if [ $? -eq 0 ]
then
echo "OK."
else
echo "ERROR: $?"
fi

fi

shift

done
exit 0

Monday, September 18, 2006

Create md5 sum files in Linux

Learned that the Linux shell can be quickly used to generate md5 checksums of an entire directory to an md5 file, the command is of the form:


cd [directory to generate check sums for]
find ./ -printf “%p\0” | xargs -0 -n 1 md5sum > ./checksums.md5

Then, to check the files in a new location (e.g. on a CD-ROM), use the command:


md5sum -c ./checksums.md5

Or there are other programs for validating the files with the checksums, like wxChecksums which is available for Windows.


I wanted this because the graphical wxChecksums was too difficult to compile, and didn't seem worth the effort. Although I'm sure a couple backwards-compatibility packages for gcc would fix this problem, the command-line method makes it straightforward for doing multiple check sums in a single command in text mode.