directory

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.

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

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

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

Here’s a Unix shell script that I use to 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:

How to create an SBT project directory structure

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.1, “How to create an SBT project directory structure.”

Problem

SBT doesn’t include a command to create a new project, and you’d like to quickly and easily create the directory structure for a new project.

Solution

Use either a shell script or a tool like Giter8 to create your project’s directory structure. Both approaches are shown here.

Scala: How to run an external command (process) in a different directory

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 12.18, “How to run an external command (process) in a different directory.”

Problem

You want to use another directory as the base directory when running an external command.

How to list subdirectories beneath a directory in Scala alvin June 19, 2015 - 11:13am

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.10, “How to list subdirectories beneath a directory in Scala.”

Problem

You want to generate a list of subdirectories in a given directory.

Solution

Use a combination of the Java File class and Scala collection methods:

How to list files in a directory in Scala (and filter the list)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.9, “How to list files in a directory in Scala (and filtering them).”

Problem

You want to get a list of files that are in a directory, potentially limiting the list of files with a filtering algorithm.

Solution

Scala doesn’t offer any different methods for working with directories, so use the listFiles method of the Java File class. For instance, this method creates a list of all files in a directory:

How to rename a directory when using Git alvin April 14, 2015 - 4:11pm

This website has the best discussion I’ve found on how to rename a directory in Git.

Note that if you need to update text in many files, my article on how to use the Unix sed command to edit many files in place may be helpful at this time.

Android/Java: How to get your app's root data directory

If you ever need to get the root data directory of your Android application (app) from within your Java code, I can confirm that this approach works:

File rootDataDir = getActivity().getFilesDir();

When I log that directory like this:

Log.i(TAG, rootDataDir.toString());

it prints this output for my application:

/data/data/com.alvinalexander.mynewapp/files

where com.alvinalexander.mynewapp is the package name for my new Android app.