Skip to content

bash
shell
find
https://stackoverflow.com/posts/45639190/timeline

list files that were changed in a certain range of time

find . -type f -newermt 20230114 \! -newermt 20230116

The lower bound in inclusive, and upper bound is exclusive, so added 1 day to it
It is recursive
In /home folder niet uitvoeren of grep gebuiken , want enorm veel config files daar

list on a name pattern

-name switch : case sensitive
-iname option : case insensitive

list-directories

list all directories and subdir’s starting in current location :
find . -type d

list all the files created or changed in the last 2 minutes

find $HOME -cmin 2

find files using metadata filters

date , aces ,/modif time , file type , etc

find . -type f ! -name "*.xml" -delete && find . -type d -empty -delete

date & time

atime : accestime mtime modification time
-mtime +8   files that are older than 8 days.
-mtime -8   files that are older than 8 days from now .
-mtime 8   
? if -type is added then hours else days ?

modified within the last 17 hours

find . -mtime -17 -type f 

modified between 2 datetimes

find . -newermt "2023-02-1 08:00:00" ! -newermt "2023-02-19 11:00:00"

directories modified within the last 10 days

find . -mtime -10 -type d 

find empty files and directories

find ./ -type f -size 0
# or
find ./ -type f –empty

“real” files only

-xdev to search only real files in the current filesystem
find / -xdev -type f -print0 | xargs -r0 grep -Hi 'your search string here'

using more complex constructs

find op mimetype

zie mimetype
the find command itself cannot test for mimetype , but is possible in combination with mimetype command
replace < somedir> with the acttual dir and the mimetype /x-shellscript with the one you want to find on

find <somedir> -type f -exec sh -c '
    case $( file -bi "$1" ) in (*/x-shellscript*) exit 0; esac
    exit 1' sh {} \; -print

You can group primaries with the -o (“or”, as opposed to the implict “and” that find applies to its primaries) primary.
The parentheses are escaped to avoid shell syntax errors and ensure they are passed as arguments to find.

find . \( \( -type f  ! -name "*.xml" \) -o \( -type d -empty \) \) -delete

compare , for study , but it is very slow

find path/to/dir/ -type f -exec sh -c '
  for f; do 
    mimetype -b "$f" | grep -Eq "application/(x-shellscript|executable)" && 
    printf "%s\n" "$f"
  done
' sh {} +

To perform some operation on the matching files (such as a chmod or chown), replace the printf command with that e.g.

find path/to/dir/ -type f -exec sh -c '
  for f; do 
    mimetype -b "$f" | grep -Eq "application/(x-shellscript|executable)" && 
    chmod u+x "$f"
  done
' sh {} +

highly recommend checking the current ownership and permissions first e.g. with ls

find path/to/dir/ -type f -exec sh -c '
  for f; do 
    mimetype -b "$f" | grep -Eq "application/(x-shellscript|executable)" && 
    ls -l "$f"
  done
' sh {} +

or