How to Create a Bash Script to Find Files with Specific Words?

0
3
Asked By CuriousCoder92 On

Hey everyone! I'm currently trying to write a Bash script that searches through the entire Linux filesystem (from / down) for files containing the terms "secret" or "confidential". I need help with a few tasks:

1. Count how many files contain each of those words.
2. Count the total occurrences of those words across all files.
3. List the names of the files that contain them.

I've been using 'grep -ril' with ' --exclude-dir' to skip system directories like /proc and /sys, and while the grep command itself works fine, I'm having trouble with getting my loop and counting part to work correctly. Any tips on structuring this safely and accurately would be hugely appreciated.

By the way, I know this is homework-related, but I've spent a good amount of time trying to figure it out. My professor seems to think giving us these tough problems in "cybersecurity" is a good idea, but it's leaving me a bit lost. What's the point of this type of scripting anyway?

2 Answers

Answered By SkepticalStudent On

I have to disagree that this is unsolvable! It’s actually a good exercise. Just break your problem down step by step. Can you show us what your loop looks like, along with any relevant code? That may help us understand where you're stuck.

Answered By TechWiz2021 On

You could try using the `find` command along with `grep` and pipe it to a tool like GNU Parallel or even `ripgrep`. This approach should speed things up and make the process a bit more efficient. Plus, using `find` allows you to specifically target directory depths or file types, which can help narrow your search and make it safer!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.