[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Linux: execute command on every file in a list

Posted on 2009-12-21
Medium Priority
Last Modified: 2013-12-16
I have a plain text file that I created with the find command. This file contains a list of javascript files (including full path) that might contain a virus.

I need to search every one of those files for the "document.write" string and generate a list of the files that have it so that I can revise them manually.

How would I go about doing it?
Question by:poisa
  • 3
  • 2
LVL 14

Expert Comment

ID: 26100258
Something like this:

for file in `cat file.txt`; do
    grep "document.print" $file

Open in new window

LVL 68

Expert Comment

ID: 26100278
while read file; do grep -H "document.write" $file  ; done < files.lst
LVL 14

Expert Comment

ID: 26100293

for file in `find .`; do grep document. $file 2>&1 >/dev/null; if [ $? = 0 ]; then echo "Found $file"; fi; done


for file in `cat file.txt`; do grep document. $file 2>&1 >/dev/null; if [ $? = 0 ]; then echo "Found $file"; fi; done
LVL 68

Expert Comment

ID: 26100304
Some variations:

- to put the output into a file

while read file; do grep -H "document.write" $file  ; done < files.lst >> found.lst

- to use a single step (including your find command

your_find_command | xargs -I{} grep -H "document.write" {} >> found.lst


LVL 68

Accepted Solution

woolmilkporc earned 2000 total points
ID: 26100692
More hints -
should you need only the names of the files with matches, without printing the matching lines themselves,
use grep -l instead of grep -H
Should you instead desire filenames, matching lines and additionally the line numbers, use grep -n -H

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question