Find Command in Linux/Unix With Examples | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions.

~/codeFactory$ find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:
operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2
positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xdev -ignore_readdir_race -noignore_readdir_race
tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]      -context CONTEXT

actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print 
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Valid arguments for -D:
exec, opt, rates, search, stat, time, tree, all, help
Use '-D help' for a description of the options, or see find(1)

Please see also the documentation at http://www.gnu.org/software/findutils/.
You can report (and track progress on fixing) bugs in the "find"
program via the GNU findutils bug-reporting page at
https://savannah.gnu.org/bugs/?group=findutils or, if
you have no web access, by sending email to <bug-findutils@gnu.org>.

Options:

  • -exec CMD: The file being searched which meets the above criteria and returns 0 for as its exit status for successful command execution.
  • -ok CMD : It works same as -exec except the user is prompted first.
  • -inum N : Search for files with inode number ‘N’.
  • -links N : Search for files with ‘N’ links.
  • -name demo : Search for files that are specified by ‘demo’.
  • -newer file : Search for files that were modified/created after ‘file’.
  • -perm octal : Search for the file if permission is ‘octal’.
  • -print : Display the path name of the files found by using the rest of the criteria.
  • -empty : Search for empty files and directories.
  • -size +N/-N : Search for files of ‘N’ blocks; ‘N’ followed by ‘c’can be used to measure size in characters; ‘+N’ means size > ‘N’ blocks and ‘-N’ means size < ‘N’ blocks.
  • -user name : Search for files owned by user name or ID ‘name’.
  • \(expr \) : True if ‘expr’ is true; used for grouping criteria combined with OR or AND.
  • ! expr : True if ‘expr’ is false.

~/codeFactory$ tree -a
.

├── Download
│   ├── Dir1
│   │   └── test.txt
│   ├── test.txt
│   ├── test1.txt
│   └── test1.txt~
├── Download1
│   ├── Dir1
│   │   └── test.txt
│   ├── Download
│   │   ├── Dir1
│   │   │   └── test.txt
│   │   ├── test.txt
│   │   ├── test1.txt
│   │   └── test1.txt~
│   ├── test.txt
│   └── test1.txt
├── Download2
│   ├── Dir1
│   │   └── test.txt
│   ├── test.txt
│   ├── test1.txt
│   └── test1.txt~
├── Folder1
│   ├── test.txt
│   ├── test1.txt
│   ├── test2.txt
│   ├── test3.txt
│   └── test4.txt
├── Photos
├── Photos and Videos
├── Videos
├── test.txt
├── test1.txt
├── test2.txt
├── test3.txt
├── test4.txt
└── test5.sh

12 directories, 26 files

1. Find specific files by name or extension

searched in current directory
~/codeFactory$ find . -name test4.txt 
./test4.txt
./Folder1/test4.txt

find in other directory
~/codeFactory$ find ./Download1/ -name test.txt 
./Download1/Dir1/test.txt
./Download1/Download/test.txt
./Download1/Download/Dir1/test.txt
./Download1/test.txt

2. Looking for specific files in another directory

~/codeFactory$ find ./Download1/ -name "test*"
./Download1/test1.txt
./Download1/Dir1/test.txt
./Download1/Download/test.txt
./Download1/Download/test1.txt
./Download1/Download/Dir1/test.txt
./Download1/Download/test1.txt~
./Download1/test.txt

3. Search for files by extension

~/codeFactory$ find . -name *.sh
./test5.sh

4. Find files and directories by name

~/codeFactory$ find . -name "Download*"
./Download
./Download2
./Download1
./Download1/Download

5. Find files or directories only

For files only, use the type f

~/codeFactory$ find . -name "test5*" -type f
./test5.sh

For directories only, use the type d

~/codeFactory$ find . -name "Download*" -type d
./Download
./Download2
./Download1
./Download1/Download

6. Case insensitive find command

All searches with -name switch are case sensitive and will not give results with capital letters. To get all cases, use the -iname option.

