How to Find File on Redhat 8

How to Find/Search File and Folder on RHEL 8

There are two common commands are available to search files and directories inside the file system in redhat/centos Linux base operating system. The command that we will discuss with examples to search files and directories are “find” and “locate”.

Find: The command “find” is use to search the files and directories inside the local file system in real time as per the given criteria command line argument. User id that use to find the file inside the directory must have the read and executable permission of the directory. The first argument in find command is the directory if directory is not define in command, find command search file on the current working directory and their sub directories. Multiple options are available to find the files, searches can be based on the file name, file size, last modified time stamp, and other file characteristics in any combination.

The find utility is install during the installation of operating system. If find utility is not install on redhat/centos, type the below mention command to install find utility.

yum install findutils.x86_64 -y

To find the file that have a name “sshd_config” inside the root directory “/”, type the below mention command. The result of this command provide the exact same matches of file name “sshd_config” in the root directory “/” and all subdirectories on server due to -name option is use.

find / -name sshd_config

To search a file that are a partial match in filename need to be use of wildcard option. To search a file in the directory “/owais” on server that end “.txt”, type the below mention command.

find /owais -name '*.txt'

Another example of wildcard option to search files in directory “/owais” that contain “good” anywhere in their filename on server, type the below mention command.

find /owais -name '*good'

To search a file with case insensitive option “-iname”, type the below mention command. By use of “case insensitive” option ignore the upper and lower case match requirement.

find / -iname 'messages'

Find a File with specific user/group:

To search the files that user “owais” have permission on files, type the below mention command.

find -user owais

To search all files in the /apps directory owned by user “owais” on server, type the below mention command

find /apps -user owais

To search the files that user have permission on it by user id “1001”, type the below mention command.

find -uid 1001

To search the files that group “IT” have permission on files, type the below mention command.

find -group IT

To search the files that group “IT” have permission on it by use of group id “1001”, type the below mention command.

find -gid 1001

To search the files that user “owais” have permission on files with specific group “IT” inside the directory root “/” on server machine, type the below mention command.

find / -user owais -group IT

Find a File with specific permission:

To Search the files with a particular set of permissions (Read, Write and Execute) inside the directory “/apps”, type the below mention command.

find /apps -perm 755

To search a file that user at least read and write permission, group have at least write permission and other at least read permission inside the directory “/apps”, type the below mention command.

find /apps -perm -624

To Search a files for which user have read permission, or the group has at least read permissions, or other have at least write permission inside the directory “/apps” , type the below mention command.

find /apps -perm /442

Find a File Base on Size:

To search the file that exactly same in size (K for kilo, M for Mega and G for Giga), type the below mention command.

find -size 8K
find -size 8M
find -size 8G

To list all files in the /apps directory with a file size greater than 100 Kilobytes, type the below mention command.

find /apps -size +100K

Find a File Base on Time Change:

To search a file that content has changed by the time in minutes, type the below mention command for search a file that change their content in exactly 100 minutes.

find / -nmin 100

To search a file that file content has changed more than 100 minutes ago, type the below mention command.

find  / -nmin +100

To search a file that file content has changed less than 100 minutes ago, type the below mention command.

find / -nmin -100

To search the files in /apps directory that have not change in the last 55 minutes, type the below mention command.

find /apps -nmin +55

To search the files in /apps directory that have change in the last 55 minutes, type the below mention command.

find /apps -nmin -55

Find a File Base on File Type:

To search all regular files on server, type the below mention command.

find / -type f

To search the entire directories in the /apps directory, type the below mention command.

find /apps -type d

To search all softlinks on server, type the below mention command.

find / -type l

To find all soft links in directory “/” that have “app” as part of their names, type the below mention command.

find / -type l -name '*app*'

To generate a list of all block device in the /dev directory on server, type the below mention command

find /dev -type b

To search for a regular file with more than 1 hard links, type the below mention command.

find / -type f -links +1

Locating file on the system:

locate: Locate is another command to search the files with file path from a pre-generated database and provide the result instantly. To install the package of locate “mlocate”, type the below mention command.

yum install mlocate.x86_64 -y

To find the file or directory of have a name “passwd” or “image”, type the below mention command.

locate passwd
locate image

To search the file/directory have name “application” with all possible combinations of upper and lowercase letters by use of option “-i” that is a case-insensitive search.

locate -i application

Use -n option to limits the number of return search results by locate. To limits the search results that return first four matches. 

locate -n 4 apps.bkp

Note: the locate database is automatically daily updated. The root user can update of the database with the updatedb command.

updatedb

Thanks For Read This Article

Comments

Post a Comment