Locating files and directories using the FIND command in Linux

Home > Search > Linux commands
  by

The find command without any options is similar to the ls (list) command. The files at and below the present working directory will be displayed.

~]# find
.
./.bash_logout
./.bash_profile
./.bashrc
./.bash_history
./file1

 


Specify search directory

By default, the the find command will search for files at and below the present working directory.  To search at and below a certain directory, include the directory to search in the find command. In this example, the find command will search for files at and below the /home directory.

~]# find /home
.
./.bash_logout
./.bash_profile
./.bashrc
./.bash_history
./file1
./file2

 


Exclude directories from search

Let's say you want to search for files below /opt but you do not want to search /opt/tmp and /opt/user. The following command will exclude /opt/tmp and /opt/user from the search.

~]# find /opt –not \( -path /opt/tmp –prune \) –not \( -path /opt/user –prune \)

 


Search for file name

If you have no idea where a certain file resides, you can search at and below the / (root) directory. This will search the entire file system. In this example, the -name option is used to only search for files named file1.

~]# find / -name file2
/home/jane.doe/file2

 

The -iname option can be used to ignore case. In this example, both file2 and File2 are found.

~]# find / -iname file2
/home/jane.doe/file2
/home/jack.doe/File2

 

If you do not know the full file name you are searching for, you can use double quotes and wild cards.

~]# find / -iname "*ammple*"
/home/jane.doe/Downloads/Sample2

 


Search for hidden files

Use -name ".*" to search for hidden files.

~]# find / -name ".*"

 


Search for multiple files or directories

lf you want to search for multiple specific files or directories, you simply just specify each file or directory to search.

~]# find /path/to/directory1 /path/to/directory2

 


Exclude Permission Denied

The find command may produce many results with the text "Permission Denied."

~]# find / -iname jar
find: '/run/user/1000/gvfs': Permission denied
. . . 

 

Appending 2>/dev/null will exclude the results that contain Permission Denied from the output.

~]# find / -iname jar 2>/dev/null
/opt/bin/jar

 


Searching for file type

The -type option can be used to locate certain types of files. The d option will only search for directories. In this example, find searches for directories named bin.

~]# find / -name bin -type d
/usr/bin
/usr/lib/debug/usr/bin
/usr/share/locale/bin
/usr/local/bin

 

The -type option can accept the following parameters:

  • b - block special
  • c - character special
  • d - directory
  • p - named pipe
  • f - regular file
  • l - symbolic link
  • s - socket

 


NOT search

An exclamation point can be used to find files that do not match a certain pattern. In this example, files that are not named "example" will be returned.

~]# find / ! -name "example"

 


Searching for file size

The -size option can be used to find files that exceed a certain file size. These examples find files that exceed 1 KB, 1MB, or 1 GB.

~]# find / -size +1K
~]# find / -size +1M
~]# find / -size +1G

 


Include file last modified date

~]# find / -exec stat --format  '%y %n' "{}" \;

 


Searching for file permissions

The -perm (permissions) option can be used to search for files with a certain set of permissions. In this example, files that have the exact permission 775 (-rwxr-xr-x) will be returned.

~]# find / -perm 775

 

In this example, files that contain the group write permission will be returned.

~]# find / -perm -g+w

 

In this example, files that do not contain the group write permission will be returned.

~]# find / ! -perm -g+w

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments