linux

Unix/Linux: Find all files that contain multiple strings/patterns

When using Unix or Linux, if you ever need to find all files that contain multiple strings/patterns, — such as finding all Scala files that contain 'try', 'catch', and 'finally' — this find/awk command seems to do the trick:

find . -type f -name *scala -exec awk 'BEGIN {RS=""; FS="\n"} /try/ && /catch/ && /finally/ {print FILENAME}' {} \;

As shown in the image, all of the matching filenames are printed out. As Monk says, you’ll thank me later. :)

(I should mention that I got part of the solution from this gnu.org page.)

Linux/Unix: How to copy a directory and save the date/time file information

If you need to copy a directory on Unix/Linux and want to preserve the date/time information while copying the directory and files, use the -p option to save the date/time information, and the -r option to copy the directory properly. For instance, I just used this cp command to copy a directory named OldDir to a new directory named NewDir, while retaining all of the date/time file information:

Using find and grep to print lines before and after what you’re searching for

A cool thing about the Unix/Linux grep command is that you can show lines before and after a pattern match with the -B and -A options. As an example, I just used this combination of find and grep to search for all Scala files under the current directory that contain the string null. This command prints five lines before and after each null line in each file:

$ find . -type f -name "*.scala" -exec grep -B5 -A5 null {} \;

That’s good stuff, but it prints a really long list of lines, and I can’t tell the output of one file from another. To fix this, I put the following code in a file named helper.sh, and made it executable:

Jeyes, a Java version of Xeyes

Jeyes, a Java version of Xeyes

In my spare time back in 2011 I created a Java version of the old Unix/X-Windows “Xeyes” application. If you ever used Xeyes, you know it as a set of eyes that are displayed on-screen, and follow the mouse cursor as you move it around.

Now in 2019 I just brought it back to life, and here’s a 56-second video that shows how it works:

A Scala “Word of the day” shell script

I have a 19" monitor on the counter between my kitchen and living room, and it’s powered by a Raspberry Pi. I use the Linux Phosphor screen saver to show a scrolling “news and stock ticker” on the display, which I’ve programmed to show news from several different sources (Atom and Rss feeds, along with other news and data sources). An old version of the display looks like this:

My Raspberry Pi news ticker display

Today I added a new “Word of the day” feature to the display, and as with all of the other code, I wrote a Scala shell script to generate the output.

Scala code to find (and move or remove) duplicate files

My MacBook recently told me I was running out of disk space. I knew that the way I was backing up my iPhone was resulting in me having multiple copies of photos and videos, so I finally decided to fix that problem by getting rid of all of the duplicate copies of those files.

So I wrote a little Scala program to find all the duplicates and move them to another location, where I could check them before deleting them. The short story is that I started with over 28,000 photos and videos, and the code shown below helped me find nearly 5,000 duplicate photos and videos under my ~/Pictures directory that were taking up over 18GB of storage space. (Put another way, deleting those files saved me 18GB of storage.)

The Red Hat ethos

The URL contains a statement of the Red Hat ethos. A couple of good quotes:

Open source is a development model, not a business model. Red Hat is in the enterprise software business and is a leading provider to the Global 500. Enterprise customers need products, not projects and it’s incumbent on vendors to know the difference. Open source projects are hotbeds of innovation and thrive on constant change. These projects are where sometimes constant change happens, where the development is done.