How to Use the ‘find’ Command by File Type in Linux/Unix: A Practical Guide

The find command is a powerful tool for locating files and directories based on various criteria, including the type of the file. Here are some examples of how to use find to search for files and directories by type:

1. Find All Regular Files

To find all regular files in the current directory and its subdirectories:

find . -type f

This command searches the current directory (.) and its subdirectories for regular files (-type f).

find -type f

2. Find All Directories

To find all directories in the current directory and its subdirectories:

find . -type d

This command searches the current directory (.) and its subdirectories for directories (-type d).

3. Find All Symbolic Links

To find all symbolic links in the current directory and its subdirectories:

find . -type l

This command searches the current directory (.) and its subdirectories for symbolic links (-type l).

4. Find All Block Special Files

To find all block special files in the current directory and its subdirectories:

find . -type b

Block special files are typically device files that support random access, such as hard drives.

5. Find All Character Special Files

To find all character special files in the current directory and its subdirectories:

find . -type c

Character special files are typically device files that do not support random access, such as terminals.

6. Find All Sockets

To find all sockets in the current directory and its subdirectories:

find . -type s

Sockets are special files used for inter-process communication.

7. Find All FIFOs (Named Pipes)

To find all named pipes (FIFOs) in the current directory and its subdirectories:

find . -type p

FIFOs are special files used for inter-process communication, similar to pipes but with a name in the filesystem.

8. Find All Types of Files

To find all types of files (regular files, directories, symbolic links, etc.) in the current directory and its subdirectories:

find . -type *

This command finds all types of files by using the wildcard (*). However, it’s not very useful in practice because it matches everything.

9. Find All Files Excluding Directories

To find all files excluding directories in the current directory and its subdirectories:

find . -not -type d

This command excludes directories (-not -type d).

10. Find All Files Excluding Symbolic Links

To find all files excluding symbolic links in the current directory and its subdirectories:

find . -not -type l

This command excludes symbolic links (-not -type l).

11. Find All Files Excluding Symbolic Links and Directories

To find all files excluding symbolic links and directories in the current directory and its subdirectories:

find . \( -type l -o -type d \) -prune -o -print

This command excludes symbolic links (-type l) and directories (-type d) using the -prune option to skip them.

Example Usage

Let’s say you want to find all regular files in the /home/user/documents directory and its subdirectories:

find /home/user/documents -type f

Conclusion

These examples demonstrate how to use the find command to search for files and directories by type. You can combine these options with other criteria, such as file size, modification time, and file name patterns, to create more complex queries. If you have specific requirements or need more advanced functionality, feel free to ask, and I can provide more tailored examples.

This entry was posted in How to, Find Command and tagged , , , , . Bookmark the permalink.