Member-only story

計算檔案個數 — 使用 ls -l

--

在 linux 命令列的系統中,想計算檔案個數,可以使用 ls 指令印出目前檔案列表,再算有幾行。

[ec2-user@ip-172-31-42-222 sdfasdc]$ ls -l
total 4
-rw-rw-r-- 1 ec2-user ec2-user 975 Feb 1 17:37 index.html

但因為 ls -l 會印出額外的一行,所以要從 row 2 開始算。

ls -l | tail -n +2 | wc -l
1
Photo by Thomas Bormans on Unsplash

後來發現 ls -1 | wc -l 就可以做到一樣的事情。甚至,ls | wc -l 也可以做到。

但你有沒有想過如果有一百多萬個檔案的時候要怎辦?

--

--

Chen Li-Chi (陳立其)
Chen Li-Chi (陳立其)

Written by Chen Li-Chi (陳立其)

希望所有困難事都可以拆成簡單的事

No responses yet