Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Find command options to improve searching scenarios  (Read 2001 times)

0 Members and 1 Guest are viewing this topic.

rinop

  • Guest
Find command options to improve searching scenarios
« on: August 22, 2017, 12:36:20 pm »
                                            Search directories and files using find command



Find will search any set of directories you specify for files that match the given arguments. We can search for files by name, owner, group, type, permissions, data and other criteria.

Syntax for find command :

find <directory path> <Search pattern> <action>


1.  Find command (without any options) :

root@localhost [~]# find

List all folders and files in the current directory inclusing hidden along with path.

Example :

root@ctesting [~]# find
.
./.HttpRequest
./.HttpRequest/cpanmetadb.cpanel.net
./.HttpRequest/cabundle.cpanel.net
./.HttpRequest/atom.cpanel.net
./.HttpRequest/httpupdate.cpanel.net
./.pearrc
./install.log
./.bash_logout

2.  To search files and directories using name :

root@localhost [~]# find . -name(case sensitive) filename
root@localhost [~]# find . -iname(not case sensitive) filename


Examples :

root@ctesting [~]# find . -name testme
./testme

root@ctesting [~]# find . -iname Testme
./testme


3.  To search files only out of all :

root@localhost [~]# find . -type f (f means files)

Example :

root@ctesting [~]# find . -type f -iname ‘*’
./.HttpRequest/cpanmetadb.cpanel.net
./.HttpRequest/cabundle.cpanel.net
./.HttpRequest/atom.cpanel.net
./.HttpRequest/httpupdate.cpanel.net
./.pearrc
./install.log
./.bash_logout
./hello.yml
./netpps.sh
./.cpobjcache/rim_bis_ipblocks/ipblocks.txt
./.cpobjcache/rim_bis_whitelist/whitelist.txt


4. To search directories only among all :

root@localhost [~]# find . -type d (d means directories)

Example :

root@ctesting [~]# find . -type d -iname ‘*’
./tmp
./tmp/pear
./tmp/pear/cache
./tmp/pear/temp
./r1soft-cpanel-plugin-2.0
./.MirrorSearch
./.MirrorSearch/cpanmetadb.cpanel.net
./.MirrorSearch/cpanmetadb.cpanel.net/pingtimes
./.ssh
./cpanel3-skel
./cpanel3-skel/public_ftp
./cpanel3-skel/public_html

5.  To search files ending with same extension :

root@localhost [~]# find . -name ‘*.txt’)

Example :

root@ctesting [~]# find . -name '*.txt'
./.cpobjcache/rim_bis_ipblocks/ipblocks.txt
./.cpobjcache/rim_bis_whitelist/whitelist.txt
./user.txt
./certbot/certbot-nginx/readthedocs.org.requirements.txt
6.  Search files based on permissions :

It is very useful since we can find the full permission files and directories thereby we can prevent vulnerability.

root@localhost [~]# find . -perm (parameter for permission)

Example :

root@ctesting [~]# find . -perm 0777
./test
./file1
./file2

7.  Search  files without permissions :

to search files or directories which are not having the specified permissions

root@localhost [~]# find .  ! -perm (! = not equal to)

Example :

root@ctesting [~]# find . ! -perm 0777

./.ssh/authorized_keys
./root.crt
./.my.cnf
./cpanel3-skel
./cpanel3-skel/public_ftp
./cpanel3-skel/public_html

8. Search for executable files or directories :

To search all files with executable permissions

root@localhost [~]# find / -perm /a+x

Example :
root@ctesting [~]# find / -perm /a+x
./.ssh
./install.sh
./url.sh
./cpanel3-skel
./cpanel3-skel/public_ftp
./cpanel3-skel/public_html

9. Search files/directories based on their permisisions and replace their permissions

root@localhost [~]# find . -perm 777 -exec chmod 700 {} \;




10.  Sort files/directories based on their size:

root@localhost [~]# find / -size Here M = MB, G = GB

Example :

root@ctesting [~]# find / -size 10M
/opt/xml2/lib/libxml2.a
/usr/local/cpanel/bin/postgrestool
/usr/local/cpanel/bin/regenerate_filters
/usr/local/cpanel/bin/admin/Cpanel/mx
/usr/local/cpanel/bin/mysqltool