1

I have the following code.

PF=opt/N*.properties

Inside PF I have 2 values /OPT/NA.properties and /OPT/NA2.properties.

How can I copy these 2 files to other location in shell script

cp $PF /opt/backup/
5

Based solely on your example, a simple for loop will suffice.

for myfile in ${PF}
do
  cp "${myfile}" /opt/backup/
done
  • @Manish your array variable should be PF=(opt/N*.properties). From here you'd have to for f in "${PF[@]}"; do cp -- "$f" /opt/backup; done otherwise you'd be copying the first file only – Valentin Bajrami Oct 26 '16 at 19:03
4
PF=(opt/N*.properties)
cp "${PF[@]}" /opt/backup

Skip the for loop and copy all files in a single command, this way the glob creates an array and the shell can expand the elements in the array in the way shown above.

If you still want to use the for loop you can skip the variable declaration and just do for myfile in opt/N*.properties; do...

  • Should the FP in line 1, and the PF in line 2 refer to the same variable name (ie. both=FP) ? – Kevin Fegan Oct 27 '16 at 0:31
  • @KevinFegan: you're right, silly typo, fixed. – Dalvenjia Oct 27 '16 at 2:36

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.