Using the Linux find command – Usage with examples

The Linux find command is a built in powerful tool that can be used to locate and manage files and directories based on a wide range of search criteria.For example, we can find files by their name, extension, size, permissions, etc.

Common Find commands

Searching for a file by name

1
find . -name data.txt

Find files larger than 100mb in the current dir:

1
find . -xdev -type f -size +100M

Searching for a specific file in a directory

1
find ./data -name tests*

Find files by extension:

1
find . -name *.jpg

Find files or directories with certain names:

1
2
3
4
5
# find only files, we need to use the -f option:
find ./ -type f -name "results*"

# To find only directories, we need to use the -d option:
find ./ -type d -name "results*"

Find files in multiple directories

1
find ./src ./res -name app*.* -type f

Find files containing a certain text

1
find ./src -type f -exec grep -l -i "getall" {} ;

Find files by size We can even find files by different sizes. Size options are:

  • c - bytes
  • k - kilobytes
  • M - Megabytes
  • G - Gigabytes

Example

1
find / -size 10M

Find and delete specific files

1
find . -type f -name "temp*" -exec rm {} ;
Last updated on Oct 14, 2024 11:46 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy