Friday, October 29, 2010

Usage of find command in redhat linux rhel5

Advertisements


Usage of find command in linux and Unix:

Find command is used to search dynamically in Linux systems. In this tutorial  we'll see various practical usage examples of find command. This examples are shown in a redhat enterprise Linux system. Same commands will work with other redhat distributions like Centos, Fedora etc and other distributions like ubuntu, debian as well.

The general syntax of find command is as follows.
#find [Directory] [criteria]

Directory is where we want to search
Criteria is what we are giving to search. Eg file name, user name, Inode number etc.

The following command will search a file with file name file_name in current working directory.
#find -name file_name


The default nature of the find is case sensitive. if u search for file abc, it will list only abc. Not ABC,Abc, abC.
To make it case insensitive use "-iname" instead of "-name"
#find -iname file_name

The following command will search a file with filename file_name in /etc and its subdirectories.
#find /etc -name file_name

The following command will search a file with filename file_name in / and its subdirectories, i.e the whole system.
#find / -name file_name

The following command will search for all the files & directories whose owner is randeep.
#find / -user randeep

The following command will search for all the files & directories whose owner is randeep and group is ibm.
#find / -user randeep -group ibm

The following command will search for all the files & directories whose owner is randeep or nibul.
#find / -user randeep -o -user nibul

The following command will search for all the files & directories whose owner is randeep and group is NOT hp.
#find / -user randeep -not -group hp

Find and Permissions
Find command can be used for finding files with specific permissions.

The following command will find the files and directories with 755 permissions.
#find / -perm 755

The following command will find the files and directories in which anyone can write.
#find / -perm +2

The following command will find the files and directories in which everyone can write.
#find / -perm -2


The following command will find the files and directories in which others can write.
#find / -perm +o+w

The following command will find the files and directories in which others can not write.
#find / -perm +o-x

Find and file sizes
Find can be used to find the files with specified size.

The followng command will list the files with size 10M [9.5-10.5]
#find / -size 10M

The followng command will list the files with size less than 10M.
#find / -size -10M

The followng command will list the files with size greater than 10M.
#find / -size +10M

Find and Access times
Find command can be associated with access times.

A file has three kind of times.

atime - access time. When file was last read
mtime - modified time. When file was last modified.
ctime - Change in metadata. When file metadata last changed.

We can see the information about the above times using stat command.
[root@server ~]# stat abc.txt
File: `abc.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 621459 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2010-10-28 16:17:15.000000000 +0530
Modify: 2010-10-28 16:17:15.000000000 +0530
Change: 2010-10-28 16:17:15.000000000 +0530
[root@server ~]#

Following command will list the files which are accessed five days ago.
#find / -atime 5

Following command will list the files which are accessed less than five days ago.
#find / -atime -5

Following command will list the files which are accessed more than five days ago.
#find / -atime +5

Following command will list the files which are modified less than 10 days ago
#find / -mtime -10

Following command will list the files which are changed metadata less than 10days ago. Changing timestamp, permissions etc.
#find / -ctime -10

Following command will list the files which are newer than abc.txt
#find -newer abc.txt

Following command will list the files which are older than abc.txt
#find -not -newer abc.txt

Find and Execute
We can execute commands on the result of find, on the flow.

Suppose we want to take the backup of all the text files in the system. The following command will search for text files and copy each to the dir /text_files.
#find / -name "*.txt" -exec cp {} /text_files \;

The following command will search for configuration files and copy each to the dir /conf_files
#find / -name "*.conf" -exec cp {} /conf_files \;

The following command will search for the file with inode number 23453 and will delete it.
#find / -inum 23453 -exec rm -rf {}\;

Other options

To search only in the partition where present working directory belongs to.
#find / -xdev -name abc.txt

To search for only the directories of name abc.
#find / -type d -name abc

To search for only the files of name abc.
#find / -type f -name abc

If you are looking for an old file its is better you use locate command. its fast.
#locate filename

if the database for the locate is old, updating it by
#updatedb&

if the locate command is not found, install the mlocate package
#yum install mlocate

1 comment:

Be nice. That's all.