BÀI VIẾT

Hướng dẫn sử dụng lệnh find trên Linux

3.9K
author Đặng Văn Chương - 2022-08-11 15:22:28 (GMT+7)

Giới thiệu

Lệnh find là một trong những lệnh mạnh mẽ của quản trị viên hệ thống Linux. Nó tìm kiếm các tệp và thư mục trong hệ thống phân cấp thư mục dựa trên một biểu thức do người dùng cung cấp và có thể thực hiện hành động do người dùng chỉ định trên mỗi tệp phù hợp.

Chúng ta có thể sử dụng lệnh find để tìm kiếm tệp và thư mục dựa trên quyền, loại, ngày, quyền sở hữu, kích thước và hơn thế nữa. Nó cũng có thể được kết hợp với các lệnh khác như grep hoặc sed.

Cú pháp câu lệnh find

bash (non-root)
find [OPTIONS] [pat/to/folder] [expression]

Chú thích tham số lệnh find:

  • [OPTIONS]: Là các tùy chọn cho lệnh find.
  • [path/to/folder]: Là đường dẫn đến thư mục cần tìm kiếm tệp.
  • [expression]: Là mẫu tìm kiếm và các hành động được phân tách bằng các toán tử.

Tìm kiếm tệp theo đuôi mở rộng

Để tìm kiếm các tệp theo phần đuôi mở rộng bên trong một thư mục bất kì, chúng ta dùng lệnh sau:

bash (non-root)
find -L /path/to/folder -name "*.html"

Chú thích tham số lệnh find:

  • -L: Là tùy chọn tìm kiếm tệp theo liên kết tượng trưng.
  • /path/to/folder: Là đường dẫn đến thư mục cần tìm kiếm.
  • -name: Tìm kiếm các tệp theo tên.
  • *.html: Chỉ tìm kiếm các tệp có phần đuôi mở rộng .html.

Ví dụ: Để itìm kiếm các tệp có phần đuôi .php trong thư mục website chúng ta dùng lệnh như sau:

bash (non-root)
find -L ~/home/website -name "*.html"
Kết quả
website1/readme.html

Tìm kiếm tệp theo tên

Tìm kiếm tệp theo tên có lẽ là cách sử dụng lệnh phổ biến nhất của lệnh find. Để tìm kiếm một tệp theo tên của nó, chúng ta hãy sử dụng tùy chọn -name theo sau là tên của tệp chúng ta đang tìm kiếm như sau.

bash (non-root)
find /path/to/folder -type f -name filename

Chú thích tham số lệnh find:

  • /path/to/folder: Là đường dẫn đến thư mục cần tìm kiếm.
  • -type f: Chỉ tìm kiếm các tệp trong thư mục.
  • -name: Tìm kiếm các tệp theo tên.
  • filename: Là tên tệp bạn cần tìm kiếm.

Ví dụ: Để tìm kiếm các tệp có tên index.php trong thư mục /home/website, bạn sẽ sử dụng lệnh sau:

bash (non-root)
find ~/home/website -type f -name index.php
Kết quả
/root/home/website1/wp-includes/blocks/index.php
/root/home/website1/wp-admin/network/index.php
/root/home/website1/wp-admin/user/index.php
/root/home/website1/wp-admin/index.php
/root/home/website1/index.php
/root/home/website1/wp-content/themes/twentytwenty/index.php
/root/home/website1/wp-content/themes/twentytwentyone/index.php
/root/home/website1/wp-content/themes/twentynineteen/index.php
/root/home/website1/wp-content/themes/index.php

Ngoài ra, chúng ta cũng có thể dùng lệnh find để tìm kiếm các nội dung không phân biệt chữ hoa, chữ thường như sau:

bash (non-root)
find /path/to/folder -type f -iname filename

Chú thích tham số lệnh find:

  • /path/to/folder: Là đường dẫn đến thư mục cần tìm kiếm.
  • -type f: Chỉ tìm kiếm các tệp trong thư mục.
  • -iname: Tìm kiếm các tệp theo tên không phân biệt chữ hoa, chữ thường.
  • filename: Là tên tệp bạn cần tìm kiếm.

