file

The beginning of a Scala “FileUtils” class

In production code I recommend that you use a good “Files” library like Apache Commons IO, but if you want to create your own Scala FileUtils class, here’s some source code that can help you get started.

First, here’s some code for the FileUtils class (an object, technically):

A collection of Unix/Linux ‘grep’ command examples

Linux grep commands FAQ: Can you share some Linux/Unix grep command examples?

Sure. The name grep means "general regular expression parser", but you can think of the grep command as a "search" command for Unix and Linux systems: it's used to search for text strings and more-complicated "regular expressions" within one or more files.

I think it's easiest to learn how to use the grep command by showing examples, so let's dive right in.

How to process every line in a file with a Unix/Linux shell script

Unix/Linux shell script FAQ: How do I write a Unix or Linux shell script where I "do something" for every line in a text file?

Solution: An easy way to process every line in a text file is to use a Unix/Linux while loop in combination with the Linux cat command, like this:

Sorting ‘ls’ command output by filesize alvin July 14, 2017 - 11:15am

I just noticed that some of the MySQL files on this website had grown very large, so I wanted to be able to list all of the files in the MySQL data directory and sort them by filesize, with the largest files shown at the end of the listing. This ls command did the trick, resulting in the output shown in the image:

ls -Slhr

The -S option is the key, telling the ls command to sort the file listing by size. The -h option tells ls to make the output human readable, and -r tells it to reverse the output, so in this case the largest files are shown at the end of the output.

A Linux shell script to find large files

I made a mistake in configuring logrotate on a new Linux system, and almost ran into a problem because of that. Fortunately I saw the problem before it became a BIG problem, but as a result, I decided to add a script to my Linux system to check for large files, typically log files that have grown out of control for one reason or another.

Here then is a simple Linux shell script I named LargeFileCheck.sh, which searches the filesystem for files that are larger than 1GB in size:

A Linux shell script to rename files with a counter and copy them

As a quick note, I was recently looking for all Messages/iMessage files that are stored on my Mac, and I used this shell script to copy all of those files — many of which have 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

How to copy text from the MacOS Terminal to the clipboard

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