copy

Scala/FP Idiom: Update as You Copy, Don’t Mutate

“I’ve been imitated so well I’ve heard people copy my mistakes.”

Jimi Hendrix

Goals

In functional programming you don’t modify (mutate) existing objects, you create new objects with updated fields based on existing objects. For instance, last year my niece’s name was “Emily Maness,” so I could have created a Person instance to represent her, like this:

How to copy the macOS Terminal path to the clipboard alvin May 15, 2017 - 9:24am

If you want to copy the current macOS Terminal path to the clipboard, you can do it with this simple command:

$ pwd | pbcopy

pwd prints the path to STDOUT, and pbcopy reads that and copies it to the macOS clipboard. Once the path is on the clipboard you can paste it into your other applications.

Of course you can also create an alias, like this:

alias path="pwd | pbcopy"
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:

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

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:

foo/bar/baz/filename.jpg
foo/baz/filename.jpg

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:

1-filename.jpg
2-filename.jpg
3-filename.jpg

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 copy text from the MacOS Terminal to the clipboard alvin April 10, 2017 - 5:48pm

If you ever need to copy text (or a text file) from the MacOS Terminal to the Mac clipboard, I can confirm that the macOS pbcopy command works. It reads from STDIN and copies the text to the clipboard, so commands like these work:

$ echo "foo bar baz" | pbcopy

$ cat /etc/passwd | pbcopy

How to use the Linux ‘scp’ command without a password to make remote backups

Summary: How to create a public and private key pair to use ssh and scp without using a password, which lets you automate a remote server backup process.

Over the last two years I've ended up creating a large collection of websites and web applications on a variety of Linux servers that are hosted with different companies like GoDaddy and A2 Hosting. I recently embarked on a mission to automate the backup processes for all these sites, and as a result of this effort, I thought I'd share what I've learned here.

How to paste and load blocks of code in the Scala REPL alvin June 20, 2015 - 4:10pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.2, “How to paste and load blocks of code in the Scala REPL.”

Problem

You want to experiment with some code in the Scala REPL, and typing it in or trying to paste it into the REPL won’t work.

How to copy files to an Android emulator’s data directory with 'adb push'

As an Android developer, you can normally use the adb push command to copy files from your computer’s hard drive to an Android device. However, I just ran into a problem where I couldn’t copy files to my Android emulator’s “data” directory, i.e., the /data/data filesystem. When I tried to copy a file using this command:

$ adb push foo.jpg /data/data/com.alvinalexander.myapp/files

I got this Android error: