find directory on linux

Cách tìm thư mục trên Linux

Cú pháp tìm lệnh là:

find /where/to/look/up criteria action

Hoặc

find /dir/path/look/up criteria action

Hoặc

find /dir/path/look/up -name "dir-name-here"

Hoặc

find /dir/path/look/up -name "pattern"

Hoặc

find /dir/path/look/up -name "dir-name-here" -print

Hoặc

find /dir/path/look/up -name "dir-name-here"

Hoặc

find / -name "dir-name-here"

Hoặc

find / -type d -name "dir-name-here"

Hoặc

find / -type d -name "dir-name-here" 2>/dev/null

Tìm thư mục lệnh Linux

Ví dụ sau sẽ hiển thị tất cả các tệp trong thư mục hiện tại và tất cả các thư mục con:

find
find .
find . -print

Tìm một thư mục

Để tìm thư mục có tên apt trong /(root) hệ thống tệp, nhập:

find / -type d -name “apt”
sudo find / -type d -name “apt”

Đầu ra như sau:

/var/log/apt 
/var/lib/apt 
/var/cache/apt 
/etc/apt /etc/logrotate.d/apt 
/etc/cron.daily/apt

Xử lý “Permission denied error messages” trên Linux

Tìm sẽ hiển thị một thông báo lỗi cho mỗi thư mục / tệp mà bạn không có quyền đọc
Find sẽ hiển thị một thông báo lỗi cho mỗi thư mục / tệp mà bạn không có quyền đọc

Để tránh những tin nhắn đó, hãy thêm 2>/dev/null vào cuối mỗi lệnh:

find /where/to/look/ criteria action 2>/dev/null
sudo find / -type d -name “apt” 2>/dev/null

Làm thế nào để tìm một thư mục có tên Documents trên Linux?

Nhập lệnh sau để tìm kiếm thư mục Tài liệu trong thư mục $ HOME của bạn:

find $HOME -type d -name Documents

Kết quả đầu ra:

/home/khaind/Documents

Lấy danh sách chi tiết các tập tin / thư mục

Truyền -ls vào danh sách tập tin hiện tại ở định dạng đầu ra lệnh ls:

find / -name “apt” -ls

Đầu ra mẫu:

4719035    4 drwxr-xr-x   2 root     root         4096 Aug 22 06:25 /var/log/apt
4718597    4 drwxr-xr-x   5 root     root         4096 Aug  4 13:46 /var/lib/apt
4718601    4 drwxr-xr-x   3 root     root         4096 Aug  8 09:37 /var/cache/apt
917524    4 drwxr-xr-x   6 root     root         4096 Jun 18 02:28 /etc/apt
917721    4 -rw-r--r--   1 root     root          173 Apr 15  2011 /etc/logrotate.d/apt
918762   16 -rwxr-xr-x   1 root     root        14985 Mar 14 12:48 /etc/cron.daily/apt

Làm thế nào để chỉ liệt kê các thư mục?

Chỉ cần tìm thư mục và bỏ qua tên tệp với tùy chọn -type d như sau:

find / -type d -name “apt” -ls

Đầu ra như sau:

4719035    4 drwxr-xr-x   2 root     root         4096 Aug 22 06:25 /var/log/apt
4718597    4 drwxr-xr-x   5 root     root         4096 Aug  4 13:46 /var/lib/apt
4718601    4 drwxr-xr-x   3 root     root         4096 Aug  8 09:37 /var/cache/apt
917524    4 drwxr-xr-x   6 root     root         4096 Jun 18 02:28 /etc/apt

Làm thế nào để tìm kiếm những trường hợp đặc biệt?

Thay thế tùy chọn -name thành -iname như sau:

find / -type d -iname “apt” -ls

HOẶC LÀ

find / -type d -iname “apt”

Các mẫu ‘apt’ khớp với các tên thư mục ‘apt’, ‘APT’, ‘Apt’, ‘apT’, v.v.

Làm thế nào để tôi tìm thấy một thư mục được gọi là project.images?

Nhập bất kỳ một trong các lệnh sau:

find / -type d -iname “project.images” -ls

HOẶC LÀ

find / -type d -name “project.images” -ls

HOẶC LÀ

find / -type d -name “project.images”

Cũng có thể sử dụng wild cards như sau:

find / -type d -name “project. *” find / dir / to / search / -type d -name “project.image ??”

Một lưu ý về lệnh xác định vị trí

Để tìm kiếm một tệp/thư mục có tên chính xác là project.images (không phải * project.images *), gõ :

locate -b ‘\project.images’
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments