使用 grep 和 find 命令 - 帮助我用简单的头脑解决问题的基本问题

发布于 2024-09-30 07:29:21 字数 192 浏览 0 评论 0原文

我带着第二个显而易见的问题回来了,但我想把这个问题弄清楚。

我有一项任务,负责提供一个命令来查找我的主目录中名为 test 的文件(一个命令使用 find,一个命令使用 grep)。我知道使用 find 只是“find ~/test”,但是使用 grep,我不需要在文件“test”中搜索模式吗?或者有没有办法搜索文件(使用 grep),即使文件是空的?

I am back with a second no-brainer question, but I would like to get this straight in my head.

I have an assignment in which I am charged with providing a command to find a file named test in my home directory (one command using find, and one using grep). I understand that using find is just 'find ~/test', but using grep, wouldn't I have to search out a pattern within the file 'test'? Or is there a way to search for the file (using grep), even if the file is empty?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀子花开つ 2024-10-07 07:29:21

ls ~ | grep 测试

ls ~ | grep test

这样的小城市 2024-10-07 07:29:21

我知道使用 find 只是“find ~/test”

find ~/test 还将与目录 $HOME/test/ 下的每个文件或目录进行匹配>。而是使用 find ~ -type f -name test

I understand that using find is just 'find ~/test'

No. find ~/test will also have a match for every file or directory under the directory $HOME/test/. Rather use find ~ -type f -name test.

北渚 2024-10-07 07:29:21

任务听起来不清楚。但是,是的,如果您为 grep 提供任何文件名,它会查看文件的内容并忽略文件的名称。也许你可以 grep 另一个命令的输出?也许像@Reese建议的那样ls,或者可能是不同的find命令。

The assignment sounds unclear. But yes, if you give any filenames to grep, it will look at the contents of the files and ignore the names of the files. Perhaps you can grep the output of another command? Maybe ls as @Reese suggested, or maybe a different find command.

向地狱狂奔 2024-10-07 07:29:21
ls -R ~ | grep test

说明:ls -R ~ 将递归列出主文件夹中的所有文件和目录。 grep test 会将列表范围缩小到名称中包含“test”的文件(和目录)。

ls -R ~ | grep test

Explanation: ls -R ~ will recursively list all files and directories in your home folder. grep test will narrow down that list to files (and directories) that have "test" in their name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文