How to Find Files by Extension using Wildcards in Ubuntu (find -name)

The Mass Extension Hunt

If you are migrating a massive web server or attempting to clean up a disorganized shared drive in Ubuntu Linux, you rarely know the exact names of the files you are looking for. Instead, you need to find every single file of a specific type. For example, you might need to locate every single .php script to audit for security vulnerabilities, or find every .mp4 video file to clear up storage space. To perform this bulk search, you must combine the standard search engine with a mathematical wildcard.

Using the find Command with Wildcards

The Linux find command utilizes the -name flag. By pairing this flag with an asterisk (*), you instruct the search engine to look for any string of characters, as long as it ends in the specific extension you defined.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. To scan the entire /var/www/ directory for every single PNG image file, type the following command exactly:
  3. find /var/www/ -type f -name "*.png"
  4. Press Enter.

The Importance of Quotation Marks

The command syntax is extremely rigid regarding wildcards. You must wrap the search term in double quotation marks ("*.png") or single quotation marks ('*.png'). If you type -name *.png without the quotes, the Linux bash shell will attempt to expand the asterisk before handing it to the find command. If there happens to be a PNG file in your current working directory, the shell will silently rewrite your command to -name logo.png, completely breaking the recursive search and hiding thousands of files from the final output. Always use quotes when using wildcards.

Get the best tech tips delivered straight to your inbox.

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