
The –exclude-dir option is used to specify the excluded directory names. But in some cases, we may want to exclude some directories or files from the text search in Linux. There is no exception excluded from the search. The text search with the grep command is executed in all files under the specified path. grep -rni '/etc/init.d' -e 'kernel' Exclude Directories From Search The -i option is provided to enable case-insensitive text search. But in some cases, we may need to search in a case insensitive way where “A” is interpreted as “a” too. This means upper case and lower case are interpreted as different. The grep command searches files in a case-sensitive manner. grep -rnw '/etc/init.d' -e 'kernel' CaseInsensitive Search Text In Files The -w option is provided for the whole word match not some part of the word. But we can specify more strict match which is called word match.

For example the “kernel” term matches “kernel”,”mykernel”,”kernels” etc. grep -rn '/etc/init.d' -e 'kernel' Search Whole Word In Filesīy default provided term is searched in some part of the word. The grep command provides a lot of features and options where search can be done in a single file, multiple files, specific path, or specific file extensions. The grep command is the most popular and useful command in order to search text in files. There are different commands that can be used to search multiple files in multiple directories. Searching text in multiple files in different ways is one of them. If you know other similar tools, do not hesitate to write them in the comments section below.Linux is a very flexible operating system that provides a lot of different actions. For small files, you can use the less command, or the echo command. You have seen some simple utilities that allow you to display a text file on the command line.
#LINUX COMMAND SEARCH FOR TEXT IN FILES HOW TO#
Read: How to send one-liner emails from Linux/Ubuntu terminal You can even perform a search on the text. You also have the possibility to scroll forwards and backwards through the text. The grep command is handy when searching through large log files. When it finds a match, it prints the line with the result. The text is shown one screenful at a time. Grep is a Linux / Unix command-line tool used to search for a string of characters in a specified file. The more command enables you to display the contents of text files in a scrollable manner. Using headĪpplying head to the file output_in_html will give the following result :

Which displays the last lines of the provided file. Īpplying tail to the file output_in_html will give the following result : Much like the previous command, the parenthesis ( ) are used to return the value of a given expression which in this case, is the contents of file_name.

Read: How to display Images in the command line in Linux/Ubuntu The parenthesis ( ) are used to return the value of a given expression which in this case, is the contents of file_name. This is a pager which is useful to view long files for instance:įor instance the command below will display the content of the file output_report.txt :īy passing the -F and -X switches, less can behave like cat when applied to small files but will act normally otherwise.Īpplying cat to the same file that we used above. In this short tutorial, we cover see some simple commands that will help users learn how to open a file in linux or view its contents on the command line.
