Skip to main content

Find Command. Uses in Diffrent Cases..


find commands examples with explanation

find command is very much powerful command which can do good work when its needed to find files with conditions. Find command  is useful when finding files with complex requirement such as size, permissions etc. Suppose we want to find a file which is a regular file and its size is more than 1GB and its accessed more than 90 days back and its owner is none and then delete it. This entire requirement is done with single command without even writing a shell script. Let’s see how we can use find command from basics to advanced in this post.
find command can find files according to
1) File names

2) File types

3) Permissions

4) Owners

5) Modified date and time

6) Size 
Advanced finds command usages
1) Mix of all the above things
2) AND operator
3) Search for files and execute commands on them
           a) chown, chmod, grep, ls, rm, mv, cp,md5sum

4) Multiple execute commands

5) Search for multiple files
            a) With different files with same extension
            b) Same file With different extensions
6) Search in multiple locations
            a) Exclude one location
            b) Search in multiple locations
         
7) OR( –o ) operator
  ! Negation operator
9) Linux find command with Regular Expressions
10) Linux find commnd practical examples

Basics of file/folders search using find command

 find files with name

Syntax:
find path options filename
Example1: find all the files in /home with name test.txt. Here –name is used to specify the filename.
find /home –name test.txt
Example2: find the files whose name is test.txt and in present working directory
find . –name test.txt
Or
find –name test.txt
Example3: find all the files whose name contains both capital letters and small letters in it.
find /home –iname test.txt
-iname option is used to mention ignore the case sensitivity of a file.
Search for files depending on their File types:
Example4: Search for only directories whose name is var in / directory
find / -type d –name var
Example5: Search for an mp3 files whose name is temp.mp3
find / -type f –name temp.mp3
Below are the file types supported by find command, to know more about file types in Linux/Unix please have a look at our other post on File types.
Sl.noSymbolType
1fRegular file
2dDirectory file
3bBlock file
4cCharacter file
5pPipe file
6lSymbolic link file
7sSocket file.

Search for files depending on their Permissions

Example6:Search for a file name test.txt and its permissions are 775 in a given box
find / -perm 775 –name test.txt
Example7: How about searcing files with SUID bit set and file permissions are 755?
find / -perm 4755
Example8:How can i find SGID bit set files with 644 permissions?
find / -perm 2644
Example9: How can i find Sticky bit set files in my system with permissions 551?
find / -perm 1551
Example10:Search for all the files whose SUID bit is set
find / -perm /u=s
Example11: Search for all the files whose SGID bit is set
find / -perm /g+s
Note: We can use = or + interchangeably to check if a permissions is set or not as shown in above two examples.
Example12: Search for all the files  whose StickyBit is set
find / -perm /o=t
Example13: Search for all the files whose owener permissions is read only.
find / -perm /u=r
Example14:Search for all the files which have user, group and others with executable permissions
find / -perm /a=x
To know about more on the permissions you have look at our other posts on chmod command.

Search according to Owners and group owners.

Example15: Search for all the files with name test.txt and the owner of this file is Surendra
find / -user Surendra –name test.txt
Example16: find all the files whos name is test.txt and owned by a group called redcluster
find / -group redcluster –name test.txt
to know more about owners and groups you have to look at our previous post on chown command.

Search according to Modified date and time.

Below is the matrix which give you brief idea on how to search according to modified date, accessed date etc.
Sl. No-ctime-mtime-atime
+90File status changed more then 90 days backModified more than 90 days backAccessed more than 90 days back
90File status changed exactly 90 days backModified exactly 90 days backAccessed exactly 90 days back
-90File status changed less than 90 daysModified less than 90 daysAccessed less than 90 days back
Example17: Search for a file: test.txt whose file status is changed more than 90 days back
find / -ctime +90 –name test.txt
Example18: Search for all the files which are modified exactly 90 days back
find / -mtime 90
Example19: Search for all the files with name test.txt which is accessed less than 90 days
find / -atime -90
Example20: find all the files which are modified more than 90 days back and less than 180 days
find / -mtime +90 –mtime -180
Below is the matrix which gives you brief idea on how to search according to modified time, accessed time in minutes etc.
Sl. No-cmin-mmin-amin
+30File status changed more then 30 mins backModified more than 30 mins backAccessed more than 30 mins back
30File status changed exactly 30 mins backModified exactly 30 days backAccessed exactly 30 mins back
-30File status changed less than 30 minsModified less than 30 minsAccessed less than 30 mins

