I am using a MacBook Pro OS X 2013 model. I have a list of filenames in a text file named list.txt separated by line breaks

list.txt exactly like this, but this list is going to be long


I need these commands to copy the same files to the destination folder thousands of times and I am running into the duplicate file name problem. I need it to copy the files over again but I figure if I rename them sequentially as they are copied to the new folder, then I won't run into the duplicate names. I am copying the same approx. 40 files over and over again thousands of times depending on the order that they appear in list.txt. Each file is between 100KB and 250KB.

I want to copy each file in the order that they are listed in list.txt to another folder and rename them sequentially

I used this to copy the files to the new folder and it worked sort of, it didn't keep or rename the duplicates.

cp `cat list.txt` new-folder/

I am using this to rename them sequentially, but it renames them in the wrong folder

find . -name '*.jpeg' \
| awk 'BEGIN{ a=0 }{ printf "mv %s %04d.jpeg\n", $0, a++ }' \
| bash

I have been trying to combine them using this with no luck:

cat [filename] | while read line; do [command] "$line"; done

I've tried a bunch of combinations of this:

cp `cat list.txt` new-folder/ | while read line; do find . -name '*.jpeg' \
| awk 'BEGIN{ a=0 }{ printf "mv %s %04d.jpeg\n", $0, a++ }' \
| bash "$line"; done

I need the files to appear in the newfolder in the order of list.txt, because I am going to use them by the date added/date creation for the project. I might have to put a delay for each line until it is coppied and renamed.

1 Answer 1


Don't try to build a shell script from a list of files, it's hard to get right and almost always a lot harder than directly executing whatever you need to execute. Instead, when you have the file name available, go ahead and rename it.

while IFS= read -r old_name; do
  new_name=$(printf %04d "$number").jpeg
  mv -- "$old_name" "new-folder/$new_name"
done <list.txt
  • I dont understand how to use this or where to put it. I am not trying to build a shell script from a list of files. I need a shell script that will copy the files as exactly as in list.txt above to a new folder and rename them sequentially as they are pasted. in the exact order as list.txt Jan 19, 2015 at 0:06
  • @timothynorris This is a shell script which does the renaming based on list.txt. You put it in a file (with the initial shebang line which makes it a shell script), make the file executable and execute it. Or, if you just want to execute it once, you can copy-paste onto your command line. That's exactly what you would have done with the commands in your question anyway. Jan 19, 2015 at 0:11
  • i think you are saying that it will rename the files as in list.txt, i don't need it to rename the files as in list.txt, i need it to copy the files as in list.txt and then paste them in a folder and then rename them as they appear in list.txt line by line. because there are only about 40 jpeg's that i need copied over and over again thousands of times in the order of list.txt i apologize if i don't understand i don't have much experience in programming Jan 19, 2015 at 0:20
  • @timothynorris I don't understand. Do you mean that you want to copy the file rather than move them? If so, call cp instead of mv. Jan 19, 2015 at 0:23
  • I'm doing a time-lapse, what i need it to do. the order of things to copy are in list.txt separated by line breaks. like this b.jpeg o.jpeg o.jpeg k.jpeg those four files when i put them in the time-lapse program it will spell book. i need it to copy each file in list.txt and rename them sequentially as the are pasted in the new folder Jan 19, 2015 at 0:43

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.