0

I have a file rock.mpg in the folder directory /folder1/folder2. I need to create multiple (340) duplicate copies of this file in a different directory say /folder1/folder2/folder3 but with different unique filenames.

The unique names for the 340 duplicate copies are random and also not sequential. They are already defined in a separate text file file abc.txt which I have.

A sample of duplicate file names in abc.txt as follows

MS123.mpg
DW235.mpg
WQ6748.mpg
HDJ74.mpg
...
...

so on

Can this be achieved with shell script?

  • 1
    Yes. Worth revising the question to show some existing file list, how you'd expect new list to look, and some of the abc.txt file content. – steve Aug 9 '15 at 10:47
  • You really want copies? Or can your problem be solved with links? – Bernhard Aug 9 '15 at 10:58
  • Thanks @Bernhard . Yes, I really want to make copies. Basically something which copies the file 'rock.mpg' to new location and then renames it one by one as per the list of filenames defined in abc.txt – Parsha Aug 9 '15 at 11:02
  • Do you need each copy in a different directory? i.e. ./folder3/MS123.mpg, ./folder4/DW235.mpg, etc? Or ALL 340 copies in folder3? – Scottie H Sep 17 '19 at 0:55
1

Should be rather easy with xargs:

xargs -a abc.txt -i cp /folder1/folder2/rock.mpg /folder1/folder2/folder3/{}
  • -a abc.txt tells xargs to read from abc.txt (instead of from standard input as usual)
  • -i tells xargs to replace {} with a line of input

Test it with:

xargs -a abc.txt -i echo cp /folder1/folder2/rock.mpg /folder1/folder2/folder3/{}
0

with BASH only:

while read newname ; do
    cp old/dir/rock.mpg /new/dir/"$newname"
done < abc.txt

or even in a single line:

while read newname ; do ; cp old/dir/rock.mpg /new/dir/"$newname" ; done < abc.txt

The mechanism is to read each line of abc.txt as the varaible newdir and then copy the old file to the new location with this new name.

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.