copy files containing words from a txt file [closed]

0

I need to copy files that contain words contained in a txt file. For example, the file I have contains:

Balaena_mysticetus
Balaenoptera_acutorostrata
Bos_taurus
Canis_familiaris
Delphinapterus_leucas
Equus_caballus
Eschrichtius_robustus
Homo_sapiens
Lipotes_vexillifer
Loxodonta_africana
Mus_musculus
Myotis_lucifugus
Orcinus_orca
Physeter_catodon
Sus_scrofa
Tursiops_truncatus
tsg_637

I want to copy files that contain all the words registered in the txt.

I tried using:

grep -L "Balaena_mysticetus" 
  • but instead of the word receive a file as input

Any help is welcome.

    
asked by user2820116 14.05.2018 в 18:03
source

1 answer

1

Good day! with this code in Bash you can do what you need:

#!/bin/bash
for line in $(cat PATH_DE_ARCHIVO_CON_PALABRAS); do
    for file in $(grep -ril ''$line PATH_ARCHIVOS_DONDE_BUSCAR_PALABRAS); do
       cp $file PATH_DONDE_SE_COPIARAN_ARCHIVOS
    done
done

In summary, in the first line all the words in your file are searched where the words to be searched will be contained.

On the second line, the word read from the file in the directory where the files that possibly contain the word in question will be searched with a GREP. (the -ril is for you to search sub-directories, just bring the file path where you find the word)

In the third line what is done is to copy to a certain directory the file that in the previous line was found to contain that word.

* I see that the second FOR could be removed, optimizing the script more, even so, it is understandable and it helped me.

Greetings!

    
answered by 14.05.2018 в 20:46