Skip to main content

Mastering the Find Command: A Guide to Locating Specific File Extensions

·559 words·3 mins· loading · loading ·
find Linux tree
UmmIt
Author
UmmIt
Loves to write about technology, and cybersecurity related topics :)
Table of Contents

Introduction
#

In the world of Unix-based operating systems, the ‘find’ command stands as a stalwart tool for searching files and directories within complex directory hierarchies. This versatile utility empowers users to pinpoint files with specific extensions efficiently. In this guide, we’ll delve into the art of wielding the ‘find’ command effectively to locate files that match your criteria.

What is ‘find’?
#

Before we embark on our journey into the depths of ‘find,’ it’s essential to understand its fundamental usage. But first, let’s introduce another helpful tool: the ’tree’ command.

Installing the ’tree’ Command and Usage
#

Visualizing directory structures can be immensely beneficial, and the ’tree’ command excels at this task. To utilize ’tree,’ you need to install it on your system. If you’re running a system that employs the Pacman package manager, like Arch Linux, you can acquire ’tree’ with the following command:

sudo pacman -S tree

Once ’tree’ is on board, you can use it to present directory trees visually. Just open your terminal and invoke ’tree’ followed by the directory path you wish to visualize. For instance:

tree /path/to/directory

Here’s an example of the ’tree’ command output to visualize a directory structure:

/path/to/directory
├── folder1
│   ├── file1.txt
│   ├── file2.txt
│   └── subfolder1
│       └── subfile1.txt
├── folder2
│   ├── file3.txt
│   └── file4.jpg
├── file5.txt
└── file6.jpg

In this example:

  • /path/to/directory is the root directory.
  • It contains two subdirectories, folder1 and folder2.
  • folder1 has files file1.txt, file2.txt, and a subdirectory subfolder1.
  • subfolder1 contains subfile1.txt.
  • folder2 contains file3.txt and file4.jpg.
  • The root directory also has two files, file5.txt and file6.jpg.

This tree-like structure provides a visual representation of the directory hierarchy, making it easier to understand the layout of your files and directories.

Now, let’s proceed to the core topic: using the ‘find’ command to unearth files by their extensions.

Basic Usage of ‘find’
#

To locate files bearing a specific extension, ‘find’ is your trusty companion. Its basic usage involves specifying the starting directory and the file extension pattern you’re hunting for. Here’s how it works:

find . -name "*.jpg"

In this command:

  • . designates the starting directory for the search (usually the current directory).
  • -name signals that you’re matching based on the filename.
  • "*.jpg" represents the pattern you’re seeking, where * acts as a wildcard, and “.jpg” is the desired file extension.

Notes: Please make sure you have enclosed it in double quotes

Advanced Usage
#

Here are some example for a useful command!

Deleting Files with Specific Extensions
#

Sometimes, you might need to take more decisive action, like removing files with a particular extension. To achieve this, you can combine ‘find’ with the ’exec’ and ‘rm’ commands. For instance, if you want to bid adieu to all “.jpg” files, the following command will do the trick:

find . -name "*.jpg" -exec rm -rfv {} \;
  • -name "*.jpg" specifies that you want to find files with the “.jpg” extension.
  • -exec enables you to execute the ‘rm -rfv’ command on the files uncovered by ‘find.’
  • {} serves as a placeholder for the discovered files.
  • \; marks the end of the ‘-exec’ command.

Conclusion
#

The ‘find’ command emerges as an invaluable companion for navigating Unix-based systems. Whether you’re in pursuit of files with specific extensions or undertaking other file-related quests, mastering ‘find’ can streamline your efforts and save you precious time.

Related

Super Handy Linux Command Tips That Will Transform Your Terminal Experience
·923 words·5 mins· loading · loading
Linux Terminal
Building the Linux Kernel on your Linux System (systemd-boot): Step-by-Step Guide
··3748 words·18 mins· loading · loading
Linux Kernel
Using Different Linux Kernel Versions with systemd-boot and Bootctl
·582 words·3 mins· loading · loading
Linux Bootctl Boot-loader
Optimizing Network Performance: Changing DNS Servers on Linux
·583 words·3 mins· loading · loading
Linux Nameserver DNS
Step-by-Step Guide: Creating a Swap File on Your Linux VPS
·1538 words·8 mins· loading · loading
VPS SSH Linux Swap
Customizing Your GNOME Desktop: Installing Icon and cursor themes
··982 words·5 mins· loading · loading
GNOME Linux