FreeKB - Linux Commands find
Linux Commands - find

The find command without any options will list the files at and below the present working directory.

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

 

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

 

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

 

The maxdepth and mindepth options can be used to only display the files in the search directory, and to exclude the parent and child directories.

find /path/to/directory -maxdepth 1 -mindepth 1

 

The -name option can be used to only show results matching name. In this example, only results matching "file2" will be displayed. The -name option is CaSe SenSiTiVe.

~]# 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

 

Or to return results for multiple files or directories.

find / -iname foo -o -iname bar

 

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"

 

Often, you know that you are searching for a file or a directory, not both. The -type f option can be used to only find files and the -type d  option is used to or only find 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 values:

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

 

Following are additional popular things you can do with the find command.

 



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 7fa52 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |