Back to: Linux
To find or search files in a Linux system, we use the find command. Let’s see various use of the find command:
To see the details of what the find command can do, we can use man find command in the Linux command Line.
Find Command Syntax: syntax from man find command
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
As previously mentioned, you can see the details of this command in the man page. The basic simplified syntax would be:
find [path] [options] [expression]
Find files by the file names or extensions:
Find a file with name filename.txt in the /tmp directory:
find /tmp -name filename.txt
-name: for finding case sensitive files
-iname: for finding case insensitive files
Find a file with name filename.txt in the current and the sub-directories:
find . -iname filename.txt
Find all the files in the current and sub-directories end with a particular file extension (i.g.: .txt, .html, .jpg etc): for the extension .txt
find . -name "*.txt"
Find files by File Type
If we want to a file with particular type, we can use -type option in the find command. Some files types in Linux system:
- Directory files: d
- Regular files: f
- Block files: b
- Character device files: c
- Named pipe file/a pipe file: p
- Symbolic link files: l
- Socket files: s
To find block devices, command would be:
find / -type b
Find files with type and name:
find /home/abc -type f -name "*.yaml"
Find files in Linux with file size
We can use -size option to find files with sizes. To find files those have size more than 100MB:
/home/abc -size +100MB
We can search with the following size units:
- b: 512-byte blocks
- c: bytes
- k: Kilobytes
- M: Megabytes
- G: Gigabytes
Find files in Linux based on when they were modified
We can use mtime or newermt option to specify the time since the files modified. Examples:
Find modified in last 10 days with an extension html:
find /home/userabc -name "*html" -mtime10
- – for the files modified in the last <number> of days
- + for the files modified more than the <number> of days
#Modified 10 days ago: find /home/userabc -name "*html" +mtime 10
Find files modified in last 24 hours with in the directory /home/userabc/:
find /home/userabc/ -mtime -1 -ls or find /home/userabc/ -mtime 0 or using newermt find /home/userabc/ -newermt "-24 hours" -ls
Find files modified yesterday:
find /home/userabc/ -newermt "yesterday" -ls
In summary, some commonly used find commands
Linux find file recursively based on a wildcard/pattern matching: To find a file in Linux in all directories
# . indicates the current directory. In this case, find command will find file in the current and # sub-directories. find . -iname "pattern*"
Find folder/directory in Linux: Find a directory in the whole system with type d
find / -type d -name 'directoryname'
Find files anywhere on Linux: To search in the entire Linux system use “/” as the path
find / -name "filename"
Also, see the example code shell-scripting-examples in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media