0

I'm expecting 6 files to be present in current directory:

prefix_alpha.txt
prefix_beta.txt
prefix_gamma_.txt
prefix_delta.txt
prefix_epsilon.txt
prefix_feta.txt

How do I get a list of whatever files are not present and store them into a variable in Bash? Would like just a space between the filenames if more than 1 aren't present. End action is using the file names in a SQL statement along the lines of INSERT INTO table (columnname) VALUES ("File(s) '$notexistingfiles' don't exist")

1

1 Answer 1

3

You can use the test(7) command to check if a file exists so all you have to do is loop over the expected files and add them to an array if they don't exist.

# The target files
FILES=(
    "prefix_alpha.txt"
    "prefix_beta.txt"
    "prefix_gamma_.txt"
    "prefix_delta.txt"
    "prefix_epsilon.txt"
    "prefix_feta.txt"
)

# Create a missing array
declare -a MISSING

for file in "${FILES[@]}"; do
    test -e "$file" || MISSING+=("$file")
done

echo "${MISSING[@]}"

Not the answer you're looking for? Browse other questions tagged or ask your own question.