Bash – Test for existence of multiple files, given by pipe

bashfilesshell-script

I have a command that gives me a list of files, one on each line. Filenames are "normal" – no spaces, no need to escape parentheses etc.

Now I want to pipe that command to something like test -f and return true if and only if all of the files exist. (Behaviour with 0 lines can be undefined, I don't really care.)

So, something like

make_list_of_files | test -f

but actually working.

"Bashisms" are allowed, since I need it in Bash.

The files are not in the same directory, but they are in subdirectories of a current directory, and the paths have directory names in them, so for example

dir/file1
dir/file2
dir2/file3

Best Answer

allExist(){
    while IFS= read -r f; do
      test -e "$f" || return 1
    done
}

make_list_of_files | allExist

This should work in all POSIX shells.

Related Question