shell script for linux


i want to run the script to run the cat and the grep command and get the data.

then run the cronjob to ecxceute the script and mail the results of the script in form of alerts.

is there a way i can do that?



Hi @karan564,

You might need to provide a bit more information in order for other community members to better help you. Can you show us the work you’ve done so far, or share what you’ve tried already?

i am using the command on the linux to get the results.

cat ./* | grep “xyz”

is there a script that can run the above command and the automate the process through cronjob.


To run a command periodically, you need to create a crontab, which contains all commands to run periodically and when they should run. If a cron service is already configured and your user has permission to manage its own crontab, you should be able to edit your crontab by executing

crontab -e

in your shell. This will open an editor where you can edit the crontab file. When you save and exit your editor, the new crontab is activated.

For instance, this crontab would execute such a command every five minutes and, provided there is a functioning local mailer installed on the machine, mail any output to
*/5 * * * * cat /path/to/directory/* | grep "xyz"

These things are documented extensively in manpages, accessible through the man command, e.g.:

man crontab

Reading these is a good way to understand what is possible with all these tools. You would be able to use grep alone to search for things, e.g.

grep -n xyz /path/to/directory/*

 would do the same as your cat | grep combo, but also tell where in which file each match is found.