1.31.2016

Using Unix to Change Filenames

I have recently been trying to standardize my file names to keep a better consistency. I have decided to use all lowercase letters rather than the title case that I have been using.

View inside a folder that has file and folder names using title case (combination of upper- and lowercase letters.

I knew there must be a quick way of making these changes using Unix commands in Terminal. After a quick Google search, I found this site. After using the cd command to get in the directory, the syntax to change everything to lowercase looks like this:

for f in * ; do mv -v $f `echo $f | tr '[A-Z]' '[a-z]'`; done

To understand how this works, let's parse the syntax. First the semicolon is how we put multiple commands on a single line. It is akin to hitting <enter>. So it may be a bit easier to understand if we format this in a different manner.

for f in *
  do mv -v $f `echo $f | tr '[A-Z]' '[a-z]'`
done

The first and last lines are executing a FOR loop. The f in * just says do something (in this case the something is the commands issued in the second line) for each filename in the directory, that is where the * comes in. The f is an arbitrary variable name.

The do command of the second line tells Unix to actually do what follows it. The heart of this command is mv. In Unix-speak, mv can either be used to move files or rename files, depending on what comes after it. In this case, we use it to rename files. The -v is an optional flag that tells Unix to print to the screen everything that mv is doing (verbose mode). In general the structure of using mv to rename files looks like this:

mv old_filename new_filename

From the FOR loop, the name of each file is stored in the variable f. Thus we can get that name by accessing $f (the old filename). Notice the new filename is not given explicitly, but is a series of Unix commands. This is enclosed in a set of backticks (`commands`). Everything typed between backticks is evaluated (executed) by the shell. (The mathematical equivalent would be enclosing this in parentheses so order of operations is followed.)

The series of commands executed is

echo $f | tr '[A-Z]' '[a-z]'

This echos back the filename ($f) and then pipes it into the translate (tr) command. This is like a pipeline, where the output of a one process is fed as input into a subsequent process. This is that vertical bar (|). SO the output of echo (the filename) is fed into the tr command.

The tr command translates the first thing into the second thing. Since these are characters, they are both enclosed in quotation marks. Both the first and second component are calling regular expressions. The first component, [A-Z], looks for uppercase letters, which are then translated into lowercase letters, [a-z], the second component.

After executing the command, the verbose flag outputs the following:

Andy -> andy
Dad -> dad
Fractals-and-Chaos-Workshop-Pamphlet.pdf -> fractals-and-chaos-workshop-pamphlet.pdf
Grandpa -> grandpa
Soccer -> soccer


The result is all the filenames (or folder names) are now lowercase

The same directory after running the commands.

There are other ways to do this as well. For example, the regular expressions can be changed so the commands are:

echo $f | tr '[:upper:]' '[:lower:]'

Be careful if you use these commands. You can accidentally write over a file if it has the same name. For example if your directory contained two files,

myFile.doc
myfile.doc

and you issued this command, you would write over the 'myfile.doc' file.






1.03.2016

Read-a-Thon (c. 1982/1983)

Going through some more old stuff, and came across the list of books I read for the Multiple Sclerosis Society Read-a-Thon. According to the "Book List Record", I read 35 books and had 9 sponsors. (The sponsor bit was not copied by my mom, so I do not know whether they donated per page or per book.) I also don't know how long this read-a-thon took place, but my guess is that it happened over Christmas break. Here is the list of books:

Title Author Pages
1 The Littles to the Rescue Peterson, John 95
2 The Littles Take a Trip Peterson, John 95
3 The Merry Adventures of Robin Hood Pyle, Howard 238
4 The Littles and the Big Storm Peterson, John 80
5 The Littles Give a Party Peterson, John 96
6 The Case of the Snowbound Spy Hildick, Edmund Wallace 120
7 The Adventures of Tom Sawyer Twain, Mark 238
8 The Count of Monte Cristo Dumas, Alexandre 238
9 The Adventures of Huckleberry Finn Twain, Mark 238
10 The Secret of the Rosewood Box Orton, Helen Fuller 120
11 Mystery in the Pirate Oak Orton, Helen Fuller 120
12 Socks Cleary, Beverly 156
13 Encyclopedia Brown and the Case of the Midnight Visitor Sobol, Donald J. 112
14 Encyclopedia Brown and the Case of the Dead Eagles Sobol, Donald J. 96
15 Encyclopedia Brown Tracks Them Down Sobol, Donald J. 96
16 Encyclopedia Brown Takes the Case Sobol, Donald J. 96
17 Crazy Horse, Sioux Warrior Meadowcroft, Enid LaMonte 104
18 May I? Please? Thank You Wilt, Joy 146
19 The Country Bunny and the Little Gold Shoes Heyward, DuBose 46
20 Encyclopedia Brown Solves Them All Sobol, Donald J. 96
21 Encyclopedia Brown Finds the Clues Sobol, Donald J. 86
22 Encyclopedia Brown and the Case of the Exploding Plumbing Sobol, Donald J. 73
23 The Great Rabbit Robbery Hildick, Edmund Wallace 101
24 Encyclopedia Brown Carries on Sobol, Donald J. 72
25 Mysteries of Sherlock Holmes Doyle, Arthur Conan 95
26 A Bear Called Paddington Bond, Michael 128
27 Encyclopedia Brown, Boy Detective Sobol, Donald J. 75
28 Encyclopedia Brown and the Case of the Secret Pitch Sobol, Donald J. 96
29 Encyclopedia Brown Shows the Way Sobol, Donald J. 96
30 The Case of the Nervous Newsboy Hildick, Edmund Wallace 131
31 (Copy cannot be read)
32 Encyclopedia Brown Keeps the Peace Sobol, Donald J. 96
33 Cam Jansen and the Mystery of the Stolen Diamonds Adler, David A. 58
34 The Mystery of the Dead Man's Riddle Arden, William 45
35 Pirate Island Adventure Parrish, Peggy 120

I clearly liked mystery, Encyclopedia Brown, McGurk, and The Three Investigators books appear in this list. The classics were the Great Illustrated Classics, so while the page count was almost always 238, about half of the pages were pictures.