How to find files in Bash?

Find files by their name in Bash in all subdirectories

find . | grep MyFileName
  • It matches all files that contain MyFileName in their name

Find files by their name in Bash in the current directory

ls | grep MyFileName

How to find new files in Bash?

Find files newer than 5 minutes?

find . -mmin -5
  • The m in mmin stands for modified
  • The min in mmin is used to indicate minutes.
  • The - sign means newer than the following number of minutes.

Find files newer than 5 days?

find . -mtime -5
  • The m in mtime stands for modified.
  • The time in mtime is used to indicate days.
  • The - sign means newer than the following number of days.

How to find old files in Bash?

Find files older than 5 minutes?

find . -mmin +5
  • The m in mmin stands for modified
  • The min in mmin is used to indicate minutes.
  • The - sign means older than the following number of minutes.

Find files older than 5 days?

find . -mtime +5
  • The m in mtime stands for modified.
  • The time in mtime is used to indicate days.
  • The - sign means older than the following number of days.

How to find files by size in bash?

Find files smaller than 4Mb

find . -type f -size -4M
  • The -type f excludes everything that is not a file (directory, link).
  • The - before 4M means to search files smaller than that.
  • Other Flags:
    - c Bytes
    - w Kilobytes
    - M Megabytes
    - G Gigabytes

Find files larger than 4Mb

find . -type f -size +4M
  • The -type f excludes everything that is not a file (directory, link).
  • The + before 4M means to search files larger than that.
  • Other Flags:
    - c Bytes
    - w Kilobytes
    - M Megabytes
    - G Gigabytes

Find files containing a string in bash

grep -rlI "string in file" .
  • The I makes it faster, as it ignores non-text files.
  • The r makes it search in all subdirectories
  • the l makes it only print the file name. Remove it and it shows the line of the matching string.

How to sort all files in a directory in bash by time?

Sort files the current directory by time

ls -1rt
  • Use 1 to show it as a list.
  • Use t to sort it according to the modification time.
  • Use r to reverse the sorting (the newest at the bottom).

Sort files in all subdirectories by time

find . -printf "%T+ %p\n" | sort | sed 's/^[^ ]* //g'
  • The command gfind . -printf “%T+ %p\n” prints the date %T+ and the path %p
  • The command sort waits until the first command is done, then sorts the files. Therefore it can be slow to use.
  • The command sed ‘s/^[^ ]* //g removes the date by replacing everything from the line beginning ^ , then all non-spaces [^ ]* until the first space.

How to make ‘find’ and ‘grep’ work on Mac?

Step 1: Install Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install findutils

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store