1

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.

Example

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

Question

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

UPDATE

I created this script:

#!/bin/bash

input="/data/web/web/gallery/data.csv"

while IFS=',' read from to
do
    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 Sep 22 '15 at 15:18
  • What application and operating system was used to create the csv file? – fd0 Sep 22 '15 at 15:49
0

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
abc.jpg
xyz.jpg

Test

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

Final

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

Result:

`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. – Artegon Sep 23 '15 at 6:33
0

That's simplest with xargs:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.