How to find files in Bash?
What are you missing? Write a comment.
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
Example to find all files that are newer than 5 minutes in the current directory.
- The
m
inmmin
stands for modified - The
min
inmmin
is used to indicate minutes. - The
-
sign means newer than the following number of minutes.
Find files newer than 5 days?
find . -mtime -5
Example to find all files that are newer than 5 days in the current directory.
- The
m
inmtime
stands for modified. - The
time
inmtime
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
Example to find all files that are older than 5 minutes in the current directory.
- The
m
inmmin
stands for modified - The
min
inmmin
is used to indicate minutes. - The
-
sign means older than the following number of minutes.
Find files older than 5 days?
find . -mtime +5
Example to find all files that are older than 5 days in the current directory.
- The
m
inmtime
stands for modified. - The
time
inmtime
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
Example to find all files that are smaller than 4 Megabytes in the current directory.
- The
-type f
excludes everything that is not a file (directory, link). - The
-
before4M
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
Example to find all files that are larger than 4 Megabytes in the current directory.
- The
-type f
excludes everything that is not a file (directory, link). - The
+
before4M
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" .
Example to find files that contain the string “word in file” in all subdirectories.
- 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)"
Step 2: Install findutils
brew install findutils
Step 3: use gfind
instead of find