Copy files from a list contained in another file

0

I have a file in Linux that contains a list of names of 500 files, I want to detect if they exist on my hard drive, and then copy them to a destination folder called, for example, Encontrados .

The file that contains the list of files to be copied has a structure like the following (each route is a line).

  

/home/user/example/archive_1.txt
  /home/usuario/proyectos/archivo_2.png

The only way I thought of solving it (in my case I did it in Bash ) is:

cat archivosAencontrar.txt|find / -exec cp /ENCONTRADOS
    
asked by mike 13.05.2017 в 17:47
source

1 answer

0

In principle find does not accept data by the standard input, so the data sent by cat to find have no effect on its operation.

Try this expression:

xargs -I '{}' find . -name '{}' < archivos.txt | xargs -I '{}' cp '{}' /tmp/

Notice that it is not necessary to use cat , just an input redirect. We use {} as container of each line of archivos.txt and for each result found by find we repeat the process to make the copy.

Use the -I modifier to set the substitution pattern:

  

-I replace-str

     

Replace occurrences of replace-str in the initial-arguments with names read from standard input.

     

Also,   unquoted blanks do   not terminate input items; instead the separator is the newline character.

     

Implies -x and -L 1.

In Spanish:

  

-I chain-replacement

     

Replaces the occurrences of cadena-reemplazo in the arguments with the names obtained from the standard input.

     

Also, blank spaces without escaping do not end the input elements; instead the separator is the new line character.

     

Imply -x and -L 1.

A reduced version in which I use = as a container for each data line would be:

xargs -I '=' find . -name '=' -exec cp {} /tmp/ \; < archivos.txt
    
answered by 13.05.2017 / 19:27
source