Check if multiple files exist on a remote server
5
votes
6
answers
286
views
I am writing a script that locates a special type of file on my system and i want to check if those files are also present on a remote machine.
So to test a single file I use:
ssh -T user@host [[ -f /path/to/data/1/2/3/data.type ]] && echo "File exists" || echo "File does not exist";
But since I have to check blocks of 10 to 15 files, that i would like to check in one go, since I do not want to open a new ssh-connection for every file.
My idea was to do something like:
results=$(ssh "user@host" '
for file in "${@}"; do
if [ -e "$file" ]; then
echo "$file: exists"
else
echo "$file: does not exist"
fi
done
' "${files_list[@]}")
Where the file list contains multiple file path. But this does not work, as a result, I would like to have the "echo" string for every file that was in the files_list.
Asked by Nunkuat
(153 rep)
Aug 6, 2025, 12:31 PM
Last activity: Aug 7, 2025, 04:26 AM
Last activity: Aug 7, 2025, 04:26 AM