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")

simplycoding
  • 2,770
  • 9
  • 46
  • 91
  • See the `-f` and `-s` expressions in the [bash man page](https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions). – Alex Howansky Oct 16 '15 at 18:50

1 Answers1

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[@]}"
Steven
  • 5,654
  • 1
  • 16
  • 19