惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
博客园 - 司徒正美
I
InfoQ
宝玉的分享
宝玉的分享
G
Google Developers Blog
J
Java Code Geeks
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
罗磊的独立博客
腾讯CDC
F
Fortinet All Blogs
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
B
Blog RSS Feed
博客园 - 聂微东
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏

hwchiu learning note Blog

Kubernetes 怎麼計算 imageFS | hwchiu learning note Nginx Proxy_Pass 不會重新查詢 DNS | hwchiu learning note Multus 下如何透過 network policy 設定 | hwchiu learning note Linux Bridge MTU | hwchiu learning note Kubevirt 初體驗 | hwchiu learning note [MacOS ]隨手筆記 Sed 與 Rename 的使用 | hwchiu learning note Docusaurus 使用 blog mode 後連結一直反白的問題 | hwchiu learning note gcloud 切換帳號 | hwchiu learning note k8s 內安裝 redis-cluster | hwchiu learning note Helm Chart 中如何根據條件來動態安裝 Template 內的物件 | hwchiu learning note GCS 操作上注意事項 | hwchiu learning note istio 操作記錄 | hwchiu learning note terraform | hwchiu learning note Kubernetes GKE 維運上小筆記 | hwchiu learning note Git 修改 author/committer | hwchiu learning note GCP NAT 相關筆記 | hwchiu learning note gcloud ssh 到 GCP VM | hwchiu learning note CloudSQL 收費注意事項 | hwchiu learning note Loki 安裝上的參數調整以及 Ring 的問題除錯 | hwchiu learning note kustomize + helm | hwchiu learning note 觀測 K8s 內 OOM 事件 | hwchiu learning note GKE 上的 RBAC 筆記 | hwchiu learning note 本地產生 jwt token | hwchiu learning note ArgoCD 安裝筆記 | hwchiu learning note CircleCI Context 的使用 | hwchiu learning note 透過 GCP IAP Gateway 來保護 GKE 內的網站 | hwchiu learning note 閱讀筆記: 「SRE 的工作介绍」 | hwchiu learning note 閱讀筆記: 「DevOps is a failure」 | hwchiu learning note 閱讀筆記: 「面試人生 - 設計一個簡易的分散式 Job Scheduler」 | hwchiu learning note 閱讀筆記: 「Cloudflare 06/21 災後報告」 | hwchiu learning note
Shell Script 筆記 | hwchiu learning note
HungWei ChiuBlogger · 2013-11-24 · via hwchiu learning note Blog

· 3 min read

HungWei Chiu

本篇文章是用來記錄以前修課關於 Shell Script 的作業

Introduction

用Unix的指令,透過pipe的方式完成下列要求

  • 計算當前目錄底下資料夾的總數
  • 計算當前目錄底下檔案總數
    • 只有計算regular file.不考慮FIFO、LINK
  • 計算所有檔案大小和 (Byte)
  • 顯示前五大的檔案名稱
  • 只能使用PIPE,不能使用 $$ ; || & > >> <

Implement

  • 使用ls來取得所有資料夾跟檔案的資訊
    • -l 可以顯示詳細資訊,這邊我們要取得的是 檔案大小
    • -R 遞迴的往每個資料夾繼續往下找
    • -A 不要把...給顯示出來,因為這種當前目錄的東西我們不需要
  • 使用sort來幫忙排序,找出檔案大小前五個
    • -r 排序結果反過來,由大到小排序
    • -n 排序的時候,採用數字的方式去排序,不使用字母大小去排序
    • -k 指定第幾個欄位要排序
  • 使用awk作最後的處理,找出前五大,印出所有檔案大小和
    • 因為再ls -l的結果中,會有很多資訊,包含 ./cs/.svn/pristine/74: 或者 total 28,所以awk再處理的時候,先用NF判斷該行的欄位數,至少要有9個欄位才處理 if(NF>=9)
    • 接下來針對檔案是資料夾還是檔案,做全部的計數,可以由 -rw-r--r-- 的第一個欄位來決定,如果是d就代表資料夾,否則就是檔案。 這邊我使用 regular expression來判斷 ($1 ~/^d/)? (dir=dir+1) : (file=file+1)(size=size+$5),此外如果是檔案得話,就順便把大小也計算一下
    • 執行過程中,因為剛剛已經排序過了,所以 前六行都把大小印出來, if(NR<6) print NR": "$5" "$9}
    • 最後就把所有資訊都列印出來

ls -RlA | sort -rnk 5 | awk '{ if(NF>=9) ($1 ~/^d/)? (dir=dir+1) : (file=file+1)(size=size+$5); if(NR<6) print NR": "$5" "$9} END{ print "Dir = "dir"\n" " File = " file"\n" "total = "size}'