Sling Academy
Home/DevOps/Ubuntu: Using Wildcards in ‘find’ Command

Ubuntu: Using Wildcards in ‘find’ Command

Last updated: January 28, 2024

The Linux find command is a powerful tool for searching and locating files and directories on your filesystem. One of the features that make find particularly powerful is its ability to use wildcards, which allows you to create versatile search patterns. This tutorial aims to provide a comprehensive guide on how to use wildcards in the find command on an Ubuntu system.

Understanding Wildcards

Before diving into the specifics of the find command, let’s have a quick overview of wildcards often used in bash. Wildcards are symbols or set of symbols that represent other characters:

  • * – Matches any number of characters, including none.
  • ? – Matches any single character.
  • [] – Matches any one of the enclosed characters.

Basic Usage of Wildcards in find

Let’s start by using wildcards to search for files by their names.

find /path/to/search -type f -name '*.txt'

This command searches for all files with a .txt extension, starting at /path/to/search. Here, * stands for any sequence of characters.

Suppose you’re searching for all files with a single character extension:

find /path/to/search -type f -name '*.?'

This command makes use of ? to represent any single character in the file extension.

Matching Specific Patterns

If you want to find files that match a specific pattern, square brackets come in handy:

find /path/to/search -type f -name 'data[0-9].txt'

This command finds files such as data1.txt, data2.txt, up to data9.txt.

Using Wildcards to Match Directories

Wildcards can also be used to match directory names:

find /path/to/search -type d -name 'proj*'

This command finds all directories starting with ‘proj’.

Case-Insensitive Searches

The find command can perform case-insensitive searches using wildcards with the -iname option:

find /path/to/search -type f -iname '*.JPG'

This command finds all files with a .jpg extension, regardless of case.

Complex search patterns

You can define more complex search patterns using wildcards:

find /path/to/search -type f -name 'photo-????-*.jpg'

This command finds file names like photo-2021-001.jpg or photo-2020-vacation.jpg, where ???? represents any four characters.

Excluding Specific Patterns

Sometimes you may want to exclude files or directories based on a pattern. While the find command doesn’t natively support an exclude option, we can achieve this by combining options:

find /path/to/search -type f ! -name '*.log'

This command lists all files that do NOT have the .log extension.

Combining Multiple Wildcard Patterns

It’s possible to combine wildcards to search for multiple patterns at once. This is done using the -o (OR) operator:

find /path/to/search \\( -name '*.txt' -o -name '*.md' \\)

This command finds all files with either a .txt or .md file extension.

Advanced: Wildcards with Exec

For advanced users, the find command can pair with -exec to perform actions on the found files. Here’s an example of how to use wildcards with -exec:

find /path/to/search -type f -name '*.bak' -exec rm {} \\;

This command finds all files with a .bak extension and deletes them. The {} is replaced with the file name of each matched file.

Conclusion

Wildcards enhance the flexibility and functionality of the find command, allowing for a broad range of file searching capabilities on your Ubuntu system. Understanding and harnessing these patterns can greatly improve your file management and scripting tasks. With this knowledge, you’re now equipped to construct powerful file search commands tailored to your specific needs.

Next Article: Soft and Hard Links in Linux: The Ultimate Guide

Previous Article: Ubuntu: How to find a file/directory by name

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide