Chapter 4. Step 4: Finding Files

In the preceding chapter, you learned how to look inside files without changing them. But how do you know which files to look at? In this chapter, I cover searching for files, which can help narrow the scope for your troubleshooting.

find Files Fast

The find command is one of the most useful commands in Linux. The command works like this:

  • Starting at location x

  • Recursively find entries that match condition(s)

  • Do something to each match

As a simple example, let’s say you’re in the /var/log directory, and you want to find all files that end in .log. Because there may be a lot of them, you will pipe the output to less so you can page through it. Here is the command:

/var/log# find . -name \*.log -print | less

Remember that I said the \ has a different meaning in Bash, that it is an escape character? Notice its use in this example, where it is preventing the Bash shell from expanding the wildcard character (*) into all matching files in the current directory. Instead, by escaping it, the \ character is telling find to expand that wildcard in the current directory and all of its children.

Figure 4-1 shows the first page of the output I got from that command, awaiting our navigation via less.

find results
Figure 4-1. find results

The find command has a lot more power than this simple example! You can find files and directories based on creation and modification ...

Get Ten Steps to Linux Survival now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.