$ find
Search for files in a directory
hierarchy
Syntax: $ find
Some of the more commonly used options include –
-name
The –name option simply searches for files that
have the specified name. It can
incorporate wildcards as long as they are enclosed in quotation marks.
For eg – this command will return all the files that start with the file name - 'file'
located anywhere beneath the /root/good directory.
$ find / -name ‘file*’
Output:
[root ~/good 12:44:58] # find /root/good -name 'file*'
/root/good/file3.sh
/root/good/file1.sh
/root/good/file2.sh
-links
Search for any file with the
specified number of links attached to it.
For eg – this command will return all files with 6 links in /home/che/test and its
subdirectories.
$ find /home/che/test –links 6
Output:
[root ~/good 16:21:03] # ls -l /home/che/test
total 48
drwxr-xr-x 2 che solaris 4096 Aug 24 15:40 dir1
drwxr-xr-x 2 che solaris 4096 Aug 24 15:40 dir2
drwxr-xr-x 2 che solaris 4096 Aug 24 15:40 dir3
-rw-r--r-- 6 che solaris 0 Aug 24 16:19 one
-rw-r--r-- 6 che solaris 0 Aug 24 16:19 one_hlink1
-rw-r--r-- 6 che solaris 0 Aug 24 16:19 one_hlink2
-rw-r--r-- 6 che solaris 0 Aug 24 16:19 one_hlink3
-rw-r--r-- 6 che solaris 0 Aug 24 16:19 one_hlink4
-rw-r--r-- 6 che solaris 0 Aug 24 16:19 one_hlink5
[root ~/good 16:21:16] # find /home/che/test -links 6
/home/che/test/one_hlink3
/home/che/test/one_hlink1
/home/che/test/one
/home/che/test/one_hlink5
/home/che/test/one_hlink4
/home/che/test/one_hlink2
-type
Search for files of a certain
type.
Syntax: $ find path –type <b/c/d/p/f/l/s>
Where -
b = Refers to block-special devices.
c = Refers to character-special devices.
d = Refers to directories.
p = Refers to named pipes.
f = Refers to ordinary files.
l = Refers to symbolic links.
s = Refers to sockets.
Output
[root ~/good 14:38:01] # find -type d
.
./dir2
./dir5
./dir4
./dir3
./dir1
./test
[root ~ 11:58:40] # find /root –type f ‘good’
/root/good
/root/test/good
Note: By default, if the path is not specified on the ‘find’
command to search, then it search the current directory
As Unix are case sensitive, to ignore the case,
-iname option is used.
This can be useful if we are unsure of the exact
case of a file name we are looking for.
Output:
[root ~/good
12:52:22] # find -iname 'File*'
./file3.sh
./file1.sh
./file2.sh
To find the files that are modified recently. For
example to find the files that are modified before/earlier/last 5 minutes -
# find -mmin -5
Where -
-mmin = modified minutes
Output:
[root ~/good 13:03:31] # ls -lh file*
-rw-r--r-- 1 root root 53 Aug 24 13:02 file1.sh
-rw-r--r-- 1 root root
0 Aug 24 12:44 file2.sh
-rw-r--r-- 1 root root
0 Aug 24 12:44 file3.sh
[root ~/good 13:03:42] # echo "Updating the file
named file2.sh for testing purpose" >> file2.sh
[root ~/good 13:04:04] # find -mmin -5
./file1.sh
./file2.sh
To find the files that are modified more than 5
min or older than that -
#find -mmin +5
Output:
[root ~/good 13:19:48] # find -mmin +5
.
./test_split_fileaj
./edit_file
./test_split_fileax
./test_mod_script_file
./du
./man
(Output Truncated...)
# find -mmin 5
Displays the files that are exactly modified before
5 min.
To find the files that are modified 5 days back.
For example to find the files that are modified before/earlier/last 5 days -
# find -mtime -5
Output:
[root ~/good
13:28:40] # ls -lht | find -mtime -5
.
./file3.sh
./file1.sh
./file2.sh
./test
./test/test_file510:54:00-20_08_13
./test/example_file10:52:49-20_08_13
./test/test_file210:54:00-20_08_13
./test/test_file110:54:00-20_08_13
./test/test_file410:54:00-20_08_13
./test/test_file310:54:00-20_08_13
Where -
-mtime = number of days.
Note:
1.
Normally 'time' stands for 24 hour, hence here 5 means, 5 times of 24
(5x24)
2. Alike -mmin -5, -mmin +5 and -mmin 5, -mtime -5, -mtime +5 and -mtime
5 can be used.
3. Alike -mmin, -cmin and -amin do
work.
4. Alike -mtime, -ctime and -atime do work.
To list out the empty files (file without content)
Output:
[root ~/good 13:44:30] # ls -lh fname?
-rw-r--r-- 1 root root 0 Aug 24 13:44 fname1
-rw-r--r-- 1 root root 0 Aug 24 13:44 fname2
-rw-r--r-- 1 root root 0 Aug 24 13:44 fname3
-rw-r--r-- 1 root root 0 Aug 24 13:44 fname4
-rw-r--r-- 1 root root 0 Aug 24 13:44 fname5
[root ~/good 13:44:38] # find -empty
./fname1
./fname4
./fname5
./fname3
./fname2
-size
Searches
for any files approximately equal to a specified size, which is specified on
the argument. By default, the size
indicates the number of 512 byte blocks of information. You can specify a size in bytes by appending
the c option or in kilo bytes by appending the k option.
To find the files with the size 10M.
# find -size 10M
Output:
[root ~/good 14:28:49] # find -size 10M
./new_file
./new_file2
./output.dat
To find the files with the size 10485760 bytes
Output:
[root ~/good 14:30:21] # find -size 10485760b
[root ~/good 14:30:31] # find -size 10485760c
./new_file
./new_file2
./output.dat
To find the files with the size 10240 kilo bytes
Output:
[root ~/good 14:33:41] # find -size 10240k
./new_file
./new_file2
./output.dat
Where -
M = mega bytes
c = bytes
k = kilo bytes
To list out only the directory.
# find -type d
Output:
[root ~/good 14:38:01] # find -type d
.
./dir2
./dir5
./dir4
./dir3
./dir1
./test
To find only the regular files with the size 10M
Output:
[root ~/good 14:37:48] # find -type f -size 10M
./new_file
./new_file2
./output.dat
To find the files owned by the user specified -
'che'
Output:
[root ~/good 14:45:17] # find /home -user che
/home/che
/home/che/.bashrc
/home/che/.bash_history
/home/che/.bash_profile
/home/che/.bash_logout
/home/che/test
/home/che/test/three
/home/che/test/one
/home/che/test/two
/home/che/.sh_history
To find the files owned by the group specified -
Output:
[root ~/good 14:52:31] # find /home -group solaris
/home/che/Unix
/home/che/world
/home/che/to
/home/che/of
/home/che/the
/home/che/welcome
/home/vcs1
/home/vcs1/.bashrc
/home/vcs1/.bash_profile
/home/vcs1/.bash_logout
[root ~/good 14:52:45] # ls -lh /home/che | grep solaris
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 of
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 the
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 to
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 Unix
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 welcome
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 world
To find the files having the permission 777 on the
path /home -
Output:
[root ~/good 14:56:15] # find /home -perm 777
/home/che/to
/home/che/of
/home/che/the
[root ~/good 14:56:29] # ls -l /home/che
total 24
-rwxrwxrwx 1 che solaris 0 Aug 24 14:52 of
-rwxrwxrwx 1 che solaris 0 Aug 24 14:52 the
-rwxrwxrwx 1 che solaris 0 Aug 24 14:52 to
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 Unix
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 welcome
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 world
To find and delete only the files owned by the
specified group - 'vcs'
Output:
[root ~/good 15:01:45] # ls -lh /home/che
total 24K
-rwxrwxrwx 1 che solaris 0 Aug 24 14:52 of
-rwxrwxrwx 1 che vcs
0 Aug 24 14:52 the
-rwxrwxrwx 1 che vcs
0 Aug 24 14:52 to
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 Unix
-rw-r--r-- 1 che vcs
0 Aug 24 14:52 welcome
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 world
[root ~/good 15:01:52] # find /home -group vcs -delete
[root ~/good 15:02:09] # ls -lh /home/che
total 12K
-rwxrwxrwx 1 che solaris 0 Aug 24 14:52 of
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 Unix
-rw-r--r-- 1 che solaris 0 Aug 24 14:52 world
To find the files owned the specific user -
'che', then execute the command - 'wc
-l' and to display the same.
Output:
[root ~/good 15:07:06] # find /home -user che -exec wc -l
{} \;
wc: /home/che: Is a directory
0 /home/che
107 /home/che/Unix
8 /home/che/.bashrc
58 /home/che/world
21 /home/che/.bash_history
114 /home/che/of
12 /home/che/.bash_profile
3 /home/che/.bash_logout
22 /home/che/.sh_history
To find and delete the files owned by the specific
user 'che', delete the files getting confirmation -
# find /home/che/test -user che -type f -ok rm {} \;
Output:
[root ~/good 15:40:51] # ls -lh /home/che/test
total 36K
drwxr-xr-x 2 che solaris 4.0K Aug 24 15:40 dir1
drwxr-xr-x 2 che solaris 4.0K Aug 24 15:40 dir2
drwxr-xr-x 2 che solaris 4.0K Aug 24 15:40 dir3
-rw-r--r-- 1 che solaris 0 Aug 24 15:40 file1
-rw-r--r-- 1 che solaris 0 Aug 24 15:40 file2
-rw-r--r-- 1 che solaris 0 Aug 24 15:40 file3
[root ~/good 15:40:57] # find /home/che/test -user che
-type f -ok rm {} \;
< rm ... /home/che/test/file1 > ? y
< rm ... /home/che/test/file2 > ? y
< rm ... /home/che/test/file3 > ? y
[root ~/good 15:41:40] # ls -lh /home/che/test
total 24K
drwxr-xr-x 2 che solaris 4.0K Aug 24 15:40 dir1
drwxr-xr-x 2 che solaris 4.0K Aug 24 15:40 dir2
drwxr-xr-x 2 che solaris 4.0K Aug 24 15:40 dir3
Where -
-ok = Key word prompts for confirmation before
performing the delete/remove operation as requested.
Sir,Amazing Work for "Find" command .. I used "find" command all the time one way to search the folder ..Now you have tried lot way with Symbols .. very useful for everyone .. thanks for updating ..
ReplyDeletetwo weeks before I had struck one issue for find command .. that time i did lot search in google but couldn't find .. now I got some points from "Find" command ... let me check ... thanks Sir
ReplyDelete-cmin is not working in my Solaris 5.10, Could u pls help me in this ?
ReplyDeleteHi,
DeleteI too worked with same version of Slorais 10. Detailed output would help to understand the issue.