Finding files with find · Tìm tệp với find
Searching the whole tree
lsshows one folder.findsearches a folder and everything inside it, however deep:
.means "start in the current folder".findlists every file and folder it can reach.
Tìm kiếm toàn bộ thư mục
lschỉ hiển thị một thư mục.findsẽ tìm kiếm trong một thư mục và tất cả mọi thứ bên trong, dù sâu đến đâu:
find .
.có nghĩa là "bắt đầu từ thư mục hiện tại".findliệt kê mọi file và thư mục mà nó có thể truy cập.
Find by name
- Usually you want files matching a pattern. Use
-name:
- This finds every
.txtfile in the tree — even ones buried three folders down. - Quote the pattern (
"*.txt") so the shell passes it tofinduntouched.
Tìm theo tên
- Thông thường bạn muốn tìm các file khớp với một mẫu. Hãy dùng
-name:
find . -name "*.txt"
- Lệnh này tìm mọi file
.txttrong cây thư mục — kể cả những file nằm sâu ba lớp thư mục. - Đặt mẫu trong dấu ngoặc kép (
"*.txt") để shell truyền nó nguyên vẹn chofind.
Why it is handy
- "Where did I put that file?" —
findanswers it in one line. - Combined with pipes, you can act on the results — for example count them with
wc -l.
Tại sao nó tiện lợi
- "Tôi đã để file đó ở đâu?" —
findtrả lời ngay trong một dòng. - Kết hợp với đường ống, bạn có thể xử lý kết quả — ví dụ: đếm số lượng bằng
wc -l.
Common mistakes
find . -name "*.txt"searches by name from the current folder down.- Quote the pattern so the shell does not expand it first.
Lỗi thường gặp
find . -name "*.txt"tìm kiếm theo tên từ thư mục hiện tại xuống dưới.- Đặt mẫu trong dấu ngoặc kép để shell không tự động mở rộng (expand) nó trước.
find walks the tree · find duyệt qua cây
find searches the whole tree below a directory. · find tìm kiếm toàn bộ cây dưới một thư mục.
Find every .txt file, even deep inside subfolders, with find . -name "*.txt". (. means "start from here".) · Tìm mọi tệp .txt, kể cả sâu bên trong các thư mục con, với find . -name "*.txt". (. có nghĩa "bắt đầu từ đây".)
You can also start the search from a subfolder. Find just the .txt files inside docs (and below) with find docs -name "*.txt". · Bạn cũng có thể bắt đầu tìm kiếm từ một thư mục con. Tìm chỉ các tệp .txt bên trong docs (và bên dưới) với find docs -name "*.txt".