How to Find Files by Extension in Ubuntu Linux

The File Type Hunt

When managing an Ubuntu Linux server, you rarely need to find a single file by its exact name. More often, you need to find an entire class of files. For example, if your web server is running out of storage space, you might need to locate every single .log file scattered across the hard drive so you can delete them. If you are migrating a website, you might need to find every single .php script to ensure they are backed up. To execute a sweeping search based entirely on file extensions, you must use a wildcard character.

Using the find Command with -name

The standard Linux find command allows you to search for partial text matches by utilizing the asterisk (*) as a wildcard.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. Determine the directory you want to search. To search the entire server, use the root directory (/).
  3. To locate every single file ending in .log, type the following command:
    sudo find / -type f -name "*.log"
  4. Press Enter and provide your administrator password.

Understanding the Syntax

The quotation marks are absolutely critical. If you type -name *.log without quotes, the terminal will attempt to expand the wildcard before the find command even runs, which will cause the search to crash if there are multiple log files in your current directory. By wrapping "*.log" in quotes, you force the find command to interpret the asterisk literally: “Find any file that has any name, as long as it ends exactly with .log”. You can easily swap the extension to find "*.jpg" or "*.conf" files instead.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.