![]() 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. In the next step, a while loop will take over the control. ![]() -r: Read all files under each directory recursively.Here, we’ve used three of grep‘s options: Grep is a very powerful tool and accepts various command line arguments. 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 command you should be using, in this case, is grep. ![]() Using the grep command and the xargs commandįirst, we examine the log files’ content to find the files to move.The grep command’s recursive option is used to search all paths and subdirectories for the specified term in all files and child files. Using the grep command and a while loop The grep command is a fantastic resource for browsing the contents of all directories and subdirectories.Today, we’ll address three approaches to achieve that: So, our requirement is to find those log files containing “ security alert” entries and move them to a new directory in one single command. Also, some log files are case-insensitively containing “ security alert” logs. Running grep 'string' /path/to/dir is supposed to be able to do this, I've read, but it gives me the error: grep: dir: Is a directory Next, I tried running grep on multiple files. These log files are lying in different directories. 56 I want to search for a string of text in all files in a directory (and not its subdirectories I know the -r option does that, but that is not what I want). Īs we can see in the output above, we have four log files for two applications – app1 and app2. 22:08:14 security alert: 10 times failed login from the same IP. => logs/app1/app1.log logs/app1/app1_user.log logs/app2/app2.log logs/app2/app2_user.log <= Let’s first have a look at some log files under the logs directory: $ head logs/**/*.log Usually, an example can explain a problem quickly.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |