Categories: Development

Using Find in Linux

A method to find files on a Linux system is with the well named find command, find allows for certain parameters to be passed allowing for a more specialized and refined search.

find can also be used to execute commands on the returned files.

Simply finding a file by its case sensitive name:

find -name index.php

For the search to not be case sensitive:

find -iname readme.txt

Type

Searching by type;

  • c: character devices
  • d: directory
  • f: regular file
  • l: symbolic link

Find files that are of a certain extension:

find / -type f -name "*.log"

 

Size and time

Search filtering can be done by size too;

  • c: bytes
  • k: Kilobytes
  • M: Megabytes
  • G: Gigabytes

All MP4 files greater than 1 Gigabyte:

find / -type f -name "*.mp4" +1G

All MP4 files less than 20 Megabytes:

find / -type f -name "*.mp4" -20M

Time filtering is based around 3 methods:

  • Access Time: When the file was read or written to last.
  • Change Time: Last time the file’s meta-data was changed.
  • Modification Time: Last time the file was modified.

Finding files accessed less than a week ago:

find / -atime -7

Finding with modification time of less than 1 day:

find / -mtime -1

All these commands and filters can be combined to help narrow down or even widen your search.

An example for doing something with the “found” files is this:

find /tmp -type f -name "*.log" -mtime +7 -exec rm -f {} \;

This will delete all the .log files in /tmp that were created over 1 week (7 days) ago.

View the official find docs here or a comprehensive guide here.

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago