Tìm Kiếm File Và Thư Mục Trong Linux - Tips & Tricks FIND Command

Skip to content Search Gists Search Gists All gists Back to GitHub Sign in Sign up Sign in Sign up Dismiss alert {{ message }}

Instantly share code, notes, and snippets.

@hoangdh hoangdh/find-command.md Last active January 7, 2021 16:14 Show Gist options
  • Star (1) You must be signed in to star a gist
  • Fork (2) You must be signed in to fork a gist
  • Embed Select an option
    • Embed Embed this gist in your website.
    • Share Copy sharable link for this gist.
    • Clone via HTTPS Clone using the web URL.

    No results found

    Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/hoangdh/625c36647500d3bf1a9609d03a3e6e71.js"></script>
  • Save hoangdh/625c36647500d3bf1a9609d03a3e6e71 to your computer and use it in GitHub Desktop.
Code Revisions 11 Stars 1 Forks 2 Embed Select an option
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.

No results found

Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/hoangdh/625c36647500d3bf1a9609d03a3e6e71.js"></script> Save hoangdh/625c36647500d3bf1a9609d03a3e6e71 to your computer and use it in GitHub Desktop. Download ZIP Câu lệnh find - Tìm kiếm file và thư mục trong linux - Tips & Tricks FIND command Raw find-command.md

Ghi chép tùy chọn của find

  • Các tùy chọn hay dùng:
    • -name: Tìm kiếm theo tên file/folder
    • -type: Theo kiểu, f: Folder; d: Directory
    • -mtime N: Thời gian tạo/chỉnh sửa. N: là số ngày
    • -exec: Thực thi gì đó với các file tìm được
    • -perm: Tìm theo quyền truy cập (RWX)
    • -user: Tìm file thuộc sở hữu của user nào đó
    • -group: Tìm file sở hữu của group nào đó
    • Tham khảo thêm: man find

Một số trường hợp cụ thể

Xóa (Truncate) các file log lớn của Docker

find /var/lib/docker/containers/ -size +1G -iname '*json.log*' -type f -exec truncate -s 0 {} \;

Tìm file theo dung lượng lớn hơn 100M

find . -type f -size +100M -exec ls -sh {} \;

Chú ý: Nếu muốn tìm dung lượng nhỏ hơn -100M

Tìm file nào đó trong thư mục

find ./folder -print | grep -e '<tên-file>'

Tìm file nào đó có quyền thực thi

find ./folder -executable

Tìm các file mới được tạo sau 1 ngày

find / -atime -1

Tìm các file có dung lượng > 100M và mới tạo sau 1 ngày

find / -type f -size +100M -atime -1 -exec du -sh {} \;

Xóa các file được tạo sau 15 ngày

find /data/logs/ -type f -mtime +15 -exec rm -rf {} \;

Tìm các file được tạo trong vòng 15 ngày trở lại

find /data/logs/ -type f -mtime -15 -exec ls -l {} \;

Đếm số file trong thư mục

find . -type f | wc -l

Đếm sub-directory trong thư mục

find . -type d | wc -l

Đếm tổng số file trong thư mục

find . -print| wc -l

Tìm các file có quyền khác với 644 và thay đổi

find /tmp -type f ! -perm 644 -exec chmod 644 {} \; Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.

Từ khóa » Cách đếm File Trong Thư Mục