Linux find Command Examples
Contents
Linux find commands.
empty files
Find empty (size 0) files
$ find /data -empty -ls
executable files
Find executable files
$ find /data -executable -ls
group id
Find files by group id
$ find /data -gid X -ls
user id
Find files by user id
$ find /data -uid X -ls
by group name
Find files by group name
$ find / -group name -ls
by username
Find files by user name
$ find / -user name -ls
type file
Find files (exclude dirs, devs, links etc.)
$ find / -type f -ls
by name
Find files by name (files with .txt extension for example)
$ find / -name *.txt -ls
find and execute cmd
Find files by name and search them (files with .log extension for example)
$ find / -name *.log -exec grep -iH error {} \;
user s bit
Find files with user s bit (u+s)
$ find / -perm -4000 -ls
group s bit
Find files with group s bit (g+s)
$ find / -perm -2000 -ls
others s bit
Find files with others s bit (o+s)
$ find / -perm -1000 -ls
example 1
Find and compress previous day’s logs
$ find /var/log -name *.$( date -d "1 day ago" '+%Y-%m-%d').log -exec gzip {} \;
example 2
Find and see logs files older than 30 days (dry run)
$ find /var/log -type f -mtime +30 -exec ls {} \;
example 3
Find and remove logs files older than 30 days (be careful)
$ find /var/log -type f -mtime +30 -exec rm -rf {} \;
example 4
Find files has been created under your home dir during last 5 mins
$ find ~/ -type f -cmin -5 -ls
example 5
Find files has been modified under your home dir during last 5 mins
$ find ~/ -type f -mmin -5 -ls