Daniel Allen Deutsch

Web Developer

Blog

Finding Stuff in Unix

November 16, 2015

If you're anything like me, you are always trying to find stuff on your computer.

"Where did I save that file? Is it buried in my home folder or on my desktop?" "Hm... I'm not sure where the config file for this program is stored."

Or perhaps someone calls you over because they need help looking at an EC2 instance used for the staging server—it's acting funky. You current knowledge about what this box does is tangential at best.

The solution is easy. Search for it!

I always find myself re-learning the commands. I cannot keep track of: 1. which operations are recursive 2. how each command handles partial matches and 3. what acts on words in files vs file names vs directory names.

Let's go through them one-by-one.

Searching For a File

find -type f

'find' takes a directory and looks recursively through it.

Pass that you want a 'file' through the -type flag.

Pass the name of the file through the -name flag (or -iname for case insensitive).

The file must exactly match the name to return a result. If you do not know the exact name of the file, you can use * (asterisk), the wildcard matcher.

Let's look at some examples:

I am looking for 'example.txt'. I know I saved it in my home folder.

find ~/ -type f -name example.txt

I am looking for all css files in my blog folder.

find ~/blog -type f -name *.txt

I am looking for a file with 'ellowor' in the name. I know I saved it somewhere.

find / -type f -name *ellowor*

locate

'locate' is a newer and non-standard command, so it is not available on all operating systems.

However, it is faster and better designed to find files when their location is unknown.

I am looking for a file with 'elephant' in the name across my machine.

locate elephant

I am looking for a file with 'eLePhAnt' in the name across my machine, but do not remember which letters are capitalized.

locate -i elephant
Searching For a Directory

find -type d

Good news—searching for a directory uses the same 'find' command.

The only difference is the -type flag, which now take 'd'.

I am looking for a directory named 'my-files'. I know it is nested in the home folder.

find ~/ -type d -name my-files

I am looking for all folders with 'important' in the name across my computer.

find / -type d -name *important*

If you are looking for either a file or directory, you can just leave off the -type flag and results of both will be returned.

Searching For Text Within a File

grep

Have you ever been on a page on a web browser, looking for something specific, and your co-worker suggested you 'grep for it'. 'Oh... just hit control F?' Let's explain why the word grep is used as a synonym for 'search for'.

'grep' searches through a file or files looking for text matches within the document(s). It is NOT recursive by default.

If you want to recursively search through a folder, use the -r flag.

This command WILL return partial matches, so if you grep for 'elloworl' the text 'helloworld' would be returned.

A final set of examples:

I am looking to see if the file ~/poem.txt has the world 'sound' in it.

grep sound ~/poem.txt

Which of the .css files in my lists folder has the text 'twitter'?

grep twitter ~/lists/*.css

Of all the files in my important directory, which contain the word 'apple'?

grep -r apple ~/important/

Where did I save that file? The only thing I remember about it is that 'amphibian' is in it.

grep -r amphibian /
Wrapping Up

It is important to note that all of these commands are far more powerful than what I went over. For example, searching with regex is a common usecase. As is limiting the depth of recursive searches. The man pages have the full details for each command. But I hope this has you covered for your most common searches.

To recap, use find to search for file and folder names. This command is recursive, and requires wildcard(s) for partial matches.

Use grep to search for text within a file. This command is not recursive by default, but can be with the -r flag. grep does return partial matches.


Have anything to say? Questions or feedback? Tweet at me @cmmn_nighthawk!