Submind YouTube summaries
Thumbnail for The find command: basic usage

The find command: basic usage

Watch on YouTube

Video summary

The `find` command is a powerful utility primarily used to locate specific files or patterns within a directory structure, addressing limitations found in simpler commands like `ls`. While `ls -L *pattern` can list items in the current directory, it often fails to search subdirectories effectively. To overcome this, the `find` command allows users to start from a specific point, such as the current directory represented by a dot, and then apply tests to match file names using wildcards. It is crucial to enclose these patterns in quotes to prevent the shell from expanding them prematurely, ensuring that files located deep within nested subdirectories are successfully identified. Beyond simple name matching, the command offers robust filtering capabilities based on file type and size, which helps in managing large datasets more efficiently. Users can specify tests to retrieve only regular files by checking for type 'F', excluding directories and other special entries from the results. Similarly, searching by file size is straightforward; one can look for files larger than a certain threshold using a plus sign or those smaller than a limit using a minus sign. However, it is important to note that size-based searches may inadvertently include directories unless further refined in future iterations, and these counts naturally encompass hidden files and directories as well. The tutorial also introduces the ability to search for files based on their modification dates, which is essential for maintaining organized archives. Since `find` does not accept a direct date string for comparison, it utilizes a reference file created with the `touch` command to establish a timestamp baseline. By creating a file with a specific past date, users can instruct `find` to list all items newer than that reference point. Although this method effectively filters by time relative to the reference file, the output initially displays only filenames without their specific dates, a limitation that is acknowledged for future improvement in subsequent lessons. In conclusion, the `find` command provides a flexible framework for locating files through various criteria including name patterns, file types like regular files or directories, size constraints, and modification times relative to a reference point. When constructing these commands, if no directory path is explicitly provided, the search automatically begins in the current directory, streamlining the workflow for most users. The available tests cover common needs such as finding names with wildcards, filtering by type (excluding rare block or character devices), comparing file sizes, and checking against modification times, making it an indispensable tool for system administration and file management tasks.
Read the full video transcript
The find command is often used to get the location of a specific file name or pattern of names. We can already do something like this with the LS command. If I say LS -L star.pdf, that gives me information on all the PDF files in the current directory. But the file or files that we want might be in a subdirectory. To find files at any directory level, we will use the find command and start in the current directory, which is dot, and then test for any name that matches the pattern star.pdf. I have to enclose this argument in either single or double quotes to prevent the shell from expanding the wildcard. And we can see that there are PDF files in the subdirectories as well. If you do a find without any test starting at the current directory, you'll get all the files in the current directory and its subdirectories. Let's find out how many files there are. I'll pipe this output to word count and find the number of lines. That's a lot of files. It includes the names of directories and subdirectories, as well as the names of ordinary files. If I want only ordinary files, I start in the current directory and test to see that the file is of type F, which is a regular file, and pipe the result to word count lines, and there are fewer files. To find only directories, I can find, starting at the current directory, everything of type D for directory, and find out how many lines there are there. By the way, these numbers include hidden files and directories. In addition to finding by name and by type, we can find by file size. Let's search the downloads directory for files whose size is greater than 100 kilobytes. To find files whose size is less than 100 kilobytes, I'd use a minus sign. Let me bring back the previous command and change the plus to a minus. And there are the results. Notice, by the way, that this does include some directories. In a later video, we will learn how to ignore the directories and find only the regular files that are less than 100 K bytes in size. For now, let's move on to finding files by date. Let's say we want to see all the files modified after May 1st, 2025. We use the newer test, but we don't specify a date directly. Instead, we specify a reference file, and find will search for files that are newer than that file. I probably don't have a file whose modification date is exactly May 1st, 2025. So, I'll create a reference file using the touch command, and specifying the date in the format year, month, and day. And I could add hour, minute, and second if I wanted to. And then the name of my reference file. Call it ref May 2025, and that will be in my home directory. In fact, if I do an ls -l, you'll see that it indeed does have the day I want. Now, I can find in my downloads folder anything that's newer than the reference file. And there's the list. Now, this list only gives us the names and not the dates, so we don't know how new the files are. We'll fix that problem in the next video. In summary, we have the following tests. -name will find a name or name pattern using wild cards. With all tests, if you leave out the directory name, find will start in the current directory. We can look for files by their type. The last two types, block special device and character special device, are not commonly used. We can look for files that are either larger or smaller than a given size. And we can find files that are newer than the modification time of a reference file.