How to use the find command in Linux

Created

find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object.

Find Files Using Name
find . -name filename
Help
find --help
version information
find --version
find (GNU findutils) 4.5.11
Copyright (C) 2012 Free Software Foundation, Inc.

Finding files by name is probably the most common use of the find command. To find a file by its name, use the -name option followed by the name of the file you are searching for.

by name
find . -name filename.md
a regular file
find . -type f -name filename.md
-not option
find . -not -name *.md
Ignoring Case
find . -iname filename.md
./filetwo
./fileone

Find all the files whose permissions are 777.

Permissions
find . -type f -perm 0777 -print
Without
find / -type f ! -perm 777
Read Only Files
find / -perm /u=r
Executable Files
find / -perm /a=x
Empty Files
find /tmp -type f -empty
Find Last 50 Days Modified Files
find . -mtime 50
find . -mtime +50 –mtime -100
find / -cmin -60
find / -size +50M -size -100M
./filetwo
./fileone

Find and remove Files

single File
find . -type f -name "filename.txt" -exec rm -f {} \;
Multiple File
find . -type f -name "*.txt" -exec rm -f {} \;
Chmod
find . -type d -perm 777 -print -exec chmod 755 {} \;
Find Specific Files and Delete
find . -type f -name *.mp3 -size +10M -exec rm {} \;
./filetwo
./fileone

The syntax for the find command is as follows:

Usage: 
  find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

OPTIONS:
-H
The -H flag will only follow symbolic links while processing the command line arguments.
-L
The -L flag will cause the find command to follow symbolic links.
-P
for explicitly disabling symlink following.
Predicates:
-name pattern
tests whether the file name matches the shell-glob pattern given.
-type type
tests whether the file is a given type. Unix file types accepted include: b - block device (buffered); c - character device (unbuffered); d - directory; f - regular file; l - symbolic link; p - named pipe; s - socket; D - door.
-print
always returns true; prints the name of the current file plus a newline to the stdout.
-print0
always returns true; prints the name of the current file plus a null character to the stdout. Not required by POSIX.
-exec program [argument ...];
always returns true; run a program with the fixed arguments given and the current path of the file.
-exec program [argument ...] {} +
always returns true; run a program with the fixed arguments given and as many paths as possible (up to the maximum command-line size, like xargs). For most implementations, additional occurrences of {} mean additional copies of the name given (feature not required by POSIX).
-ok program [argument ...];
like -exec, but returns true or false depending on whether the program returns 0.
Operators:
( expr )
forces precedence;
!expr
true if expr is false;
expr1 expr2 (or expr1 -a expr2)
AND. expr2 is not evaluated if expr1 is false;
expr1 -o expr2
OR. expr2 is not evaluated if expr1 is true.

Related Tags

#Linux# #find#