使用 grep 和 find 命令 - 帮助我用简单的头脑解决问题的基本问题
我带着第二个显而易见的问题回来了,但我想把这个问题弄清楚。
我有一项任务,负责提供一个命令来查找我的主目录中名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ls ~ | grep 测试
ls ~ | grep test
。
find ~/test
还将与目录$HOME/test/
下的每个文件或目录进行匹配>。而是使用find ~ -type f -name test
。No.
find ~/test
will also have a match for every file or directory under the directory$HOME/test/
. Rather usefind ~ -type f -name test
.任务听起来不清楚。但是,是的,如果您为
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? Maybels
as @Reese suggested, or maybe a differentfind
command.说明:
ls -R ~
将递归列出主文件夹中的所有文件和目录。grep test
会将列表范围缩小到名称中包含“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.