Skip to content
Search

Find

lastmod 2025-03-20created 2015-02-0626 edits

search for files in a directory hierarchy.

Command line options

  • -print: Set by default; when given, prints the final result to the screen.
  • -o or -or: Either the preceding or the following condition must match. (OR)
  • -a or -and: Both the preceding and the following conditions must be satisfied. (AND)
  • -not: Finds entries that do not match the following expression.
  • -type [filetype]: Searches by file type. The types are listed below.
    • b: Block special file (block device)
    • c: Character special file (character deice)
    • d: Directory (directory)
    • f: Regular file (file)
    • l: Symbolic link (link)
    • p: Pipe (pipe)
    • s: Socket (socket)
  • -maxdepth: Sets the maximum depth of the file path.
  • -depth: Depth of the file path.
  • -name filename: Finds by file name.
  • -atime +n: Finds files whose access time is more than n ago.
  • -atime -n: Finds files whose access time is within n.
  • -amin +n: Files last accessed more than n minutes ago.
  • -amin -n: Finds files last accessed within n minutes.
  • -mtime +n: Finds files modified more than n ago.
  • -mtime -n: Finds files modified within n.
  • -ctime +n: Finds files whose status changed more than n ago.
  • -ctime -n: Finds files whose status changed within n.
  • -perm nnn: Finds files whose permissions are nnn.
  • -size n: Finds files of size n or larger.
  • -links n: Finds files with n links.
  • -user username: Finds by user name.
  • -group groupname: Finds by group name.
  • -exec cmd arg0 arg1 arg2 ... argN \;: Runs a command for each file found. {} is replaced with the file name, and \; ends the command.
  • -execdir cmd arg0 arg1 arg2 ... argN \;: Same as -exec. Use it when you want {} in search results replaced with the file name without the directory.
  • -empty: Finds regular files of size 0 or empty directories.
  • -anewer [PATH]: Finds files accessed more recently than [FILEPATH].
  • -newer [FILE]: Searches for all the most recently updated files.

Time format

The input format for time is like -atime n[smhdw]. Here, each of [smhdw] means the following.

  • s: second
  • m: minute (60 seconds)
  • h: hour (60 minutes)
  • d: day (24 hours)
  • w: week (7 days)

For example, -atime -1h30m finds files accessed within 1 hour 30 minutes of the current time.

Example

find /path -type f -exec rm '{}' \;
Deletes all regular files.
find /path -maxdepth 1 -type d
Finds all directories (folders) at depth 1.
find . ! -name "*.png" -and ! -name "*.jpg"
Finds all files except those with the png and jpg extensions.
find /etc -type d
Searches for directories.
find /home -executable
Searches for executable files
find /home/nestgoer -atime -100 -exec ls -l {} \;
Searches for files used (accessed) within the last 100 hours.
To search in minutes, use -amin.
find /home -anewer /home/nestgoer/begin -exec ls -l {} \;
Searches for files used (accessed) more recently than the given file
find / -size +100M -exec ls -lh {} \;
Searches for all large files and processes them
find / -perm 4755 -exec ls -l {} \;
Searches for files with the given permissions
find / -empty -exec ls -l {} \;
Searches for empty files of size 0.
find / -user nestgoer -print
Searches for files owned by a specific user. (in this case, searches for user nestgoer)
touch -t 200511071100 /tmp/tstmp ; find / -newer /tmp/tstmp -print
Finds files created after a specific time.
find ./ -type f -size 0 or find ./ -type f -empty
To find all zero size files, simply use
find trains -mindepth 1 -maxdepth 1 -type d -printf '%f\n'
Prints only the names of directories located exactly in the trains folder (depth 1).
find "$find_dir" -mindepth 1 -maxdepth 1\(-type d -o -type l\)-printf '%f\n'
When using multiple conditions and passing the result to the next command, use \(</code> and <code>\).

bash mapfile input method

See Bash:Mapfile for details. Briefly:

mapfile -t vars < <(find "$find_dir" -mindepth 1 -maxdepth 1 -type d)
echo ${vars[*]}

Troubleshooting

GnuWin32 find Bug?

In GnuWin32, the following may happen.

D:\down>find . -name "*.c"
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

It says the command-line syntax is invalid; in this case, do the following.

D:\down>find . -name "*.[c]"

See also

  • fzf
  • Scooter - Interactive Find & Replace tool for the terminal
  • fd - A simple, fast, and user-friendly alternative to find

Favorite site

author Zer0 sitemap