Linux Find Command Examples
The Linux find command is a very useful and powerful command in linux system for search and find files or directories. How to use linux find command to find a file under linux operating system. How to use find command to search the smallest and largest files? this post will guide you how to use this command. I will show you some linux find command examples.
NAME
find – search for files in a directory hierarchy
SYNOPSIS
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression
Examples:
#1 find all files in the current directory
type:
find
outputs:
[root@devops ~]# find . ./.bash_profile ./.viminfo ./.tcshrc ./install.log ./.bash_logout ./install.log.syslog ./.xauthbqznZZ ./.cshrc ./.bash_history ./.gconfd ./.gconfd/saved_state ./anaconda-ks.cfg ./.bashrc
#2 find specific files in current directory
type:
find . -name foo.txt
outputs:
[root@devops ~]# find . -name foo.txt ./foo.txt #3
#3 find files under specific directory
type:
find /root -name foo.txt
#4 find files using name and ignoring case using linux find command
type:
find /root -name foo.txt
outputs:
[root@devops ~]# find /root -iname foo.txt /root/foo.txt /root/Foo.txt
#5 find only file in the current directory
type:
find . -type f -name foo.txt
outputs:
[root@devops ~]# find . -type f -name foo.txt ./foo.txt
#6 Limit depth of directory traversal to find files
type:
find . -maxdepth 2 -name "*.txt"
or
find . -maxdepth 1 -name "*.txt"
outputs:
[root@devops ~]# find . -maxdepth 2 -name "*.txt" ./foo.txt ./alog/foo.txt ./Foo.txt [root@devops ~]# find . -maxdepth 1 -name "*.txt" ./foo.txt ./Foo.txt
#7 find only directories in the current directory using linux find command
type:
find . -type d -name alog
outputs:
[root@devops ~]# find . -type d -name alog ./alog
#8 find files under multiple directories
type:
find /opt /usr/root -type f -name foo.txt
#9 find files with different extensions
If you want to search files that matching “*.txt” or “*.cfg”, type the following command:
find . -type f \( -name "*.txt" -o -name "*.cfg" \)
outputs:
[root@devops ~]# find . -type f \( -name "*.txt" -o -name "*.cfg" \) ./foo.cfg ./foo.txt ./alog/foo.txt ./anaconda-ks.cfg ./Foo.txt
#10 find files that don’t match a pattern
If you want to find all files that not matching “.txt” in the current directory, type:
find . -type f -not -name "*.txt"
outputs:
[root@devops ~]# find . -type f -not -name "*.txt" ./.bash_profile ./foo.cfg ./.viminfo ./.tcshrc ./install.log ./.bash_logout ./install.log.syslog ./.xauthbqznZZ ./.cshrc ./.bash_history ./.gconfd/saved_state ./anaconda-ks.cfg ./.bashrc
#11 find all hidden files using linux find command
type:
find . -type f -name ".*"
outtpus:
[root@devops ~]# find . -type f -not -name "*.txt" ./.bash_profile ./foo.cfg ./.viminfo ./.tcshrc ./install.log ./.bash_logout ./install.log.syslog ./.xauthbqznZZ ./.cshrc ./.bash_history ./.gconfd/saved_state ./anaconda-ks.cfg ./.bashrc [root@devops ~]# find . -type f -name ".*" ./.bash_profile ./.viminfo ./.tcshrc ./.bash_logout ./.xauthbqznZZ ./.cshrc ./.bash_history ./.bashrc
#12 find all files with certain permission
if you want to find all files with a 0777 permission, you can use “perm” option to find command, type:
find . -type f -perm 0777
outputs:
[root@devops ~]# find . -type f -perm 0777 ./install.log ./foo.txt
#13 find all readonly files
type:
find . -maxdepth 1 -perm /u=r
output:
root@devops ~]# find . -maxdepth 1 -perm /u=r -type f ./.bash_profile ./foo.cfg ./.viminfo ./.tcshrc ./install.log ./.bash_logout ./foo.txt ./install.log.syslog ./.xauthbqznZZ ./.cshrc ./.bash_history ./anaconda-ks.cfg ./.bashrc ./Foo.txt
#14 find all executable files
type:
find . -maxdepth 1 -perm /a=x
outputs:
[root@devops ~]# find . -maxdepth 1 -perm /a=x . ./install.log ./foo.txt ./alog ./.gconfd ./.config ./.gconf
#15 find files that belonging to a particular user
type:
find . -type f -user root
outputs:
[root@devops ~]# find -type f -user root ./.bash_profile ./foo.cfg ./.viminfo ./.tcshrc ./install.log ./.bash_logout ./foo.txt ./install.log.syslog ./.xauthbqznZZ ./.cshrc ./.bash_history ./alog/foo.txt ./.gconfd/saved_state ./anaconda-ks.cfg ./.bashrc ./Foo.txt
#16 find files that belonging to a group
type:
find -type f -group root
outputs:
[root@devops ~]# find -type f -group root ./.bash_profile ./foo.cfg ./.viminfo ./.tcshrc ./install.log ./.bash_logout ./foo.txt ./install.log.syslog ./.xauthbqznZZ ./.cshrc ./.bash_history ./alog/foo.txt
#17 find files by modification time
type:
find . mtime 1 #files were modified in last 24 hours find . mtime -7 # files were modified in last 7 days
#18 find files in given size
such as: if you want to find all 10M files ,type:
find . -size 10M
#19 find files in a size range
if you want to find files that size are greater that 10MB and less than 20MB, type:
find . -size +10M -size -20M
#20 find files and remove all finded files
type:
find . -type f -name "*.txt" -exec rm -f {}\;