Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a file exists in a remote?

Tags:

Is there a way to check if a file under specified, relative path exist in a remote? I'm fine with fetching the info first if it's the only option. In other words I'm looking for git-ls-files with an option to specify remote and branch. I'm only interested if the file exists (a list of files on the branch will do as well), I don't care about hashes, diffs etc.

like image 532
zaza Avatar asked Nov 09 '10 14:11

zaza


People also ask

How do you check if a file is present in a remote server?

You first need to enable key-based SSH authentication to the remote host, so that your script can access a remote host in non-interactive batch mode. You also need to make sure that SSH login has read permission on the file to check.

How do I check if a remote file exists in Python?

Here are a few options: test -e to see if any file exists (directory or regular file), test -f to see if it exists and is a regular file, or test -d to see if it exists and is a directory.

How do I check if a file exists in Linux?

When checking if a file exists, the most commonly used FILE operators are -e and -f . The first one will check whether a file exists regardless of the type, while the second one will return true only if the FILE is a regular file (not a directory or a device).


1 Answers

You can use

git cat-file -e <remote>:<filename> 

which will exit with zero when the file exists. Instead of <remote> above you'd use a remote branch name (but it could in fact be any tree-ish object reference). To use such a remote branch, you'll need to have the remote repository configured and fetched (i.e. by using git remote add + git fetch).

A concrete example:

$ git cat-file -e origin/master:README && echo README exists README exists  $ git cat-file -e origin/master:FAILME fatal: Not a valid object name origin/master:FAILME 

Two things to note:

  • Use / as path delimiter in filenames, even on e.g. Windows.
  • <filename> is a full path (such as foo/bar/README), relative to the root of the repository.
like image 97
earl Avatar answered Oct 02 '22 22:10

earl