How to check via SSH and credentials if file on remote server exists?

Hi there,

I am sorry to ask that kind of beginner thing, but all the code I found online didnt work for me.
All I want to do is: Check via SSH if a File exists on my webserver. The SSH login has to be with username and password.

So I would be very thankful if somebody could write the line.
Example data:

User: user0815
Password: pwd0815
Host: example.com

Path of file on remote server: /htdocs/www/my/folder/file.jpg

Thank you very much.
Jens

Welcome to the forum.

Any attempts / ideas / thoughts from your side? Where are you stuck?

Welcome Jens885544,

I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • Why must SSH using id/password be used for the connection?
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using? The output of uname -a on both the client and the server would be useful here.
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards,
Robin

Hey guys. You are totally right so far. I forgot to add my try.
The problem was how to add the credentials. Well the problem still exists but now I was able to add the public key to the webserver.
So now it is working without entering user and password.

The so far working code is:

FILETOCHECK=/test.txt
if ssh 'user0815@example.com' stat $FILETOCHECK \> /dev/null 2\>\&1
            then
                    echo "File exists"
            else
                    echo "File does not exist"

fi

Thank you.
I was not able to do with pwd authentication. If you know how to do so, please post it, because then we have a 100% solution on the topic.
Jens

Congratulation. That is an easy, short, comfortable, and, before all, safe solution, working as intended by the ssh creators (at least I hope so).

Why do you want (or have to?) fall back to a complicated and unsafe workaround?

Not for me, but maybe somebody would need it and expects it because its in the headline.

But maybe you can give me a hint: I now build in the working code into my script. The script does a foreach on 7000 files. Is it a recommended way to do
"if ssh 'ssh-user0815@example.com' stat $FILETOCHECK \> /dev/null 2\>\&1"
on every file? I ask because to me it looks like it builds then 7000 times a ssh connection.
Is there a way to build it once and then check the files?

Thank you.
Jens

It's difficult to say, in a way. What do you want to do with the output from the ssh command? Because the output will be in a standard format, you might be able to call stat just once for all of them. The output will be one long trail, but there is a clear marker for the file name, size and other information. You can even direct it to format in a way most useful to you.

If you fancy a fairly terse output, you could try ssh $username@target_server "stat -t $files_to_check" or push it even further with ssh $username@target_server "stat -tc '%n' $files_to_check" which will give you the filenames that exist or a message something like stat: cannot stat `not_a_file': No such file or directory as one big list of output.

What do you eventually want to get out of it?

Are any files likely to be a symbolic link? You might need to add the -L flag to follow links to check them.

Does this help,
Robin

I'd have a local file with 7,000 filenames in it:

/path/to/file1
/path/to/file2
/path/to/whatever
etc

And run ssh once:

ssh username@host 'while read -r FILE ; do [ -e "$FILE" ] && echo "$FILE exists" || echo "$FILE missing" ; done' < fileslist

For that many files, an ssh connection for every single one is pointless unless they all are on different servers. Should they be on one or few servers, and you just need to check their existence, why not use the sftp command, which is part of the ssh suite, to get a remote directory listing and evaluate that locally? Like

sftp user@host <<< "ls -1 $DIRTOCHECK" > filesonserver

If there are more than one remote directories to be searched, try using a "batchfile" containing the ls commands for the target directories, like

sftp -b batchfile user@host  > filesonserver

After that, on the local node, you can compare the results against the list of necessary files.