I need to copy all files listed in TXT file from one location to another location.

/1132526906_tt_nad87_1.jpg  /thumb/t1132526906_tt_nad87_1.jpg
/1132526906_tt_nad87_10.jpg /thumb/t1132526906_tt_nad87_10.jpg
/1132526906_tt_nad87_11.jpg /thumb/t1132526906_tt_nad87_11.jpg
/1132526907_tt_nad87_12.jpg /thumb/t1132526907_tt_nad87_12.jpg
/1132526907_tt_nad87_13.jpg /thumb/t1132526907_tt_nad87_13.jpg
/1132526908_tt_nad87_14.jpg /thumb/t1132526908_tt_nad87_14.jpg

I can create a CSV file and divide source and target with some usable character.


I would like to copy file 1132526906_tt_nad87_1.jpg from ./ to /thumb/t1132526906_tt_nad87_1.jpg - rename and move.


Is there some command line command to do it? I found examples but these examples do only copying (without rename).


I created this script:



while IFS=',' read from to
    echo "from: $from, to: $to"
done < "$input"

but no line is "echoed", it seems that data.csv file is not read. Is there something wrong?

  • Use #!/bin/sh no bash required so far.
    – Marco
    Commented Sep 22, 2015 at 15:18
  • What application and operating system was used to create the csv file?
    – fd0
    Commented Sep 22, 2015 at 15:49

2 Answers 2


From the OP's example, the rename and change seems fairly consistent, eg filename.jpg seems always prefixed with same path plus a "t" becoming thumb/tfilename.jpg . If this is the case then its possible to do something simpler.

If all are destined for "thumb/" directory.

Have test files abc.jpg, xyz.jpg in current directory.

Here is sample file list

$ cat testfile


It is helpful to do test version of the command with an echo to see what the code will do:

$ while read my_file; do
   echo cp -a "$my_file" "thumb/t$my_file"
done < testfile

Test result:

cp -a abc.jpg thumb/tabc.jpg
cp -a xyz.jpg thumb/txyz.jpg
  • while read my_file; do ... done syntax will process stdin , line by line
  • < testfile uses < to redirect testfile to stdin, so the while statement reads it
  • my_file is a variable we made up to hold the input, which as we see in the body of the code can be referenced with $my_file
  • use quotes to avoid potential problems if file names have spaces or other wierd characters that may break the spaces that cp command uses to recognize arguments


If satisifed with the test echo results we can remove the echo to actually carry out the command:

$ while read my_file; do
  cp -a "$my_file" "thumb/t$my_file"
done < testfile

When you check the thumb/ directory you will see the two test files copied there.

Final, with Feedback

By default cp gives no feedback unless there are errors. So if you are copying hundreds or thousands of files, it can potentially seem as if nothing is happening at all. To see feedback use the verbose option -v:

$ while read my_file; do
  cp -av "$my_file" "thumb/t$my_file"
done < testfile


`abc.jpg' -> `thumb/tabc.jpg'
`xyz.jpg' -> `thumb/txyz.jpg'
  • No. I need to copy two files with different names. This is only coincidence what you noticed about file names, so your solution doesn't work for me.
    – jnemecz
    Commented Sep 23, 2015 at 6:33

That's simplest with xargs:

< file-list.txt  xargs -n2 cp -v

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .