Searching For Files and Directory Names

If you have ever forgotten where you saved a file, you know the value of search. Tools exist on Linux to search for files by file name, or you can search the contents of files.

Learning Objectives

You should be able to search for files using find.

Video Walkthrough

Use this video to follow along with the steps in this lab.

Load Files

  • Run the following command in the terminal to ensure you are in your home directory.
cd ~
  • Run the following command to delete the cyfunfiles directory (if it exists).
rm -rf cyfunfiles
  • Run the following command to download files from the internet. This will create a new folder called cyfunfiles in your home directory. (It is best to copy and paste this command into your terminal. If you get an error message asking for a username, it likely means there was a typo in the URL.)
git clone https://github.com/jimmarq/cyfunfiles.git
  • Change directories to the cyfunfiles directory.
cd cyfunfiles
  • Change directories to the linux_finding subdirectory.
cd linux_finding
  • Look at the files in the folder with ls.
ls

Contents of the cyfunfiles directory

Using Find

The find command can be used to find files by name, size, or other attributes. The find command searches the file system each time it is run. This means that it can take a long time to run, depending on how many files are on the system. The find command is very flexible, and can be used to find files by name, size, or other attributes.

  • The basic syntax for find is 3 parts: 1) find, 2) where to look, and 3) filters.
  • Run the following command to find files in the current directory with "fruits" in the name.
find . -name "*fruits*"

In this case, the period tells find to start searching in the current directory. The -name option tells find to search for files with "fruits" in the name. The asterisks are wildcards that match any characters. So, the command will find files with "fruits" anywhere in the name.

  • Run the following command to search the entire computer for files with the word "fruits" in the name.
sudo find / -name "*fruits*"
  • The sudo command is required because find will search the entire computer, including directories that are not accessible to your user account. You may still get some access denied errors, but those wouldn't be places you'd be looking for files anyway.

  • By default, Linux is case-sensitive. To make the search case-insensitive, use the -iname option. The following command finds files and directories that have "fruits" in the name, only searching the home directory instead of the entire system.

find ~/ -iname *Fruits*
  • You can search for files only using the -type f option.
find ~/ -type f -name *fruits*
  • You can search for directories only using the -type d option.
find ~/ -type d -name *find*

Challenge

  • Find files with the name "bin" in the file name only.
  • Find files and directories with "network" in the name.
  • Find directories with "log" in the name.

Reflection

  • When would it be best to use find to search for files?

Key Terms

  • Linux find: A powerful command-line utility used to search for files and directories within a file system based on various criteria such as name, size, modification date, and permissions. The find command can also execute actions on the found items, such as deleting, moving, or changing permissions.