UNIX/AWK Tricks: How to copy a long list of files into a directory

  1. Make your list of files, with complete paths, call it “imgList.txt”. Put every file on a new line.
  2. Write a small awk script with the following 1 line:
    { printf("cp %s images/\n", $0);}
  3. Save the script as “copyList.awk”
  4. Do the following command:
    awk -f copyList.awk imgList.txt | sh

Brief explanation of the awk script:

  • %s means you’re putting a string in that spot of your output,
  • $0 is the string you’re putting there
  • $0 is a match for each line in the file “imgList.txt”
  • images/ is the directory to copy your files to (in this example relative to where the command will be run from – can be absolute, of course)
  • \n stands for new line

brief explanation of the command:

  • awk executes the awk code found in “copyList.awk” on the lines found in “imgList.txt” and pipes the output to the shell ( | sh).
wrote byColin
category inDevOps
tagged

2 Comments

  1. Or you could just do everything with awk and use system().
    { system(“cp \”” $0 “\” images”) }
    The \” are so that the $0 argument is getting surrounded by “” ‘s so that if there are any spaces in the path, then the copy will still work.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.