shell script

A Linux shell script to rename files with a counter and copy them alvin May 9, 2017 - 9:39am

As a quick note, I used this shell script to copy many files with the same name into a directory named tmpdir, giving them all new names during the copy process:

for i in `cat myfiles`
  fname=`basename $i`
  cp $i tmpdir/${count}-${fname}
  count=`expr $count + 1`

The way this works is that I have a file named myfiles that I created with a find command, and it contains a bunch of entries like this:


When the shell script runs, it reads one line at a time from that file, gets the basename (filename) from that line, prepends that name with a counter, then copies the original file to the directory named tmpdir, giving it the new name, so the new filenames will be like this:


I did this to copy all of the images I have under the Messages cache folder on my Mac. A friend accidentally deleted our text message stream, and I was able to recover 350+ images with this script.

You can also use it to copy iTunes music files, where it’s possible that many music files (MP3, M4A, etc.) will have the same filename.

How to write a Scala shell script that reads input from STDIN

As a quick note, if you need an example of how to write a Scala shell script that reads from STDIN (standard input) and writes to STDOUT (standard output), this code shows a solution:

exec scala -savecompiled "$0" "$@"


var line = ""
while ({line = StdIn.readLine(); line != null}) {

An Ubuntu screensaver shell script to rotate images

This is a Bash shell script written for Ubuntu (Linux). I just switched from Mac/MacOS to Ubuntu, and I don't like the default blank screensaver in Ubuntu. I just want a screensaver to rotate my collection of images, so I'm considering using this rather than Xscreensaver. The script comes from

Applescript: How to open a PDF in Preview and go directly to a page alvin July 7, 2016 - 7:43pm

I’m currently generating my new book on “functional programming in Scala” as a PDF using a combination of Pandoc and LaTeX, and as a result it feels like I’m opening the same PDF file about 100 times a day.

A Scala shell script to move your mouse cursor

I’m currently trying to automate a GUI task, and as a part of that, one thing I need to do is move the mouse cursor.

In short, the solution I came up with was to write a Scala shell script that uses the Java Robot class to move the mouse. Here’s the source code for my script, which I named

How to run a Scala SBT-packaged jar file with Java (the `java` command)

If you want to run/execute a main method from a jar file you created with Scala and the sbt package command, this little tutorial shows how to do it. To make things a little more complicated, my Scala project depends on three external jar files, and the main method requires a command-line argument.

As noted in the Summary, you’ll probably want to use a tool like SBT-Assembly for larger projects.

How to make your Scala shell scripts run faster by pre-compiling them

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 14.13, “How to make your Scala shell scripts run faster by pre-compiling them.”


You love using Scala as a scripting language, but you’d like to eliminate the lag time in starting up a script.


Use the -savecompiled argument of the Scala interpreter to save a compiled version of your script.

A basic Scala script like this:

How to prompt users for input from Scala shell scripts

This is an excerpt from the Scala Cookbook, partially modified for the internet. This is Recipe 14.12, “How to prompt users for input from Scala shell scripts.”

Back to top


You want to prompt a user for input from a Scala shell script and read her responses.

Back to top


Use the readLine, print, printf, and* methods to read user input, as demonstrated in the following script. Comments in the script describe each method:

Table of Contents

  1. Problem
  2. Solution
Back to top
How to access command-line arguments in Scala shell scripts alvin June 20, 2015 - 4:34pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.11, “How to access command-line arguments in a Scala shell script.”


You want to access the command-line arguments from your Scala shell script.


Use the same script syntax as shown in Recipe 14.8, “Generating Documentation with scaladoc”, and then access the command-line arguments using args, which is a List[String]` that is implicitly made available:

Raspberry Pi camera module shell script

As a quick note today, this is the source code for a Raspberry Pi (RPI) camera module shell script that I created so a friend can turn her Raspberry Pi camera on and off from the RPI command line (Linux command line):