~/codeFactory$ find . -iname "TEST4*" 
./test4.txt
./Folder1/test4.txt

7. Search for a file from multiple directories

~/codeFactory$ find ./Download ./Download1 -name test1.txt -type f
./Download/test1.txt
./Download1/Download/test1.txt

8. Find multiple files with different extensions from all directories

~/codeFactory$ find . -type f `( -name "*.txt" -o -name "*.sh" )`
bash: -name: command not found
./test5.sh
./Download1/Download/Dir1/test.txt
./Download1/Download/test1.txt
./Download1/Download/test.txt
./test1.txt
./test.txt
./test3.txt
./test4.txt
./Download/Dir1/test.txt
./Download/test.txt
./Download/test1.txt
./test2.txt

9. Find files containing certain text

~/codeFactory$ find . -name "*.*" -exec grep -l -i "code" {} \;
grep: .: Is a directory
./test1.txt
./test.txt

~/codeFactory$ find . -name "*.*" -exec grep -i "code" {} \;
grep: .: Is a directory
CODE...
code factory

~/codeFactory$ find . -name "*.*" | xargs grep -in "code"
grep: .: Is a directory
./test1.txt:1:CODE...
./test.txt:1:code factory

10. Find files of a certain size – equal to 10MB

~/codeFactory$ find . -size 10M

b
c
w
k – KB
M – MB
G – GB

11. Find files larger than a specified size

~/codeFactory$ find . -size +1k

12. Find files smaller than a specified size

~/codeFactory$ find . -size -1k

13. Find files with sizes between 1-2MB

~/codeFactory$ find . -size +1M -size -2M

14. Look for directories larger than 1kb

~/codeFactory$ find . -size +1k -type d

15. Find empty files and directories.

~/codeFactory$ find . -size 0 -type f

~/codeFactory$ find . -empty -type f

~/codeFactory$ find . -empty -type d

16. Find files by age or modification time

~/codeFactory$ find . -type f -name '*.txt' -mtime +2

The -mtime +8 will look for txt files that are older than 2 days.

17. By modification date

files modified within the last 5 hours
~/codeFactory$ find . -mtime -5 -type f

directories modified within the last 2 days
~/codeFactory$ find . -mtime -2 -type d

18. Find files based on access or modification

To see files that have not been accessed within the last 5 days in the home directory.

~/codeFactory$ find . -atime +5

Files accessed exactly 5 days ago

~/codeFactory$ find . -atime 5

Accessed within the last 10 days

~/codeFactory$ find . -atime -5

19. Find files modified within the last n days

~/codeFactory$ find . -mtime -10 -type f

20. Find files modified within a specific period.

files modified between 5 and 10 days ago
~/codeFactory$ find . -type f -mtime +5 -mtime -10

21. Files and directories accessed within the last 55 minutes

~/codeFactory$ find . -amin -55

~/codeFactory$ find . -amin -55 -type f

~/codeFactory$ find . -amin -55 -type d

22. Find files with permission 777

~/codeFactory$ find . -perm 777

23. Find files writable by the owner

We will now use the “/” to looks for files writable by either their owner, or group, or others.

~/codeFactory$ find . -perm /644

The above looks for files that are writable by either their owner or group.

This returns files that are writable by either but not necessarily both. To see files, where both have writable permissions, use the – prefix.

~/codeFactory$ find . -perm -644

24. Find files owned by a user

~/codeFactory$ find . -user user

25. Find specific files owned by a user

~/codeFactory$ find . -user user -iname "*.txt"

26. Find and list files and directories together with their permissions

Find files with 644 permissions and change them to have 655 permissions

~/codeFactory$ find . -type f -perm 644 -exec chmod 655 {} \;

27. Find and copy files or directories

The command below will find the test5.sh file and copy it to the Folder1/ directory.

~/codeFactory$ find -iname test5.sh -exec cp {} Folder1/ \;

Find and copy one type of files to a directory

~/codeFactory$ find -iname "*.sh" -exec cp {} Folder1/ \;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s