ZhangZhihui's Blog  
[zzh@rext03-0322 ~]$ ls ~/project/deploy/mirrored
aaa  a.zip b.jwt d.09_202211071.tgz
[zzh@rext03-0322 ~]$ ls ~/project/deploy/mirrored | wc -l
4
[zzh@rext03-0322 ~]$ ls ~/project/deploy/mirrored | grep "\.zip"
a.zip

 

The ls Behavior (Terminal vs. Pipe)

The ls command is "smart" and changes its output format depending on where it is sending the data:

  • To a Terminal (Directly): When you run ls alone, it formats the output in columns to save space on your screen. That’s why you see aaa a.zip b.jwt d.09... all on what looks like one or two lines.

  • To a Pipe (|): When ls detects that its output is being sent to another program (like wc), it automatically switches to one file per line.

So, when you run ls | wc -l, ls actually sends this to wc:

aaa
a.zip
b.jwt
d.09_202211071.tgz

Since there are 4 distinct items, wc -l (which counts newlines) counts 4 lines.

 

posted on 2022-11-08 11:01  ZhangZhihuiAAA  阅读(29)  评论(0)    收藏  举报