Unix commands: Find

Mitch

After watching one of Gary Bernhardt’s Destroy All Software screencasts I thought I’d take some of his advice and learn about some basic Unix commands. I’m going to start with find.

This post will be a small collection of useful commands I’ve found.

Basic directory searching

find . - Searching the current directory recursively.

find .. - Search from the parent directory.

find $HOME or find ~ - Search your user directory

find $HOME/projects/*.js - Search for directories and files in your projects directory that end with .js - Not recursive.

find $HOME/projects -name *.js Search for .js files in your projects directory - recursive.

find . -print0 - Print the full filename followed by a null character instead of a newline. Best used with files that have multiline filenames.

find . -printf - for formatting output.

Example: Showing filename and access

find . -name *.js -printf "%p %M\n”

You can use numbers between % (directives) to align text evenly. %100p will right align the filename for 100 characters. %-100p will left align for the same.

Note that this doesn’t work natively on Macs. Instead, you have two options:

fprint

Puts the results into a file. This also requires findutils if you’re on a Mac.

find . -name *.js -fprint all_files.txt

find -P . never follow symbolic links

find -L . follow symbolic links

find -H . don’t follow except when processing command line arguments

Filter by time

find . -mmin n modified n minutes ago

find . -mtime n modified n*24 hours ago.

find . -newer ./composer.json find files modified after composer.json

Other filters

find . -size n[cwbkMG] find files that use n units of space. where c = bytes, k = kilobytes, M = megabytes, G = gigabytes

find . -maxdepth 1 - Only go one directory deep.

find . -mindepth 1 - Ignore the first directory

Actions

Delete

find . -delete - delete files in directory

Example - deleting all .js files.

find . -name *.js -delete

Exec

Removing all .bak files:

find . -name \*.bak -exec rm {} \;

The curly braces will get replaced with each filename that's found.

As seen here.

Windows alternative to find

For powershell use Find-ChildItem. Read more here

Useful resources

Spread the word

Share this article

Like this content?

Check out some of the apps that I've built!

Snipline

Command-line snippet manager for power users

View

Pinshard

Third party Pinboard.in app for iOS.

View

Rsyncinator

GUI for the rsync command

View

Comments