Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any way to pass AND and OR conditions in linux cp command

I want to copy jar and war files present inside a directory to destination
The condition here is if there are no jar present inside dir it should copy only war’s and vice versa
Any suggestions on how this can be done using cp command?
Thank you

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Something like this would work in bash:

#!/usr/bin/env bash

shopt -s nullglob

jar_files=( directory/*.jar )
war_files=( directory/*.war )

if [[ ${#jar_files[@]} -eq 0 && ${#war_files[@]} -gt 0 ]]; then
    cp "${war_files[@]}" destination/
elif [[ ${#jar_files[@]} -gt 0 && ${#war_files[@]} -eq 0 ]]; then
    cp "${jar_files[@]}" destination/
else
    echo "No jar or war files found!"
fi

Save the jar and war files in respective arrays, and then see if they have 0 elements or not and copy the other if so.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading