Why count differs between ls and ls -l linux command?

落爺英雄遲暮 提交于 2019-12-01 12:32:49

问题


I had a directory with number of files and need to check the count of files present in it.

I tried the following two commands:

ls | wc -l
ls -l | wc -l

and found there are differences while using both commands. (ie. number of files is greater in the usage of second command while comparing to the first command.)

I would like to know the changes happening in the both commands.


回答1:


From man ls:

-l      (The lowercase letter ``ell''.)  List in long format.  (See below.)  If the output is to a terminal, a total sum for all the file sizes is output on a line before the
         long listing.

So ls -l adds a header line stating the "total" size of files:

$ ls -l /
total 65
-r--r--r--   1 root  wheel  6197 May 11 21:57 COPYRIGHT
drwxr-xr-x   2 root  wheel  1024 Jun  1 16:02 bin
drwxr-xr-x   9 root  wheel  1536 Jun  1 16:02 boot
dr-xr-xr-x   8 root  wheel   512 Jul  7 20:16 dev
.......


来源:https://stackoverflow.com/questions/38348582/why-count-differs-between-ls-and-ls-l-linux-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!