PuTTYgen

Download PuTTYgen - Putty key generator

Find Command in Linux With Examples

Find Command in Linux With Examples

"find" is a command-line utility in Unix-like operating systems that is used to search for files and directories in a specified location. It can search based on various criteria such as name, size, date modified, and others, and perform actions on the results such as printing, deleting, or executing other commands.

Find Command is a command-line utility that locates files in one or more directory trees. More so, using the command, users can set specific search criteria and actions on files that match the search. Therefore it is a utility for file hierarchy, where not only the user can leverage it to find data but also perform successive operations on it.

Find Commands in Linux
Find Commands in Linux

Users can choose from several standards such as either search a matching filename or time range that matches when the file was accessed or modified. Users can search for files using the following criteria that find command supports – file pattern, name pattern, creation date, owner and permissions, folder, and modification date. While the command-utility can search for files in multiple directories, by default the ‘find command’ shows results of files in the current working directory.

Examples of the file command in Linux

There are several ways one can use the file command to make tasks easier. Below are examples of the ‘find’ command on Linux:

Find Files by Name using find command

1. Find From the Current Directory

The command searches the current working directory for the file name ‘putty.txt’ However if a user wants the ‘find command’ to return only files and not the directories then one has to add -type f at the end of the command.

find . -name putty.txt

2. Look for Files in the Home Directory

The command checks the /home directory. For example, the below command is to look for the name ‘putty.txt.’

find /home -name tecmint.txt

3. Find Files in /Home Directory Regardless of Case Sensitive

The command seeks to look for all files irrespective of the case, i.e., uppercase or lowercase in the /home directory. Therefore the result will display all results that match the file name.

find /home -iname putty.txt

4. Search All Directories for a Particular File

The command searches directories with a specific name such as ‘puttygen’ in the root folder. For example, the title to search is ‘puttygen,’ then the user has to type the below command.

find / -type d -name puttygen

5. Find PHP Files Using Name

The command will allow users to find all PHP files with a specific name such as ‘ask’ in a working directory.

find . -type f -name ask.php

6. Check Files in a Temporary Directory

It checks ‘tmp’ files that have the file name ‘file.php.’

find / -type f -name tmp.php​

Find Files/Folders by Permissions

7. Find Files/Folders by Permissions 777

The command will return all files that have 777 permissions. In this case, it will check all data with the name ‘trial.php’ and has 777 permissions.

​find / -perm 777 -name test.php

8. Find SUID File With Permissions 755

The SUID (Set owner User ID upon execution) is temporary permission set for a file that permits a user to run executable by inheriting access permission of the executable owner. The below command will search for files that have SUID permission 755

find / -perm 4755​

9. Look for SGID File With 644 Permission

Akin to SUID, SGID (Set Group ID upon execution) is also temporary permission given to a user to run a file or executable. The primary difference is that it inherits permission of the group when running a file rather than the person logged into the system. One can search SGID files on Linux using the below command

find / -perm 2644​

10. Sticky Bit Files With Permissions 755

The sticky bit is another type of permission bit that when assigned on a file will only let the root user delete the file. Therefore, no other users will have the liberty to delete the data that is created by the root user.

So users enable sticky bit to ensure no one can delete the folder or contents. Apart from deletion, users have other full privileges. Below is the one can use the find command on Linux to search for files with permission 755:

​find / -perm 1755

User and Group Commands

11. User-Specific Command

The above command will search for files with ‘putty.txt’ owned by a specific user or group. For example, in this case, the username is ‘trial.’ Molly is the owner of the file or folder.

find / -user trial-name molly.txt

12. Group-Specific Command

In the case of the group, if the group name is ‘college’ then the command will be as follows:

find / -group college-name molly.txt

Search by the Time Parameter

13. Look for Files Modified Beyond 90 Days

The command will return all files that were changed beyond the 90 days.

find / -ctime +90 molly.txt​

14. Search for File Altered Precisely at 90 Days

If a user wants to search for files that were modified exactly before 90 days, then they need to enter the following file command:

find / -mtime ​90

15. Search for Files Modified Before 90 Days

Moreover, if a user wants to find files that were opened in the 90 days than type the following command:

find / -atime -90​

16. Modified Files Within a Time Range

In case a user wants to look for a file that was modified in a given time range, for example, more than 30 days and less 90 days, use the following command:

find / -mtime +30 -mtime -90

17. Files Than Fixed Time

Look for files modified less than a specific time

find / -cmin -30

 18. Files Changed Exactly Below Given Time

So let’s say if a user wants to see all files whose log is changed exactly below 30 minutes, for example, then type the following command

find ​/ -mmin 30

19. Files Modified Before Than 30 Mins

With the following command, one can look files altered more than 30 mins back

find / -amin +30

20. File in a Given Time Range (Hours/Minutes)

The command will allow users to search for files modified in a given range. For example, if one needs to see a file that was modified after 5 minutes and before 25 minutes then enter the following command:

find / -mmin +5 -mmin -25​

21. Check Files Created

To check newly created files a user has to enter a simple find command on Linux.

​find / -newer test.txt

Search by File Size Using Commands

22. Look for File More Than 15 Bytes

Using the following command, a user can look files or folders that exceed 15 bytes in size.

find / -size +15c​

23. File or Folder More Than 40KB

User can use the below command to look for files that are more than 40kb in any of the folder:

find /tmp -size 40k​

24. Looking up File Less Than 20MB

Users can leverage the ‘find command’ to search for files that are less than 20MB is size within a folder

find /tmp -size -20M​

25. Searching Files More Than 1GB

The below command will return all files or folders that exceed 1GB in size

find /​ -size +1G

26. Empty Files in a Server

Searching for an empty file is very easy, and one can do so by executing the below find command on

find / -size 0c

27. Looking for Files by a Range of Size

With the command, one can search files between a given range of size. For example, if a user wants to look for files that are more than 100MB and less than 300MB than the command would like this:

find /fast -size +100M -size -300M -user molly -iname trial.txt

In the above example, Molly is the owner of the file, and the ‘trial’ is the name of the file.

Run Execute Command on Files That Return With ‘Find’ Command

Before using the executable command, users must be cautious as it can modify everything. The execute a command (-exec) allows users to take action on files found by using the ‘find command.’

28. Searching /Tmp Folder

So if a user wants to search the /tmp folder for the file name ‘trial’ and show the results on the screen, then one needs to execute the following find command:

find /tmp -iname *trial.* -exec ls -ld {} ;

29. Changing the Owner

It is an interesting example of the extent a user can use the execute command. The following command will return file trial.txt whose owner is Molly in /home folder. Then the -exec command will alter the ownership to Sam by running the chown command.

find /home -user Molly -name trial.txt -exec chown Sam.Sam {} ;​

30. Change Permission Using -Exec

As it is a fact that possibilities with -exec is endless. A user can run the chmod command on files that return with the find command to change the file permissions to 755.

find /var/named -user Molly -name trial.txt.db -exec chmod 755 {} ;​

The above are some of the most common ‘find’ commands on Linux. There are many other possibilities that users can choose to and make file searching less time consuming and easy.