Finding Files and Directories

Start course
Overview
Difficulty
Intermediate
Duration
53m
Students
84
Ratings
5/5
starstarstarstarstar
Description

This course will get you up to speed with the fundamentals of Linux and prepare you for further study. You'll learn how to navigate your way through the Linux directory structure and the permissions for doing so. We'll also cover files and the multitude of ways in which they can be created, managed, and deleted.

Learning Objectives

Gain a foundational understanding of how to create and modify files in Linux.

Intended Audience

  • Anyone with little to no knowledge of Linux who wants to learn more about the operating system
  • Professionals who want to learn about Linux to enhance their career prospects

Prerequisites

This is a beginner-level course so there are no prerequisites, but an interest in Linux and programming knowledge in general would be beneficial.

Transcript

In today's lesson, we are going to talk about two commands that can help you find files and directories. The first command we're going to cover is the find command. You supply a path to find and a search expression and find will return the results that match the path and expression. If you don't tell find what to look for, it just returns all the files that are in your current directory and all the subdirectories below that directory.

You can give find a search pattern by using the -name option and find will return all the files and directories that match that pattern. If you use -iname, that tells find to ignore case. If you provide the -ls option to find, it will run a ls -l along listing format display of the files and directories that it finds. You can search for files by modification time by using the -mtime option to find. You can also search for files based on size using -size. To find files in directories that were created after another file, you can give that file to find with -newer.

If you want to execute a command against all the results that find returns, you can use the -exec or dash, E-X-E-C. You then provide the command that you want to run, and in the command with an \;. The matching curly braces represent the file or directory that was found by find. If you run find without any options, it just finds everything in your present working directory and below. That's the same really as running find ., which dot represents this directory.

Let's look in sbin for something named makedev. Didn't return any results. Let's do a case insensitive search, -iname. And we can see that there is a file named MAKEDEV but it's all uppercase. So when using name and matches case when using iname it ignores case. Let's look in bin and find any file that ends in V. So * means match anything. And we can see that it returns two results.

Let's look for files that are more than 10 days old but less than 13 days old in the current directory. So we can use mtime for modification time. And those are the files that it returns. It's also important note that you can use multiple options to find at the same time. So let's find anything that begins with S and perform an ls on it. Okay. Just one file.

Let's find things that are of size one megabytes or greater. And we find one result there. So this, you can use one megabyte, one kilobyte, one gigabyte, et cetera. So no files that are over a gigabyte in size in my directory. Let's find all the directories that are newer than file.txt. You can see file.txt is created on February 15th. Tpsreports is created on February 16th so it is indeed newer than file.txt. So this command will find everything in the current directory, find ., and it's going to execute the file command against all the search results.

Let's just run this. The file command just tells what kind of file it is. It's the best guess as to what it thinks the contents of the file represents. The locate command is similar to the find command in that you can tell it what to look for, and it will return a list of results that match your search pattern. Locate is faster than the find command. The find command looks through the path you gave it and evaluates each and every file and directory and determines if that matches your search pattern or not and then returns those results.

The locate command, however, works off of an index. Typically the index is rebuilt every day. Instead of evaluating each and every file to see if it matches the pattern, locate just does a lookup in the index. What you need to know about this is that the results are not in real-time. There is a lag between the time that the index gets created and the time that you run the locate command. So if you're looking for a file that was created just two or three minutes ago, chances are it's not going to be in the index. Your only hope for finding that file is with the find command that evaluates files and directories in real-time. Also on some systems, locate is not enabled by default.

Let's use the locate command to find files and directories that have sales in the name. Get four results back. Let's do another example. You don't have to specify a full name, you can specify a part of a name. So let's look for tpsre. That returns three results. Let's create a new file. And the touch command just simply creates an empty file.

Now, if we run our locate command again, we can see that the file is not found. Let's do it with the file's name . That's because the index is not up to date. Let's use the find command to evaluate all these in real-time. And we'll see that indeed, we can find the file with the find command.

Today, we talked about the find command and locate command. Find searches for files and directories in real-time. Find is very powerful and has many options. The locate command can be faster than find but the disadvantages is is that it's not in real-time.

About the Author
Avatar
Jason Cannon
Founder, Linux Training Academy
Students
2047
Courses
60
Learning Paths
7

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.

Covered Topics