Of course, before we move the files, we should create the target directory: $ mkdir logs/security_logs_loop & grep -lir 'security alert' logs | while read log do mv "$log" logs/security_logs_loop doneĪs we can see in the tree output above, after the command’s execution, the three log files have been moved to the target directory. It’ll read each filename from grep‘s result and move the file to the target directory. grep -f pattern.txt textfile.txt This is a sample text file. grep -f patternfile filetomatch In our example, we’ve created pattern file names pattern.txt with the below contents: cat pattern.txt This It. In the next step, a while loop will take over the control. The file should contain one pattern per line. ![]() -r: Read all files under each directory recursively.Here, we’ve used three of grep‘s options: ![]() Pathnames are written once per file searched. From the Unix standard: -l (The letter ell.) Write only the names of files containing selected lines to standard output. The grep command is a good choice for this task: $ grep -lir 'security alert' logsĪs grep‘s output shows, we’ve found the three log files containing “ security alert“. The standard option grep -l (that is a lowercase L) could do this. How can I change the command to print the file name only once rather than having the same file name.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |