Search code examples
linuxbashshellsh

Using a file with file names with paths in it as target for cp


I have some paths written like that

./20231023_155014.jpg
./20231023_155217.jpg
./20231023_155016.jpg

inside my file called rheuma. Now I want to use this file as my input for the cp command and iterate through it. Is it possible by default as a one-liner ?

Data should be copied from the smartphone via termux ssh and scp.

This command I would usually use… find . -mtime -1 -type f -exec scp {} /; It would be great but it does not work pretty well. So I need the solution from above or similar.

(Stuff like … -exec scp {} /; does not work in my environment)

Original wording of the question: How can I use a file with paths with the cp command

EDIT: It is a file rheuma and in it are file names with paths like ./20231023_155014.jpg, and I am interested in running cp <path_from_file> somewhere.


Solution

  • In bash [not sure this is your environment], this should work for you:

    for file in $(cat rheuma); do COMMAND $file ; done
    

    where command is your function. As some commenters point out, this won't work if you have extraneous whitespace in your input file, see their comments for some somewhat more complex approaches that can handle those cases.

    You may also want to put xargs into your toolkit, it is useful as workalike to "find -exec" but ultimately you may hit a limit on the number of arguments it will process. This technique looks like

    cat rheuma | xargs COMMAND
    

    Not that each line will just get appended to an iteration of COMMAND.

    https://man7.org/linux/man-pages/man1/xargs.1.html

    Again, if you have whitespace in your input file, see some of the comments for recipes for that. I have very different ways of removing whitespace and I consider it a completely separate topic in fact.