find

How to search multiple jar files for a string or pattern (shell script)

Here’s a Unix shell script that I use that search Java “jar” files for any type of pattern. You can use it to search for the name of a class, the name of a package, or any other string/pattern that will show up if you manually ran jar tvf on each jar file. The advantage of this script — if you’re a Unix, Linux, or Cygwin user — is that it will search through all jar files in the current directory:

Unix: How to find files with multiple filename extensions

As I mentioned in my How to find multiple filenames with Linux find tutorial, you can use find command syntax like this to find files with multiple filename extensions:

find iTunes \( -name "*.mp3" -o -name "*.m4a" \)

As that command shows, I ran this find command to find all of my music files under my iTunes directory, including .mp3 and .m4a filename extensions.

While I’m in the neighborhood, this is the full find command I use to backup all of my iTunes files that have changed or been added in the last 180 days:

find iTunes \( -name "*.mp3" -o -name "*.m4a" \) -type f -mtime -180 -print0 | xargs -0 tar rvf NewMusic.tar

There’s probably an easier way to do this, but that backup command works for me.

A Unix find and move command (find in subdirectories) alvin September 11, 2016 - 3:52pm

This is a dangerous Unix command, but if you want to move a bunch of files from their subdirectories into your current directory, this find and mv command works:

find . -type f -exec mv {} . \;

That command finds all files beneath the current directory, and moves them into the current directory. I just moved a bunch of files from their (iTunes) subdirectories into my current working directory, and that find and move command did the trick. (But again, it’s a dangerous command, be careful out there.)

How to search a MongoDB collection with Scala and Casbah alvin June 20, 2015 - 6:27pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 16.5, “How to search a MongoDB collection with Scala and Casbah.”

Problem

You want to find objects in your MongoDB collection using Scala and the Casbah driver.

Solution

Use the find* methods of the MongoCollection class to get the elements you want, specifically the find and findOne methods.

How to find good Scala libraries alvin June 20, 2015 - 4:22pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 14.7, “How to find good Scala libraries.”

Problem

Ruby has the RubyGems package manager, which lets developers easily distribute and manage the installation of Ruby libraries; does Scala have anything like this?

Scala: How to extract parts of a string that match a regex alvin May 30, 2015 - 5:10pm

Scala String FAQ: How can I extract one or more parts of a string that match the regular-expression patterns I specify?

Solution

Define the regular-expression patterns you want to extract from your String, placing parentheses around them so you can extract them as “regular-expression groups.” First, define the desired pattern:

val pattern = "([0-9]+) ([A-Za-z]+)".r

Next, extract the regex groups from the target string:

How to replace regular expression patterns in strings in Scala alvin May 30, 2015 - 4:57pm

Scala String FAQ: How do I replace a regular expression (regex) pattern in a String in Scala?

Solution

Because a String is immutable, you can’t perform find-and-replace operations directly on it, but you can create a new String that contains the replaced contents. There are several ways to do this.

You can call replaceAll on a String, remembering to assign the result to a new variable:

How to determine if a Scala String contains a regular expression pattern alvin May 30, 2015 - 4:18pm

Scala String FAQ: How can you determine whether a String contains a regular expression pattern in Scala? (Or, “How can I find the first match (or all matches) of a regex in a String?”)

Solution

Create a Regex object by invoking the .r method on a String, and then use that pattern with findFirstIn when you’re looking for one match, and findAllIn when looking for all matches.

Sencha ExtJS Store “findRecord” query examples

Here are a few short examples of how to find an object in a Sencha Store (ExtJS or Sencha Touch).

As a first example, imagine that you have a Store of users, and want to find a user with the first name of “Alvin”. Assuming that your user model has a field named firstName, your query would look like this:

var user = usersStore.findRecord('firstName', 'Alvin');

After this query you can use the user object just as though you had created it by hand. (The object returned is a Model instance.)

A Linux shell script to find large files alvin November 19, 2013 - 11:48am

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: