[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
lsalone, it formats the output in columns to save space on your screen. That’s why you seeaaa a.zip b.jwt d.09...all on what looks like one or two lines. -
To a Pipe (
|): Whenlsdetects that its output is being sent to another program (likewc), 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.

浙公网安备 33010602011771号