Ví dụ: Để tìm kiếm các tệp có tên index.php trong thư mục /home/website, bạn sẽ sử dụng lệnh sau:

bash (non-root)
find ~/home/website -type f -name index.php
Kết quả
/root/home/website1/wp-includes/blocks/index.php
/root/home/website1/wp-admin/network/index.php
/root/home/website1/wp-admin/user/index.php
/root/home/website1/wp-admin/index.php
/root/home/website1/INDEX.PHP
/root/home/website1/wp-content/themes/twentytwenty/index.php
/root/home/website1/wp-content/themes/twentytwentyone/index.php
/root/home/website1/wp-content/themes/twentynineteen/index.php
/root/home/website1/wp-content/themes/index.php

Ví dụ: Để tìm kiếm tất cả các tệp trong thư mục ~/home/website1/wp-admin/js, chúng ta dùng lệnh sau:

bash (non-root)
find . -type f
Kết quả
./theme-plugin-editor.min.js
./customize-widgets.js
./editor.js
./code-editor.js
./editor-expand.js
./application-passwords.js
./gallery.min.js
./password-strength-meter.min.js
./updates.min.js
./plugin-install.js
. . .

Ví dụ: Để tìm kiếm tất cả các thư mục con bên trong trong thư mục ~/home/website1, chúng ta dùng lệnh sau:

bash (non-root)
find . -type d
Kết quả
./wp-includes
./wp-includes/ID3
./wp-includes/block-supports
./wp-includes/block-patterns
./wp-includes/Text
./wp-includes/Text/Diff
./wp-includes/Text/Diff/Engine
./wp-includes/Text/Diff/Renderer
./wp-includes/assets
./wp-includes/sodium_compat
./wp-includes/sodium_compat/namespaced
./wp-includes/sodium_compat/namespaced/Core
./wp-includes/sodium_compat/namespaced/Core/ChaCha20
./wp-includes/sodium_compat/namespaced/Core/Poly1305
./wp-includes/sodium_compat/namespaced/Core/Curve25519
./wp-includes/sodium_compat/namespaced/Core/Curve25519/Ge
. . .

Tìm kiếm tệp theo kích thước

Để tìm kiếm các tệp dựa trên kích thước tệp, chúng ta hãy sử dụng tham số -size cùng với kích thước tệp mà chúng ta mong muốn.

bash (non-root)
find /path/to/folder -type f -size [file-size]

Chú thích tham số lệnh find:

  • /path/to/folder: Là đường dẫn đến thư mục cần tìm kiếm.
  • -type f: Chỉ tìm kiếm các tệp trong thư mục.
  • -size: Tìm kiếm các tệp theo kích thước mong muốn.
  • fie-size: Là kích thước của tệp cần tìm kiếm.

Ví dụ: Để tìm kiếm các tệp có kích thước chính xác 1234 byte bên trong thư mục ~/home/website1 chúng ta dùng lệnh như sau:

bash (non-root)
find ~/home/website1 -type f -size 1234c
Kết quả
/root/home/website1/wp-includes/blocks/site-title.php
/root/home/website1/wp-content/languages/vi-81c889563f09dd13de1701135dc62941.json

Ví dụ: Để tìm kiếm tất cả các tệp có kích thước bé hơn 3kb bên trong thư mục làm việc hiện tại. Chúng ta sử dụng dấu trừ (-) trước giá trị kích thước như sau:

bash (non-root)
find ~/home/website1 -type f -size -3k
Kết quả
/root/home/website1/wp-content/themes/index.php
/root/home/website1/wp-content/index.php
/root/home/website1/wp-content/plugins/index.php
/root/home/website1/wp-content/plugins/akismet/views/get.php
/root/home/website1/wp-content/plugins/akismet/views/title.php
/root/home/website1/wp-content/plugins/akismet/views/setup.php
/root/home/website1/wp-content/plugins/akismet/views/activate.php
/root/home/website1/wp-content/plugins/akismet/views/stats.php
/root/home/website1/wp-content/plugins/akismet/views/predefined.php
/root/home/website1/wp-content/plugins/akismet/views/enter.php
/root/home/website1/wp-content/plugins/akismet/views/start.php
/root/home/website1/wp-content/plugins/akismet/_inc/img/logo-a-2x.png
/root/home/website1/wp-content/plugins/akismet/.htaccess
/root/home/website1/wp-content/plugins/akismet/index.php
. . .

