1. Introduction

The find command utility in Unix-like operating systems searches for files and directories within a file system. We can combine this command with various options and actions to perform complex searches, making it a versatile tool for system administrators and developers.

One common use of the find command is to list files in the current directory using find . or find . -print. In this tutorial, we’ll explore the differences between find . and find . -print. To begin with, we’ll discuss the implications of each command. Lastly, we’ll cover the use of the -print option as a default action.

2. Understanding the Differences Between find . and find . -print

At its core, find . searches the current directory, denoted by . and its subdirectories. However, when we use find . without specifying any options or expressions, it lists all files and directories starting from the current directory. For example, if we run the find . command in a directory containing files named file1.txt, file2.txt, and a subdirectory called folder1, we’ll see all the contents:

$ find .
./file1.txt
./file2.txt
./folder1

However, what’s happening under the hood is that find defaults to the -print action if no other expression is specified. Therefore, this means that find . is functionally the same as find . -print.

On the other hand, -print also outputs the pathnames of the files found to the standard output, one per line. For instance, if we execute find . -print in the same directory, we get the same output as before:

$ find . -print
./file1.txt
./file2.txt
./folder1

As per the POSIX standard and the findutils documentation, when no specific action is provided, find assumes -print as the default action. Therefore, whether we type find . or find . -print, we get the same result. In other words, we get a list of all files and directories under the current directory, each on a new line.

Now, let’s consider some examples. Suppose a directory structure where dir1 contains files file3.txt and a subdirectory dir2 with a file file4.txt. Running find . will list all files and directories, including those in subdirectories:

$ find .
./file1.txt
./file2.txt
./folder1
./dir1
./dir1/file3.txt
./dir1/dir2
./dir1/dir2/file4.txt

Notice that running the find . -print command produces the same output:

$ find . -print
./file1.txt
./file2.txt
./folder1
./dir1
./dir1/file3.txt
./dir1/dir2
./dir1/dir2/file4.txt

Hence, we can see that both find . and find . -print produce a same output.

3. The Role of -print as a Default Action

To understand why find . and find . -print produces identical outputs, it’s crucial to understand the role of the -print action in the find command’s syntax. The -print action is the default behavior because it provides a list of file and directory paths. Hence, this aligns with the primary purpose of the find command of locating files and directories.

Historically, the find command didn’t have a default action. Users had to explicitly include -print to see any output. This often led to confusion and errors when -print was omitted. In other words, there was no visible output even if files were found. To prevent such issues and streamline usage, -print was made the default action. Now, the find command automatically includes -print if no other action is specified.

4. Conclusion

In this article, we covered the difference between find . and find . -print. While find . and find . -print are effectively equivalent in modern Unix-like systems, understanding their historical context, subtle differences, and best practices is important for smooth command-line operations.