Example21: find all the files changed less than 30mins
find / -cmin -30
Example22: find all the files modified exactly 30 mins back
find / -mmin 30
Example23: find all the files accessed more than 30 mins back
find / -amin +30
Example24: find all the files which are modified more than 5mins back and less than 25mins
find / -mmin +5 –mmin -25
Example25: I have new file called test.txt which is just created, now I want to get all the files which are created later this file creation.
find / -newer test.txt

Search for files/folders depending on the size with –size option

Sl.no+1010-10
c for bytes(8 bits)Search for files more than 10c sizeSearch for files exactly 10b sizeSearch for files less than 10b size
k for kilobytesSearch for files more than 10k sizeSearch for files exactly 10k sizeSearch for files less than 10k size
M for MegabytesSearch for files more than 10M sizeSearch for files exactly 10M sizeSearch for files less than 10M size
G for GigabytesSearch for files more than 10G sizeSearch for files exactly 10G sizeSearch for files less than 10G size

Example26: Search for files whose size is more than 10bytes
find / -size +10c
Example27: Search for files which are exactly 10kb in /opt folder
find /opt –size 10k
Example28: Search for files which are less than 10MB in /var folder
find /var –size -10M
Example29: Search for files which are more than 1GB size in /usr folder
find /usr –size +1G
Example30: find all the empty files in my system
find / -size 0k

Comments

Popular posts from this blog

Docker Container Management from Cockpit

Cockpit can manage containers via docker. This functionality is present in the Cockpit docker package. Cockpit communicates with docker via its API via the /var/run/docker.sock unix socket. The docker API is root equivalent, and on a properly configured system, only root can access the docker API. If the currently logged in user is not root then Cockpit will try to escalate the user’s privileges via Polkit or sudo before connecting to the socket. Alternatively, we can create a docker Unix group. Anyone in that docker group can then access the docker API, and gain root privileges on the system. [root@rhel8 ~] #  yum install cockpit-docker    -y  Once the package installed then "containers" section would be added in the dashboard and we can manage the containers and images from the console. We can search or pull an image from docker hub just by searching with the keyword like nginx centos.   Once the Image downloaded we can start a contai

Remote Systems Management With Cockpit

The cockpit is a Red Hat Enterprise Linux web-based interface designed for managing and monitoring your local system, as well as Linux servers located in your network environment. In RHEL 8 Cockpit is the default installation candidate we can just start the service and then can start the management of machines. For RHEL7 or Fedora based machines we can follow steps to install and configure the cockpit.  Following are the few features of cockpit.  Managing services Managing user accounts Managing and monitoring system services Configuring network interfaces and firewall Reviewing system logs Managing virtual machines Creating diagnostic reports Setting kernel dump configuration Configuring SELinux Updating software Managing system subscriptions Installation of cockpit package.  [root@rhel8 ~] #  dnf   install cockpit cockpit-dashboard  -y  We need to enable the socket.  [root@rhel8 ~] #  systemctl enable --now cockpit.socket If firewall is runnin

Containers Without Docker on RHEL/Fedora

Docker is perfectly doing well with the containerization. Since docker uses the Server/Client architecture to run the containers. So, even if I am a client or developer who just wants to create a docker image from Dockerfile I need to start the docker daemon which of course generates some extra overhead on the machine.  Also, a daemon that needs to run on your system, and it needs to run with root privileges which might have certain security implications. Here now the solution is available where we do not need to start the daemon to create the containers. We can create the images and push them any of the repositories and images are fully compatible to run on any of the environment.  Podman is an open-source Linux tool for working with containers. That includes containers in registries such as docker.io and quay.io. let's start with the podman to manage the containers.  Install the package  [root@rhel8 ~] # dnf install podman -y  OR [root@rhel8 ~] # yum