Bootstrap FreeKB - Linux Commands - List files and directories using the find command
Linux Commands - List files and directories using the find command

Updated:   |  Linux Commands articles

There are a few commands that can be used to find files on a Linux system.

The find command without any options will list the files and directories in and below your present working directory, including hidden files and directories.

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

 

To list the files and directories in and below a directory other than your present working directory, you will simply include the directory to search (the /home directory in this example).

~]# find /home
/home/john.doe/
/home/john.doe/.bash_logout
/home/john.doe/.bash_profile
/home/john.doe/.bashrc
/home/john.doe/.bash_history
/home/john.doe/foo.txt
/home/jane.doe/
/home/jane.doe/.bash_logout
/home/jane.doe/.bash_profile
/home/jane.doe/.bashrc
/home/jane.doe/.bash_history
/home/jane.doe/bar.txt

 

lf you want to search multiple directories, you simply just specify each directory to search.

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

 

The maxdepth option can be used to specify how many directories the find command should search. Often, -maxdepth 1 is used so that the find command only lists the files and directories in your present working directory, or in a specific directory.

The mindepth option can be used to exclude the present working directory or specified directory from the results.

mindepth and maxdepth are often used together.

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.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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