Archive of ‘command-line magic’ category

Bulk process RAW image files

Recently I had to convert about 250 RAW image files to PNGs. For neatness, I wanted to convert the upper-case filenames the camera assigned with a lower-case name.

A little bash script-fu is all it took:

# Extract from SD Card
for i in /Volumes/SDCARD/DCIM/100ND40X/DSC_0*; do
 filename=$(basename "$i")
 lower_file="$(echo $filename | tr '[A-Z]' '[a-z]')"
 # verify it doesn't already exist
 echo -e "Processingnt$i tont$newfile"
 if [[ -e $newfile ]]; then
  echo "****SKIPPING"
  convert "$i" "$newfile"

echo -e "Detoxing..."
find . -iname "*.png" -exec detox "{}" ;

echo "Procedure complete."

(“SDCARD”, etc is the path to the source files)

Once the script was up and running, it took about 1/2 hour to process all the files. Meanwhile, I was off doing something else!

Bulk fix a list of words to the proper case

A set of metal types

A set of metal types (Photo credit: Wikipedia)


Given a long list of upper-case words (one word per line), such as:

... (for thousands of rows) ...

You want to change this to




Perl (Photo credit: Wikipedia)

Use perl from the command line. Assume all your names are in a file called ‘names.txt’. We’re going to send the output to a new file called ‘cap_names.txt’.

$ perl -pe 's/^(.)(.*)$/1L2/g' names.txt > cap_names.txt

How this works

On the matching side:
^(.) → get the first character. The parenthesis allow us to refer to it as 1.

(.*)$ → get the rest of the line. The parenthesis allow us to refer to it as 2.

On the substitution side:

1 → outputs that first character, as is.

L2 → outputs everything else, lower case (the L makes everything in 2 lower case).


Inspired by Regexident’s answer at StackOverflow.

Enhanced by Zemanta

What are your top commands?

Sometimes it is interesting to navel-graze. What commands do you type the most?

history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

Here’s mine:

95 ll
93 git
67 cd
37 vi
29 identify
15 find
12 agvtool
11 history
11 exit
10 cat

“ll” is an alias for ls -l
Interesting that I’ve been using imagemagick (identify) a lot. I’ve been formatting/sizing a bunch of images for inclusion in an iOS app, so that’s probably what that is from.