You may find lots of option with "find" command. Please use "find --help" or "man find" to see all options.
I will here share two most common used case scenarios where we use the command "find".
1) To search a specific file by its name.
syntax "find Location(/) name Udaipur"
[root@RedHat7 ~]# find / -name Udaipur
/etc/Udaipur
/root/Udaipur
2) To search file on basis of it's size.
[root@RedHat7 ~]# find / -size +10M
/boot/initramfs-3.10.0-1062.el7.x86_64kdump.img
/boot/initramfs-3.10.0-1062.el7.x86_64.img
/boot/initramfs-3.10.0-1062.1.1.el7.x86_64.img
Let check above highlighted file and see if it's size is more then 10 M
[root@RedHat7 ~]# du -sh /boot/initramfs-3.10.0-1062.el7.x86_64kdump.img
11M /boot/initramfs-3.10.0-1062.el7.x86_64kdump.img -⇾ 11 MB
3) Assume a user "mukesh" left the organization so how will the administrator need to find all the data the user has created.
Let's create user mukesh
[root@RedHat7 ~]# adduser mukesh
[root@RedHat7 ~]# su - mukesh
[mukesh@RedHat7 ~]$ touch abc{1..6}
[mukesh@RedHat7 ~]$ ls
abc1 abc2 abc3 abc4 abc5 abc6
[mukesh@RedHat7 ~]$ mkdir 123{4..7}
[mukesh@RedHat7 ~]$ ls
1234 1235 1236 1237 abc1 abc2 abc3 abc4 abc5 abc6
Our task being an administrator is to find all files and directories created by user "mukesh"
Exit and go back to root
[mukesh@RedHat7 ~]$ exit
[root@RedHat7 ~]# find / -user mukesh
find: ‘/proc/10517/task/10517/fd/6’: No such file or directory
find: ‘/proc/10517/task/10517/fdinfo/6’: No such file or directory
find: ‘/proc/10517/fd/5’: No such file or directory
find: ‘/proc/10517/fdinfo/5’: No such file or directory
/var/spool/mail/mukesh
/home/mukesh
/home/mukesh/.bash_logout
/home/mukesh/.bash_profile
/home/mukesh/.bashrc
/home/mukesh/.cache
/home/mukesh/.cache/abrt
/home/mukesh/.cache/abrt/lastnotification
/home/mukesh/.config
/home/mukesh/.config/abrt
/home/mukesh/abc1
/home/mukesh/abc2
/home/mukesh/abc3
/home/mukesh/abc4
/home/mukesh/abc5
/home/mukesh/abc6
/home/mukesh/1234
/home/mukesh/1235
/home/mukesh/1236
/home/mukesh/1237
/home/mukesh/.bash_history
4) Now assume that administrator wants to find which start with abc
[root@RedHat7 ~]# find / -user mukesh -type f -name abc*
/home/mukesh/abc1
/home/mukesh/abc2
/home/mukesh/abc3
/home/mukesh/abc4
/home/mukesh/abc5
/home/mukesh/abc6
5) Now administrator wants to copy all files and folders which user "mukesh" created to a folder called "mukeshdata"
Create folder "mukeshdata" with the command mkdir
[root@RedHat7 ~]# find / -user mukesh -exec cp -rf {} mukeshdata \;
find: ‘/proc/16330/task/16330/fd/6’: No such file or directory
find: ‘/proc/16330/task/16330/fdinfo/6’: No such file or directory
find: ‘/proc/16330/fd/5’: No such file or directory
find: ‘/proc/16330/fdinfo/5’: No such file or directory
cp: cannot overwrite non-directory ‘mukeshdata/mukesh’ with directory ‘/home/mukesh’
[root@RedHat7 ~]# ls mukeshdata/
1234 1235 1236 1237 abc1 abc2 abc3 abc4 abc5 abc6
6) To find all files with with permission 775
[root@RedHat7 ~]# find / -perm 775
/run/lsm
/run/lsm/ipc
/run/netreport
/run/lock/lockdev
/var/cache/abrt-di
/var/spool/mail
7) To search and change file permission from 775 to 777
[root@RedHat7 ~]# find / -perm 775 -exec chmod 777 {} \;
I hope you will find this article will be helpful. If Yes, please like the post.
Please feel free to comment in case of any queries.
Love
Mukesh
Comments