簡體   English   中英

Linux:如何列出有關文件或目錄的信息(大小,權限,按類型划分的文件數?)

[英]Linux:How to list the information about file or directory(size,permission,number of files by type?) in total

假設我留在當前目錄中,我想列出所有文件的總數,以及大小、權限以及按類型划分的文件數。

這是示例輸出:

這是一個示例:

打印有關“/home/user/poker”的信息

文件總數:83

pdf文件:5

html文件:9

文本文件:15

未知:5

注意:任何沒有擴展名的文件都可以被認為是未知的。

我希望使用一些簡單的命令,如 ls、cut、sort、unique ,(只是示例)將每個不同的擴展名放在文件中並使用 wc -l 來計算行數

還是我需要使用 grep、awk 或其他東西?

希望得到大家的建議。謝謝!

最好的方法是使用file僅輸出 mimetype 並將其傳遞給awk

file * -ib | awk -F'[;/.]' '{print $(NF-1)}' | sort -n | uniq -c

在我的主目錄上,它會生成此輸出。

 35 directory
  3 html
  1 jpeg
  1 octet-stream
  1 pdf
 32 plain
  5 png
  1 spreadsheet
  7 symlink
  1 text
  1 x-c++
  3 x-empty
  1 xml
  2 x-ms-asf
  4 x-shellscript
  1 x-shockwave-flash

如果你認為text/x-c++text/plain應該在同一個使用這個

 file * -ib | awk -F'[;/.]' '{print $1}' | sort -n | uniq -c

  6 application
  6 image
 45 inode
 40 text
  2 video

根據需要更改{print $1}部分以獲得適當的輸出。

你需要bash

files=(*)
pdfs=(*.pdf)

echo "${#files[@]}"
echo "${#pdfs[@]}"
echo "$((${#files[@]}-${#pdfs[@]}))"

find . -type f | xargs -n1 basename | fgrep . | sed 's/.*\\.//' | sort | uniq -c | sort -n

這為您提供了文件擴展名的遞歸列表。 如果您只想要當前目錄,請在 find 命令中添加-maxdepth 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM