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

Viewed 29

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

1 Answers

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/
elif [[ ${#jar_files[@]} -gt 0 && ${#war_files[@]} -gt 0 ]]; then
    echo "Both jar and war files present; not copying anything."
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.

Related