$ grep -rl -include='*ll*' -exclude='*.sh' 'He'Īs mentioned earlier, these options can be used even when recursive search isn't active. # files ending with '.sh' are excluded as expected # here, exclude gets countered by the include option If you mix -include and -exclude options, their order of declaration matters. $ grep -rl -include='*.txt' -include='.hi*' 'blue' # allow only filenames ending with '.txt' or starting with '.hi' $ grep -rl -exclude-dir='backups' -exclude='.*' 'blue' # excluding 'backups' directory and hidden files $ printf '*.txt\n.hi*' | grep -rl -exclude-from=- 'blue'Įach of these options can be used multiple times to narrow your search. # exclude filenames ending with '.txt' or starting with '.hi' # search only filenames ending with '.txt' Here are some basic examples: # without filtering For more information about globs, see this mywiki.wooledge article. When recursive options are used, the GLOB applies only to the basename of a file or directory, not the entire path. These are NOT the same as regular expressions. GLOB here refers to wildcard patterns (also known as globs) used by the shell to expand filenames. Option Description -include=GLOB search only files that match GLOB (a file pattern) -exclude=GLOB skip files that match GLOB -exclude-from=FILE skip files that match any file pattern from FILE -exclude-dir=GLOB skip directories that match GLOB To aid in such custom searches, four options are available: There are situations, such as version controlled directories, where specific paths should be ignored or all the files mentioned in a specific file should be ignored. $ grep -rl 'pwd' backups projects/dot_filesīy default, the recursive search options -r and -R will include hidden files as well. # link provided as an argument will be searched even with -r The -R option will follow links even when they are not part of the argument list. If links are provided as part of the argument list, grep will perform a search within that path even if only the -r option is used. # show all matching lines containing 'clear'īackups/dot_files/.bash_aliases:alias c=clear Here are some basic examples: # current directory is the default path to be searched recursively By default, the current directory will be used if there's no path specified. When the above options are used, any directory in the argument list will be searched recursively. r and -R will work as if -H option was specified as well, even if there is only one file found during the recursive search. Read all files under each directory, recursively. This is equivalent to the -d recurse option. That if no file operand is given, grep searches the workingĭirectory. Symbolic links only if they are on the command line. Read all files under each directory, recursively, following Sample directoryįor sample files and directories used in this chapter, go to the example_files directory and source the grep.sh script. The example_files directory has the script used to create the sample directory for this chapter. You'll also learn how to pass the files filtered by grep to other commands for further processing. Shell globs and the find command are also discussed to show alternate methods. This chapter will cover recursive search options and ways to filter the files to be searched.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |