-->

Linux Find Command Examples

Author Piyush Gupta

find is a Linux command line tool to search files and directories in the file system. The find command works much fast than any other command. It provides a large number of options for more specific search. It also supports wildcard characters.


Every Linux user must read this article and understand the uses of the find command. This command is very useful in your daily tasks. This article will help you to understand find command and its uses in Linux system.

Syntax: To search a file or directory under specified filesystem.
find /search/in/dir -name filename
Explanation:
find => command line tool
/search/in/dir => Directory name where to start search
-name => Switch to specify filename
filename => File or Directory name

Find files by Name


Use -name option to find a file with name “hello.txt” under root (/) file system.
find / -name hello.txt

Find files by Type

Search for the file (not directory) named “backup.zip” in entire file system. Use -type f to specify search for files and ignore direcories.
find / -type f -name backup.zip

Search directory only

Search for the directory (not file) named “backup” in entire file system. Use -type d to specify search for directory and ignore files.
find / -type d -name backup

Find files by Size

Search all files systemwide, which are greater than or equal 10MB with find command
find / -type f -size +10M
And this command will search all files in system which are less than 10MB.
find / -type f -size -10M
-size: switch are used for searching file on bais of there size. Plus ( + ) is used to greater than size and minus ( – ) sign is used for less than size.
like: +100MB, -50KB, +1GB etc…

Find files by Time


Search all files which modification time is more than 30 days.
find / -type f -mtime +30
Search all files which modification time is less than 30 days.
find / -type f -mtime -30

Find files by User/Group

Find command also provides search based on user and group ownership. like:

Search all .txt files own by user bob.
find  / -user bob -name "*.txt"
Search all .txt files with group ownership of root.
find  / -group root -name "*.txt"
You can combine both to more specific search to files with owner bob and group ownership of the root.
find  / -user bob -group root -name "*.txt"

Find files by Permissions

Search all files which are having 777 permissions under /var/www directory tree. This can be helpful for the security audit.
find . -perm 777

Find files by Inode Number

This command is used to search files on basis of their inode number. -inum is used for this search.
find / -inum 1532
If you want check inode number of a file using below command. The first field of output is an inode number
ls -li piyush.txt

30878 -rw-r--r--. 1 root root 0 Mar 22 17:20 piyush.txt

Find Empty files

This command is very useful to search empty files and directories. It is useful to the cleanup system with empty files and directories.
$ find / -empty

Find files by File Types

Search all block special files available under / filesystem.
find / -type b
Other file type options are as below:
b – block (buffered) special
c – character (unbuffered) special
d – directory
p – named pipe (FIFO)
f – regular file
s – socket
l – symbolic link; this is never true if the -L option or the -follow option is in effect unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.

No comments:

Post a Comment