

The option H tells grep to print the file name when it finds the text (something it doesn’t normally do if it searches a single file). Use the tr command to convert all uppercase letters to lowercase letters, then use the egrep command to grab all the words in the text and output them item by item.The escaped semicolon “\ ” tells -exec where the command ends. name "*.js" -exec grep -iH foo have been replaced with the name. Solution: let “find” call “grep”, as follows.įind. Problems: With many files, the command can become too long and grow beyond the maximum allowed number of characters.įurthermore, the above doesn’t work well with directories whose names have spaces in them. The option i ensure that the search is case-insensitive. Go through a list of files, search for the text “foo”:.Insert that list after “foo”: backquotes.Assemble a list of all JavaScript files in the current directory:.The easiest way to do this is to execute the command The given expression is then run on each of the files found in the paths. If no paths are specified, then the current directory is used. This is followed by the set of paths to search in.

#HOW TO FIND WORD IN FILE UNIX COMMAND HOW TO#
In a previous version of himself he wrote books on technology.Ĭontent is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.This post shows you how to use “find” and “grep” to search for a text string in all files that are directly or indirectly contained in a given directory.Įxample: Find all JavaScript files in the current directory and search for the word “foo”. Find Command in Unix Syntax: find options paths expression The options for this command are used to specify how symbolic links should be treated. He is interested in people, music, food and writing. You can edit it here and send me a pull request.
#HOW TO FIND WORD IN FILE UNIX COMMAND UPDATE#
Have an update or suggestion for this article?

It supports searching by file, folder, name, creation date, modification date, owner and permissions. It can be used to find files and directories and perform subsequent operations on them. The find command in UNIX is a command line utility for walking a file hierarchy. It tells the shell not to treat the wildcard character as a wildcard when interpreting the command line arguments. The following command finds all files with the word maybe in it: find / -name maybe -print The backslash character is important.
