Featured

    Featured Posts

Find command in linux

Do you want to locate any file in Linux ?
Are you tired of manually finding files in linux system ?

No worries.. We have a great command called "find".
Lets make use of it.... 🙂 



          
Command :    find
Syntax :          find   [dir]    [criteria]    [pattern]   [action]   

Here...
            [dir]       -  specifies path under which you are searching for.
                             ex:    / 
                                     /root
                                     /bin
                                     /home/user
⇨ If you dont enter any thing in a dir field , then command will look under current working directory. [ same as  if you specify  ( . ) dot - current working directory ]

             [criteria]  -   Specifies WHAT to search 
                                ex:   -name       →   Name of a file 
                                        -iname      →   Ignore case of name
                                        -user         →   To find files related to username
                                        -group       →   To find files related to group
                                        -uid           →   To find files related to user id
                                        -gid           →   To find files related to group id
                                        -perm        →   To find files based on file permission
                                        -atime        →   To find files based on access time
                                        -mtime       →   To find files based on Modified time
                                        -ctime        →   To find files based on Creation time
                                        -size          →   To find files based on Size 
                                        -inum         →   To find files based on Inode number
                                          
            [pattern]  -  The pattern that you would like to search/find.

            [actions]  -  Actions to be applied on the output of find.
                               like rm , cp , mv etc.,

--------------------------------------------------------------------------------------------------------------------------------------------------------

Now we will go through some examples of find commands which will be most helpful in realtime.

-name
To find  “flower.jpg”  file under  /

[root@localhost Desktop]# find  /  -name  flower.jpg
/root/Desktop/flower.jpg


-iname
If you are not sure whether the file name is in capitals or small letters:

[root@localhost Desktop]# find /root -iname FLowEr.jpg
/root/Desktop/flower.jpg


-user
To search the files related to user “kiran” :

[root@localhost ~]# find /home/ -user kiran
/home/kiran
/home/kiran/.bashrc
/home/kiran/flower.jpg
/home/kiran/mydir
/home/kiran/scripts
/home/kiran/contacts.txt



If you specify multiple criteria, then by default it will consider as "and"  [ the two criteria should match]

and
without any option
using -a

[root@localhost ~]# find /home/ -user kiran  -iname flower*
/home/kiran/flower.jpg


[root@localhost ~]# find /home/ -user kiran  -a -iname flower*
/home/kiran/flower.jpg



or
using  -o (or) option

[root@localhost ~]# find /home/kiran/ -name "*.jpg"   -o  -name "*.txt"
/home/kiran/flower.jpg
/home/kiran/contacts.txt


  
Both and & or
using-a (and ) &   -o (or) option

[root@localhost ~]# find /home/  -user kiran -a \( -iname "*.txt" -o -iname "*.jpg" \)
/home/kiran/flower.jpg
/home/kiran/contacts.txt


-size
To find the files which has exactly 1kb size
To find the files which has more than 3kb size
To find the files which has less than 3kb size
[kiran@localhost ~]$ find -size 1K
./.bashrc
./.bash_logout
./.bash_profile

[kiran@localhost ~]$ find -size +3M
./mydir
./.mozilla
./.mozilla/extensions
./.mozilla/plugins
./.gnome2
./scripts

[kiran@localhost ~]$ find -size -3M
./.bashrc
./flower.jpg
./.bash_logout
./hi
./.bash_profile
./contacts.txt


-perm
To find the files which has more than 666 permissions
To find the files which has less than 555 size
To find the files which has less than 3kb size

[kiran@localhost ~]$ find . -perm +600
./scripts
./.bash_logout
./hi
./.bash_profile
./contacts.txt


[kiran@localhost ~]$ find . -perm -555
./mydir
./.mozilla
./.mozilla/extensions
./.mozilla/plugins
./.gnome2


[kiran@localhost ~]$ find . -perm 777
./contacts.txt

















-mtime
To find file which was older than 3 days

[root@localhost ~]# find / -mtime 3
/bin/uname
/bin/vi
/bin/gzip
/bin/raw



[action]

Command can be executed on founded files


commands must be preceded with -exec (or) -ok

 -ok will prompts before actiong on each line

⇒ command must end with \;

⇒ we can use {} as an file name place holder


Ex:

-ok
Using –ok option
[kiran@localhost ~]$ find . -name "file*" -ok rm -rf {} \;
< rm ... ./file3 > ? y
< rm ... ./file1 > ? y
< rm ... ./file2 > ? y

-exec
To delete flower file of user kiran

[root@localhost ~]# find /home/kiran/ -name "flower*" -exec rm -f {} \;
[root@localhost ~]#


To delete flower file under /opt which are older than 30 days

[root@localhost ~]# find /opt/  -mtime +30  -exec rm -rf {} \;
[root@localhost ~]#


To copy files to other directory

[root@localhost ~]# find /home/kiran/ -name "flower*" -exec  cp {}  /tmp  \;
[root@localhost ~]#





--------------------------------------------------------------------------------------------------------------------------------------------------------


Hope you guys got an idea of using "find" command.


Thanks! 🙂








www.CodeNirvana.in

COPYRIGHT © 2017. Powered by Blogger.

Featured Posts

Featured Posts

Featured Posts

Popular Posts

Translate

Total Page Views

blog counter
Copyright © gskLinux | Linux Tutorials | Designed By | Gnanasekhar