linux : find
Tags: linux, cli, terminal
Published: 2020-06-10 02:26:44

We can search files or directories in linux using `find`

Basic usage : 

```markdown
find DIRECTORY OPTION WHAT_TO_FIND
```

## DIRECTORY :

We can specify where we want to run the find command. 

By default, if we don't specify the directory it will call the current directory `./`

```markdown
# find FILE.txt in home directory
$ find /home -name "FILE.txt"

# find all .txt files in the current directory
$ find -name "*.txt"
```

## OPTIONS :

- `-type f` : find files

```markdown
# find matched files
$ find -type f -name "FILE.txt"
```

by default find is finding files. we can also use `-type f` for file type

- `-type d` : find a directories

```markdown
# find matched directories
$ find -type d -name "DIRECTORY"
```

- `-iname` : find with case insensitive

```markdown
# find a FILE.txt or file.txt
$ find -iname "FILE.txt"
```

- `-print` : display the path name

    We can use `-print` option or not with the sam result.

```markdown
# find files
$ find -name "FILE.txt" -print

# find directories
$ find -type d -name "DIRECTORY"
```

- `-size` : find by size

```markdown
# find files smaller than 10MB
$ find -size -10M

# find files larger than 10MB
$ find -size +10M

# find files exactly 3 10MB
$ find -size 10M

# find files between 10MB and 1GB
$ find -size +10M -size -1G
```

- `-empty` or `-size 0` : find empty files or directories

```markdown
# find an empty files
$ find -type f -empty

# find an empty directories
$ find -type d -size 0
```

- `-exec CMD` :  execute a command after

```markdown
# execute grep after find is done
$  find -name "FILE.txt" -exec grep "KEYWORD" {} \;
```

- `-delete` or `-exec rm {} \;` : delete matched files or directories

```markdown
# delete an mathed files
$ find -name "*.bak" -delete

# delete an empty directories
$ find -type d -size 0 -exec rmdir {} \;
```

- `not` or `!` : exclude the matched files or directories

```markdown
# find all except the matched files
$ find -not -name "FILE.txt"
```

- `-mtime` : find by modified time

    we also can use `-mmin` if we want to specify the time interval to minutes

```markdown
# find files modified within the last 3 days
$ find -name "FILE.txt" -mtime -3

# find files modified more than 3 days
$ find -name "FILE.txt" -mtime +3

# find files modified exactly 3 days
$ find -name "FILE.txt" -mtime 3

# find files modified between 3 and 10 days
$ find -name "FILE.txt" -mtime +3 -mtime -10
```

- `-atime` : find by accessed time

    we also can use `-amin` if we want to specify the time interval to minutes

```markdown
# find files accessed within the last 3 days
$ find -name "FILE.txt" -atime -3

# find files accessed more than 3 days
$ find -name "FILE.txt" -atime +3

# find files accessed exactly 3 days
$ find -name "FILE.txt" -atime 3

# find files accessed between 3 and 10 days
$ find -name "FILE.txt" -atime +3 -atime -10
```

- `-ctime` : find by changed time

    we also can use `-cmin` if we want to specify the time interval to minutes

```markdown
# find files changed within the last 3 days
$ find -name "FILE.txt" -atime -3

# find files changed more than 3 days
$ find -name "FILE.txt" -atime +3

# find files changed exactly 3 days
$ find -name "FILE.txt" -atime 3

# find files changed between 3 and 10 days
$ find -name "FILE.txt" -atime +3 -atime -10
```

- `-perm` : find by permission

```markdown
# find files with permission 777
$ find -perm 777
```

- `-user` : find by user access

```markdown
# find matched files with user root
$ find -user root -name "FILE.txt" 
```

- `-group` : find by group access

```markdown
# find matched files with group developer
$ find -group developer -name "FILE.txt"
```

- find hidden files

```markdown
$ find -name ".*"
```