Find is a very powerfull command for searching files and folders in Linux and Unix. This is the first find article in a serios of three.
The find command uses the brute force to search for files and folders. The locate command is faster than find, but find is more powerfull and combined with the propper parameters makes your life in Linux easier.
In this article I will show you how to search for files by names, sizes, permissions, type, and inodes.
First I will create these 3 files:
$ touch STARWARS starwars STARwars
How to find files and directories by name:
find -name “name” AND find -iname “name”
Case sensitive search:
This will match only the file named starwars:
$ find . -name "starwars"
./starwars
Incase sensitive search:
This will ignore the case. It will find starwars, STARwars, StArWarS, StarwarS etc:
$ find . -iname "starwars"
./starwars
./STARWARS
./STARwars
Do not hesitate to use wildcards.
Example: find . -iname “*string*”
How to limit the search with -mindepth and -maxdepth:
The -mindepth and -maxdepth options limit the search of the files on levels. The level 1 (mindepth 1) is the path set in the find command:
$ find /etc -mindepth 1 -name "*ssh*"
[the search starts from /etc and goes down on the directory stack]
$ find /etc -mindepth 1 -maxdepth 1 -name "*ssh*"
/etc/ssh [search only on the level 1]
$ find ~ -mindepth 1 -maxdepth 2 -iname "naboo"
/home/razvan/naboo
/home/razvan/work/naboo
[search only on the levels 1 and 2]
How to find files by type:
I will show you how to find regular files, directories, symlinks, blocks, named pipes and sockets.
Find regular files: find -type f
# find /etc -type f | tail -3
/etc/csh.logout
/etc/adjtime
/etc/group
Find directories: find -type d
$ find /etc -type d | tail -3
/etc/cron.hourly
/etc/udev
/etc/udev/rules.d
Find symlinks: find -type l
$ find /etc -type l | tail -3
/etc/rc5.d/S70pppd-dns
/etc/rc5.d/S50rsync
/etc/rc5.d/S50pulseaudio
Find other file types:
- block : find -type b
- FIFO: find -type p
- socket: find -type s
How to find files by file permissions:
Find the files that have x access for the group, and ignore the other permissions. This matches files with g=rwx, g=rx, g=x , ignoring the permissions for the user and others:
find . -perm -g=x .
I have this files:
$ ls -l
-rwxr-x--x 1 razvan razvan 0 2012-06-25 07:04 starwars
------x--- 1 razvan razvan 0 2012-06-25 07:04 STARwars
---xrwx--x 1 razvan razvan 0 2012-06-25 07:04 STARWARS
$ find . -perm -g=x
.
./starwars
./STARWARS
./STARwars
To view the permissions when using find, you have to use exec:
$ find . -perm -g=x -exec ls -l {} \; | head -4
total 0
-rwxr-x--x 1 razvan razvan 0 2012-06-25 07:04 starwars
------x--- 1 razvan razvan 0 2012-06-25 07:04 STARwars
---xrwx--x 1 razvan razvan 0 2012-06-25 07:04 STARWARS
This will match only the file with 0 permissions for users and others, and only x for group (the file with 010 permissions):
find . -perm g=x
$ find . -perm g=x
./STARwars
$ find . -perm g=x -exec ls -l {} \;
------x--- 1 razvan razvan 0 2012-06-25 07:04 ./STARwars
Find files by permissions set in octal mode: find . -perm 0171
$ find . -perm 0171
./STARWARS
$ find . -perm 0171 -exec ls -l {} \;
---xrwx--x 1 razvan razvan 0 2012-06-25 07:04 ./STARWARS
How to find files by size:
Find all the files with the size smaller than 10 Mb:
$ find -size -10M -type f
Find all the files with the size bigger than 10 Mb:
$ find -size +10M -type f
Find all the files with the size = 10 Mb:
$ find -size 10M -type f
Use K to for Kbytes and G for Gbytes.
How to find files by their inodes
I will create a file, get it’s inode and locate it with find, by the inode:
$ touch file1
$ ls -i1
806778 file1.txt
$ find -inum 806778
How to find and delete files by inodes:
This is usefull when you need to delete files with special characters in names. Read more about deleting files with weird names here.
$ find -inum 806778 -exec rm {} \;
How to find empty files:
Find empty files (with zero bytes size):
$ find . -empty
Find and delete the empty files:
$ find . -empty -exec rm {} \;
How to find files by inverting the match:
I will show you how to find all the files in a directory, except the links, and move them in the backup directory:
$ find -not -type l -maxdepth 1 -exec mv ~/backup {} \;
In this post I did not insist on the -exec option because I have written an article about it the serios of three articles about the find command.
Leave a Reply