Check for files with find command
In this step, you'll learn how to use the find
command to search for files and directories within a specified location. While ls
lists contents of a single directory, find
can search recursively through subdirectories.
The basic syntax for find
is:
find [starting_point] [expression]
[starting_point]
is the directory where the search begins. .
means the current directory.
[expression]
specifies what you are looking for (e.g., file name, type, size).
Let's create a simple file to search for. We'll use the touch
command, which creates an empty file. Make sure you are in the ~/project
directory.
touch my_document.txt
Now, let's use find
to locate the file we just created. We'll start the search in the current directory (.
) and look for a file named my_document.txt
.
find . -name my_document.txt
The -name
option tells find
to search for entries with a specific name.
You should see the path to the file as the output:
./my_document.txt
This confirms that find
successfully located the file.
The find
command is very powerful and has many options for searching based on different criteria like file type (-type f
for file, -type d
for directory), size, modification time, and permissions.
For example, to find all directories within the current directory and its subdirectories, you could use:
find . -type d
This would list all directories, including .
and ..
and any subdirectories you might have created.
Experiment with find
in your ~/project
directory. Try creating another file or directory and then using find
to locate it.
Click Continue to move on.