How to Find Files in Linux

Introduction

In this post we will learn how to find files and directories in Linux using command-line utility called find. This utility can search for files based on a great number of parameters and therefore it makes it very powerful and useful tool. Another good thing about find utility is that it allows to execute various commands against the matching files. For example, it is possible to search for files with specific extensions in a specific directory and modify their file permissions – all in one command.

Find Command Syntax

The find command has the following syntax:

find [options] [path] [expression]

options – defines how to dereference links. Most common options:

  • -L option instructs the command to follow symbolic links
  • -P option instructs the command to skip the symlinks (default behavior)

pathdefines the starting directory. Command will search all hierarchy beneath this point. If not specified, current working directory will be the starting point.

expression – defines search patterns, search criteria and the actions to be taken on the search results. The default action is to print the names of the found files, but we can change it, as we will see later.

Examples

Now let’s look at its different usage examples, so you can better understand how to make it fit your needs.

I will use find commands to search for files within the following hierarchy:

├── files
   ├── cpp
   │   ├── file1.cpp
   │   └── readme.txt
  └── js
  ├── file1.js
   ├── file2.js
   └── readme.txt

Find Files by Name

The most common usage is to search for files by name. We can specify the exact name of a file:

orkhans@ubuntu1:~$ find ./files -name file1.js
./files/js/file1.js

Or we can use a search pattern:

orkhans@ubuntu1:~$ find ./files -name 'file1*'
./files/js/file1.js
./files/cpp/file1.cpp

Find Files by Extensions

We use search patterns for -name attribute if we want find files by their extensions:

orkhans@ubuntu1:~$ find ./files -name '*.txt'
./files/js/readme.txt
./files/cpp/readme.txt

If you want to use case-insensitive search, use -iname instead of -name:

orkhans@ubuntu1:~$ find ./files -name 'FILE1*'
orkhans@ubuntu1:~$ find ./files -iname 'FILE1*'
./files/js/file1.js
./files/cpp/file1.cpp

Find Files by Type

We can make a search based on a file type. The supported file types are:

  • b – block (buffered) special
  • c – character (unbuffered) special
  • d – directory
  • p – named pipe (FIFO)
  • f – regular file
  • s – socket

The most common usage of this, though, is to search for directories only:

orkhans@ubuntu1:~$ find ./files -type d
./files
./files/js
./files/cpp

Notice, that the command only returned the names of the directories.

We can also combine both type and name attributes:

orkhans@ubuntu1:~$ find ./files -type d -name js
./files/js

Find Files by Size

We can use the -size parameter to specify the required file size. The following size suffixes are most common:

  • b: 512-byte blocks (default)
  • c: bytes
  • k: Kilobytes
  • M: Megabytes
  • G: Gigabytes

Let’s find all files larger than 50 bytes. We use plus sign to indicate that the file should be larger than the specified size:

orkhans@ubuntu1:~$ find ./files -type f -size +50c
./files/cpp/readme.txt

Let’s find the files smaller than 5 Gigabytes. We use minus sign to indicate that the file should be smaller than the specified size:

orkhans@ubuntu1:~$ find ./files -type f -size -5G
./files/js/file1.js
./files/js/file2.js
./files/js/readme.txt
./files/cpp/file1.cpp
./files/cpp/readme.txt

We can also specify the exact file size. The found file is exactly 273 bytes:

orkhans@ubuntu1:~$ find ./files -type f -size 273c
./files/cpp/readme.txt
orkhans@ubuntu1:~$ ll ./files/cpp/readme.txt
-rw-rw-r-- 1 orkhans orkhans 273 Yan 25 11:13 ./files/cpp/readme.txt

Find Files by Permissions

We can also search for files based on their file permissions.

orkhans@ubuntu1:~$ find ./files -type f -perm -664
./files/js/file1.js
./files/js/file2.js
./files/js/readme.txt
./files/cpp/file1.cpp
./files/cpp/readme.txt

Find Files by Timestamps

It is possible to find files by their modification, access or change time stamps.

Three main timestamps are:

  • atime – shows when the file was last accessed, for example read.
  • mtime – shows when the file contents were last modified.
  • ctime – shows when metadata(ownership or access permissions) was last changed. Content modification also updates ctime.

The following command finds all files that were modified less than 10 days ago. We use parameter -mtime for this:

orkhans@ubuntu1:~$ find ./files -type f -mtime -10
./files/js/file1.js
./files/js/file2.js
./files/js/readme.txt
./files/cpp/file1.cpp
./files/cpp/readme.txt

If we wanted to find all files older than 10 days we would use this:

orkhans@ubuntu1:~$ find ./files -type f -mtime +10
orkhans@ubuntu1:~$ 

If we do not specify neither plus sign nor minus sign. The command will search for the file which is exactly N days old.

All of my test files for this article have been modified today, so they match -mtime 0:

orkhans@ubuntu1:~$ find ./files -type f -mtime 0
./files/js/file1.js
./files/js/file2.js
./files/js/readme.txt
./files/cpp/file1.cpp
./files/cpp/readme.txt

Conclusion

Now you know how to use Linux find tool to search for files based on various criteria.

Tags:

Add a Comment