0

我正在尝试使用管道编写一个 UNIX 命令,该管道将显示我的主目录中的文件数,包括以“。”开头的隐藏文件。

到目前为止,我有:

ls -a .* | wc -l 返回一个整数

我的命令正确吗?

4

4 回答 4

2

While being in current directory:

ls -1 | wc -l

or specify full path:

ls -1 /path/to/dir | wc -l

-note that key for ls is not l, it's 1 - that will skip 'hidden' files (those who starts with .). If you want to include them, then:

ls -1a /path/to/dir | wc -l

-but note, that . (current directory pointer) and .. (parent directory pointer) will be included, so probably you'll want to subtract 2 from result number.

于 2013-09-17T07:13:15.327 回答
1

我的命令正确吗?

不。 话虽如此,除了返回和之外ls -a .*,该命令还将返回以 a 开头的目录中的文件....

为了display the number of files in my home directory including hidden files that begin with a '.',说:

find $HOME -type f | wc -l

如果要将其限制为仅 HOME 目录,请说:

find $HOME -maxdepth 1 -type f | wc -l
于 2013-09-17T07:20:46.700 回答
1

你也可以使用find

find ~ -type f | wc -l

或者

find ~ -type f -maxdepth 1 | wc -l

如果您不想递归查找。

于 2013-09-17T07:23:10.950 回答
0

具有更多管道的 YA 非递归命令:

ls -la | awk '{ print $1 }' | grep -v total | grep -v d | wc -l

最好的非递归 - 和上面的同事一样,但为了避免警告,请将 maxdepth 放在 type 选项之前:

find ~ -maxdepth 1 -type f | wc -l

递归:

find ~ -type f | wc -l
于 2013-09-17T09:21:49.363 回答