• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

How to find files on linux / mac

Posted Jan 19, 2023 — Updated Jan 10, 2024

Command line find is a powerful, underutilized tool. In this post, I’ll go through the following use cases:

  1. Find files by name
  2. Find files by file extension
  3. Find recently modified files
  4. Delete files using find

Find files by name

I use this all the time. Let’s say I’m in my ~/Downloads directory. That directory has several hundred files in it—and quite a few subdirectories—so it’s a bit hard to comb through in a GUI. I misplaced my favicon files in there, so I’ll use find:

find . -name "favicon*"

# Here's why I couldn't find em, they're in a subdirectory
# ./dev_site/favicon_io
# ./dev_site/favicon_io/favicon-16x16.png
# ./dev_site/favicon_io/favicon.ico
# ./dev_site/favicon_io/favicon-32x32.png

The search above was for all files starting in my current directory (.) whose names start with “favicon”. Find also has -iname for case-insensitive filename search.

Find files by file extension

This is really just another flavor of finding by name, where the file extension is part of the name:

# Find all csv files
find . -name "*.csv"
# ./data/states/MN.csv
# ./data/states/AZ.csv
# ./data/summaries/N00047871.csv
# ./data/summaries/N00001373.csv

You’ll notice above that find searches directories recursively by default, which is handy. There is a way to limit the depth, but I rarely need it and won’t cover it here. To learn more, try this link and look for -maxdepth.

Find recently modified files

I use this version less frequently than finding by name, but it still comes in handy:

# Find all files with "last modified" time in the last 3 days
# Note it is negative 3
find . -mtime -3

The ls command is also very useful for finding recently modified files; the -t flag will order your results by last modified time. For example, this command will give you the 5 most recently modified files in a directory: ls -t | head -n 5

Delete files using find

No need to use xargs, find has a built-in delete option:

# BE CAREFUL: find will do this recursively by default
find . -name "filename.csv" -delete

# If you're unsure about your directories, use maxdepth
find . -name "filename.csv" -maxdepth 1 -delete

Earlier I mentioned that find searches directories recursively by default. Usually I think that’s a good thing, but when deleting it could come back to bite you. If you’re deleting with find and you’re not 100% sure about the directory structure underneath you, you might want to pass in a -maxdepth flag to stop find from going too far into subdirectories.

That’s it! Find has a lot more options, but I rarely use anything other than what’s described above. If you want to dive deeper, the helpful link below has a ton of information.


Helpful Links

A much more in-depth walkthrough of find – opensource.com

Filed Under: Command Line, Productivity

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy