Search

INPUT · Slides

Narrow it down by kind and size

01 / 08

A name is not enough on its own

Last lesson you saw that find finds things without telling files and boxes apart.

When you are actually hunting for something, these are the ways you want to narrow it down.

  • Only the files (the boxes are in the way)
  • Only a list of the boxes
  • Only the big ones that are eating up space
  • The empty ones, so you can tidy them away

find does all of these. You just keep adding conditions.

02 / 08

-type picks the kind

Give -type a single letter and you get that kind only.

  • -type f — files (f for file)
  • -type d — boxes (d for directory)

There are others, like l for links, but these two are plenty for now.

find . -type f means "every file below here". It is the shape you type most.

~ $ find . -type d../log./log/old./photos./empty

03 / 08

Conditions side by side mean "and"

You can put as many conditions as you like side by side, and side by side means "and" (both have to hold).

find . -type f -name '*.log'

That is "it is a file, and its name ends in .log".

The order is up to you, but most people write -type, then -name, then -size, because it reads better.

04 / 08

-size narrows by size

Narrowing by size is -size. You put a unit after the number.

  • -size +1kbigger than a kilobyte
  • -size -1ksmaller than a kilobyte
  • -size +2000c — bigger than 2000 bytes (c is bytes)

+ is "bigger than" and - is "smaller than". Those two mean the same thing on conditions other than size as well (-mtime, for instance).

~ $ find . -type f -size +1k./log/access.log

05 / 08

Finding the empty ones

To find things with 0 bytes in them, use -size 0.

There is also -empty, which additionally catches boxes with nothing in them, so it is handy when you are looking for things nobody uses.

find . -type f -size 0    # empty filesfind . -empty             # empty files and empty boxes

06 / 08

-exec gives orders to what it found

At the end of the last lesson you copied the directions across by hand and gave them to cat. That was a nuisance.

With -exec you can run a command on each thing it found.

find . -name '*.log' -exec wc -l {} \;

The {} gets replaced by the directions to what it found. The \; at the end is the marker for "the command stops here". People forget the \, so watch for it.

~ $ find . -name '*.log' -exec wc -l {} \;80 ./log/access.log15 ./log/error.log2 ./log/old/2025.log

07 / 08

"Anything but" is !

Put a ! in front of a condition and it turns round.

find . -type f ! -name '*.log'

That is "a file, and not a .log".

! is a special symbol to the shell as well, so if it will not work, write \! instead, or use -not.

08 / 08

Now have a go

This room has six files and five boxes (counting where you are standing).

The sizes have been spread out on purpose. Only log/access.log is over a kilobyte, and blank.txt is 0 bytes.

When you want to check that the numbers add up, count them with | wc -l.