Linux Find Command Tutorial

28/12/2020
Chưa phân loại
In this quick tutorial, we will see how we can use find command in an Ubuntu machine to quickly find files based on various patterns, regular expressions and inside directories in a recursive manner. Let’s play through various conditions so that we can master basic find commands which can make our everyday life easier with Ubuntu.

Find by name

The easiest command we can use for finding a file is by a specific name in a directory. The advantage with this command is that bash will find the file even if it present in one of the recursive directories inside the passed directory path. Let’s look at an example:

find Code -name ifelse4.sh

This command will find the file ifelse4.sh recursively inside the directory Code. Let’s see the output for this command:

Find file by name

The name option we mentioned above is case-sensitive. If you want to find the file irrespective of the case in the name, use the following command:

find Code -iname ifelse4.sh

Finding a file by regular expression

We can use simple regular expressions to find a file in a directory which matches that regular expression. Let’s demonstrate this with a simple command to find all files with any name and with an extension of .txt:

find Code -regex ".*.sh"

Here is what we get back with this command:

Finding file by regex

Let us understand what this command means here:

  • -regex: This just signifies that we are going to pass a regular expression next.
  • In the regular expression, first period (.) signifies that any number of characters in file name shoule be a match.
  • Next, with the *, we match any number of repetitions of any character (due to the period).
  • Finally, we match all files with .sh extension.

The good thing about a regular expression can be that it can be made as flexible as you can define. Let’s modify above example to find files with .sh and .txt extension as well:

find Code -regex ".*.sh|.txt"

Finding files modified in last n minutes

Finding a file which was modified in last n minutes is easy as well. Let’s look at an example straight away:

find Code -mmin -90

Let’s see the output for this command:

Finding file modified in last 90 minutes

Finding files modified in last n days

Finding a file which was modified in last n days is easy as well. Let’s look at an example straight away:

find Code -mtime 0

Here is what we get back with this command:

Find file by days

Finding files by permissions

Finding a file with specific permissions is possible as well. We can find files which are associated to a user or a user-group:

find . -user shubham

Here is what we get back with this command:

Finding files belonging to a user

We can also apply the same logic to find files belonging to a user group:

find . -group root

Finding files by size

To find files which are bigger than a specified size can be found with the following command:

find Code -size +500

Here is what we get back with this command:

Find file by size

Apart from the bytes, size of a file can be mentioned as:

  • b: 512-byte blocks: This is the default unit if none is specified
  • c: bytes
  • k: kilobytes
  • M: megabytes
  • G: gigabytes

Finding files by type

It is possible to find file with a type. We have following types for find command:

  • d: directory
  • f: regular file
  • l: symbolic link
  • b: buffered block
  • c: unbuffered character
  • p: named pipe
  • s: socket

Let’s use a command to find a regular file:

find . -type f

Here is what we get back with this command:

Find files by type

Finding files with multiple conditions

As a last example, it is possible to find files by appending multiple conditions as we saw above. Let’s try multiple conditions in a single find command now:

find . -size +1c -and -name "*.sh"

Here is what we get back with this command:

Find files with multiple conditions

Conclusion

In this lesson, we looked at how we can use the find command to find any files with some name or by permissions or by type. We can even append multiple conditions to find files which satisfy all the conditions. Play with the commands even more to fund the rela power.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install Neo4J on Ubuntu

In this post, we will see how we can install one of the most popular Graph-based database, Neo4J on Ubuntu and start using...
28/12/2020

malloc in c language

You can come here for two reasons: either you want to dynamically allocate content, or you want to know more about how...
29/12/2020

Guide to using Apple Music on Linux with/without ‘Hack’

If you are a music freak, there is no way you haven’t heard of Apple Music. Known as one of the best music streaming...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024