Ví dụ: Để tìm kiếm tất cả các tệp có kích thước lớn hơn 3kb bên trong thư mục làm việc hiện tại. Chúng ta sử dụng dấu cộng (+) trước giá trị kích thước như sau:

bash (non-root)
find ~/home/website1 -type f -size +3k
Kết quả
/root/home/website1/wp-content/themes/twentynineteen/style-editor.css
/root/home/website1/wp-content/themes/twentynineteen/comments.php
/root/home/website1/wp-content/themes/twentynineteen/package-lock.json
/root/home/website1/wp-content/plugins/akismet/views/connect-jp.php
/root/home/website1/wp-content/plugins/akismet/views/config.php
/root/home/website1/wp-content/plugins/akismet/_inc/akismet.js
/root/home/website1/wp-content/plugins/akismet/class.akismet-cli.php
/root/home/website1/wp-content/plugins/akismet/changelog.txt
/root/home/website1/wp-content/plugins/akismet/class.akismet.php
/root/home/website1/wp-content/plugins/akismet/wrapper.php
/root/home/website1/wp-content/plugins/akismet/readme.txt
/root/home/website1/wp-content/plugins/akismet/class.akismet-admin.php
/root/home/website1/wp-content/plugins/akismet/class.akismet-rest-api.php
/root/home/website1/wp-content/plugins/akismet/LICENSE.txt
. . .

Tìm kiếm tệp theo thời gian sửa đổi

Lệnh find cũng có thể tìm kiếm các tệp dựa trên thời gian sửa đổi gần đây nhất. Tương tự như khi tìm kiếm theo kích thước, hãy sử dụng các ký hiệu cộng và trừ cho “lớn hơn” hoặc “nhỏ hơn”.

bash (non-root)
find /path/to/folder -name "*.html" -mtime x

Chú thích tham số lệnh find:

  • /path/to/folder: Là đường dẫn đến thư mục cần tìm kiếm.
  • -name: Tìm kiếm các tệp theo tên.
  • *.html: Tìm kiếm các tệp có đuôi là .html.
  • -mtime x: Là khoảng thời gian tệp được sửa đổi (tính bằng x ngày).

Ví dụ: Để hiển thị các tệp có đuôi .txt bên trong thư mục ~/home được sửa đổi trong khoảng thời gian 3 ngày gần đây. Chúng ta dùng lệnh sau:

bash (non-root)
find ~/home -name "*.txt" -mtime 3
Kết quả
/root/home/domain.txt
/root/home/123host.txt
/root/home/number.txt

Tìm kiếm tệp theo thời gian sửa đổi với thời gian được xác định trước.

bash (non-root)
find /path/to/folder -mtime +n -daystart

Chú thích tham số lệnh find:

  • /path/to/folder: Là đường dẫn đến thư mục cần tìm kiếm.
  • -name: Tìm kiếm các tệp theo tên.
  • -mtime +n: Là khoảng thời gian tệp được sửa đổi nhiều hơn n ngày.
  • -daystart: Là ngày bắt đầu sửa đổi tệp.

Ví dụ: Tìm kiếm các tệp có thời gian sửa đổi lớn hơn 10 ngày trong thư mục ~/home, chúng ta dùng lệnh như sau:

