![]() These commands can be used in everyday life while using the Linux system.Drowning in a sea of files and wishing for an easier way to sift through it all? It’s time to master your file system with grep! Think of grep as a metal detector for your data, pinpointing the exact files you need.īut grep has a secret weapon: the ability to exclude certain patterns from your search results. We discussed three practical ways to exclude directories when recursing through the file system. txt : This is sample text from tdir2 / file2. txt : This is sample text from tdir1 / file1. We can provide a listing of directories in curly braces. You can use an alternative syntax to achieve the same result. In the above example, the grep command searches for a pattern in all directories except tdir1, tdir2, and tdir3. $ grep - R "sample" - exclude - dir = tdir1 - exclude - dir = tdir2 - exclude - dir = tdir3 ![]() You can specify multiple − exclude−directories options to exclude multiple directories. If you are looking for a literal asterisk (*), you should escape it by putting a backslash before it. You can use either the * or? characters to represent a single character. For example, let’s say you have two directories that you want to exclude − If you wanted to exclude more than one directory, you could combine them into one string using the pipe character (|). In the above example, the grep command searches for a pattern in all directories except tdir1. log : This is sample text from apache - logs / apache. txt : This is sample text from tdir3 / file3. log : This is sample text from logs / service. We can use the −exclude−dir option of the grep command to exclude a directory − $ grep - R "sample" - exclude - dir = tdir1 ![]() Let’s now look at the directory tree we just created − $ tree - h. $ echo "This is sample text from apache-logs/apache.log file" > apache - logs / apache. $ echo "This is sample text from logs/service.log file" > logs / service. $ echo "This is sample text from tdir3/file3.txt file" > tdir3 / file3. $ echo "This is sample text from tdir2/file2.txt file" > tdir2 / file2. $ echo "This is sample text from tdir1/file1.txt file" > tdir1 / file1. $ mkdir tdir1 tdir2 tdir3 logs apache - logs We're going to create some files and folders to use as an illustration. So, if you want to exclude the /home directory, you could use − grep - rd '/home' / path / to /* To exclude a single directory, you need to include the −d flag. This will find all files in the specified directory or any sub−directories. For example − grep - r 'pattern' / path / to / directory1 /* The simplest way to do this would be to simply add an exclusion directory name to the end of the file path. We’ll discuss the different ways to achieve this. Grep can be used with the −r option which will allow you to specify multiple patterns and then use the −v option to show only those files that match your pattern. ![]() This is useful when searching through large amounts of data. A feature that allows you to exclude certain directories from recurrence. The grep command provides some additional functionality that makes the search even better. We often run a grep command to look for specific strings of text within files.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |