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:

Scala: How to list files and directories under a directory

When using Scala, if you ever need to list the subdirectories in a directory, or the files under a directory, I hope this example is helpful:


object FileTests extends App {

    // list only the folders directly under this directory (does not recurse)
    val folders: Array[File] = (new File("/Users/al"))
        .filter(_.isDirectory)  //isFile to find files


If it helps to see it, a longer version of that solution looks like this:

How to show the largest files under a directory on Mac OS X (Unix)

Here’s an example that shows how to find the largest files under a directory on MacOS and Linux/Unix systems.

A du/sort command to show the largest files under a directory on Mac OS X

The Unix/Linux command that worked for me on my MacOS system is this:

$ du -a * | sort -r -n | head -10

du is the disk usage command, and the -a flag says, “Display an entry for each file in a file hierarchy.” Then I use the sort command to sort the du output numerically and in reverse. After that, head -10 shows only the first ten lines of output. In the Music folder on my Mac the command and output look like this:

Linux: Recursive file searching with grep -r (like grep + find)

Linux grep FAQ: How can I perform a recursive search with the grep command in Linux?

Solution: find + grep

For years I always used variations of the following Linux find and grep commands to recursively search subdirectories for files that match a grep pattern:

find . -type f -exec grep -l 'alvin' {} \;

This command can be read as, “Search all files in all subdirectories of the current directory for the string ‘alvin’, and print the filenames that contain this pattern.” It’s an extremely powerful approach for recursively searching files in all subdirectories that match the pattern I specify.

How to get Java/Scala system environment variables and properties

Want to get the system environment variables and/or properties from your Scala or Java application? This quick post shows what environment variables and properties are available.

Here’s a little Scala application that prints all the environment variables and properties. You’ll see that you can convert it to Java very easily:

How to create an SBT project with subprojects

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.6, “How to create an SBT project with subprojects.”


You want to configure SBT to work with a main project that depends on other subprojects you’re developing.


Create your subproject as a regular SBT project, but without a project subdirectory. Then, in your main project, define a project/Build.scala file that defines the dependencies between the main project and subprojects.

A Scala function to list subdirectories in a directory

If you ever need to generate a list of subdirectories in a directory in Scala, here's one way to do it:

def getListOfSubDirectories(directoryName: String): Array[String] = {
  return (new File(directoryName)).listFiles.filter(_.isDirectory).map(_.getName)

I intentionally wrote that function in a short, "Scala like" style, but you can expand it to multiple lines, if you prefer.