bash (non-root)
find ~/home -mtime +10 -daystart
Kết quả
/root/home/website1/wp-includes/css/dist/block-library/editor-rtl.min.css
/root/home/website1/wp-includes/css/dist/block-library/reset-rtl.min.css
/root/home/website1/wp-includes/css/dist/block-library/reset.css
/root/home/website1/wp-includes/css/dist/block-library/reset-rtl.css
/root/home/website1/wp-includes/css/dist/block-library/style-rtl.min.css
/root/home/website1/wp-includes/css/dist/block-library/style.css
/root/home/website1/wp-includes/css/dist/block-library/style.min.css
/root/home/website1/wp-includes/css/dist/block-library/style-rtl.css
/root/home/website1/wp-includes/css/dist/block-library/theme-rtl.css
/root/home/website1/wp-includes/css/dist/block-library/editor-rtl.css
/root/home/website1/wp-includes/css/dist/block-library/theme-rtl.min.css
/root/home/website1/wp-includes/css/dist/editor/style-rtl.min.css
/root/home/website1/wp-includes/css/dist/editor/style.css
/root/home/website1/wp-includes/css/dist/editor/style.min.css
/root/home/website1/wp-includes/css/dist/editor/style-rtl.css
. . .

Tìm kiếm tệp theo phân quyền

Để tìm kiếm tệp theo phân quyền, chúng ta dùng lệnh find với tùy chọn -perm như sau:

bash (non-root)
find /path/to/folder -perm permission

Chú thích tham số lệnh find:

  • /path/to/folder: Là đường dẫn đến thư mục cần tìm kiếm.
  • -perm: Là tùy chọn tìm kiếm tệp theo phân quyền.
  • permission: Là số phân quyền bạn muốn tìm kiếm đối với tệp.

Ví dụ: Để tìm kiếm các tệp có phân quyền 644 bên trong thư mục ~/home/website1 chúng ta dùng lệnh như sau:

bash (non-root)
find ~/home/website1 -perm 644
Kết quả
/root/home/website1/wp-admin/js/widgets/media-audio-widget.js
/root/home/website1/wp-admin/user/profile.php
/root/home/website1/wp-admin/user/credits.php
/root/home/website1/wp-admin/user/admin.php
/root/home/website1/wp-admin/user/freedoms.php
/root/home/website1/wp-admin/user/user-edit.php
/root/home/website1/wp-admin/user/menu.php
/root/home/website1/wp-admin/user/index.php
/root/home/website1/wp-admin/user/privacy.php
/root/home/website1/wp-admin/user/about.php
/root/home/website1/wp-admin/ms-users.php
. . .

Tìm kiếm tệp theo chủ sở hữu

Để tìm kiếm các tệp theo chủ sở hữu, chúng ta dùng lệnh sau:

bash (non-root)
find /path/to/folder -user name

Chú thích tham số lệnh find:

  • /path/to/folder: Là đường dẫn đến thư mục cần tìm kiếm.
  • -user: Tìm kiếm tệp theo tên chủ sở hữu.
  • name: Là tên chủ sở hữu tệp.

Ví dụ: Để tìm kiếm các tệp có phân quyền chủ sở hữu là 123host bên trong thư mục ~/home/website1 chúng ta dùng lệnh như sau:

bash (non-root)
find ~/home/website1 -user 123host
Kết quả
/root/home/website1/index.php

Kết luận

Qua bài viết này chúng ta đã có thêm một số kiến thức cơ bản về lệnh find và có thể áp dụng lệnh find vào thực tế để tìm kiếm tệp và thư mục dựa trên quyền, loại, ngày, quyền sở hữu, kích thước và hơn thế nữa. Hi vọng bài hướng dẫn này sẽ giúp bạn làm việc hiệu quả hơn trên môi trường Linux.


Bạn có làm được theo hướng dẫn này không?

Bài liên quan

Lệnh xác định vị trí của tệp hoặc thư mục trên Linux
Lệnh xác định vị trí của tệp hoặc thư mục trên Linux
Lệnh kiểm tra nhiều cổng port đang mở của một tên miền trên Linux
Lệnh kiểm tra nhiều cổng port đang mở của một tên miền trên Linux
Lệnh tạo một dãy số tăng dần trên Linux
Lệnh tạo một dãy số tăng dần trên Linux

Object Storage

Chuẩn S3, không giới hạn băng thông

Object Storage

Thuê Server Riêng

Chi phí thấp, cấu hình cao

Thuê Server Riêng
Thông tin tác giả
1 Bình luận

Tính năng đang được phát triển

Đang tải bình luận
Liên hệ chúng tôi