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

x
?
Solved

Linux: execute command on every file in a list

Posted on 2009-12-21
5
Medium Priority
?
629 Views
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?
0
Comment
Question by:poisa
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:jb1dev
ID: 26100258
Something like this:

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

Open in new window

0
 
LVL 68

Expert Comment

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

Expert Comment

by:jb1dev
ID: 26100293
Or:

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

Or:

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

Expert Comment

by:woolmilkporc
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

etc.

0
 
LVL 68

Accepted Solution

by:
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
0

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