![]() ![]() The last line shows that the file example_file2 contains the string FINDME. Here’s how you can find which one using the grep command: example]$ example]$ grep FINDME * Going back to the files and subdirectories I have created under /var/tmp/example/, I have added the string FINDME inside one of the files. The syntax of the grep command is: grep PATTERN FILE If you want to know which files on your filesystem match a specific string, the grep command is the answer to that. The grep command can find patterns inside the content of files. Now, let’s have a look at how the grep command works. Grep Command: Search For Patterns Inside Files The find command allows to find files and directories in the Linux filesystem based on a search criteria provided by the user. I have written an article that explains the find command more in detail, have a look at it. regular file, directory, socket, name pipe, etc…) The expression part of the command can be used to filter the output of the find command based on other criteria. The expression also contains the -name flag that allows to filter the name of files and directories found by the find command: example]$ find /var/tmp/example/ -name *file1Īs you can see this time the find command only returns the file /var/tmp/example/example_file1. Now let’s add an EXPRESSION to filter the output based on the name *file1 (the expression we use to select the output coming from the find command can also contain wildcards). In the output of the find command I see all the files and subdirectories inside the /var/tmp/example directory. This is the output of the find command where path is /var/tmp/example/: example]$ find /var/tmp/example/ ![]() I have created a test directory (called example) under /var/tmp. It’s easier to understand with an example… The fact that the EXPRESSION is surrounded by square brackets indicates that the expression is optional. It goes through the directory tree recursively for each path provided and it returns files or directories that match the EXPRESSION. What does the find command do exactly when we run it? The generic syntax of the find command according to its manual is the following: find PATH In this case the criteria is that the filename matches “findme.txt”. ![]() This is the perfect opportunity to use the find command that, as mentioned in the previous section, allows to find files and directories on Linux based on certain criteria. I want to find a file called findme.txt on my Linux system but I don’t know the exact directory for the file. Let’s have a deeper understanding of this with some examples! Find Command: Search For Files Based on Their Name based on their name) while grep returns files whose content matches the pattern you are looking for. ![]() In other words, find returns files and directories in the filesystem based on your search criteria (e.g. The difference can be subtle if you don’t have a lot of experience with Linux. filename, modify date, size, file type, etc…), grep is a utility to search for patterns in the content of files or in the output of other commands. What is the difference between the grep and find commands?įind is a utility to search for files and directories in the Linux filesystem based on certain criteria (e.g. It seemed obvious to me, but maybe it’s not if you are getting started with Linux. I have heard people asking what is the difference between grep and find multiple times in the last few years. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |