So I have a filename bobv1.txt, and I don't want to manually check to see if bobv2.txt is on the site. On the site bobv1.txt will be replaced by bobv2.txt. I have downloaded the the html page and determined the full download path of bobvX.txt and I know were the file is in my file system. How can I tell if the file is already on my file system? I need this to work for all subsequent versions.
Asked
Active
Viewed 5,571 times
-2
2 Answers
2
There's plenty of ways to perform a check on whether or not a file exists.
- use
test
command ( aka[
) to do[ -f /path/to/file.txt ]
use redirection to attempt to open file ( note that this isn't effective if you lack permissions to read the said file)
$ bash -c 'if < /bin/echo ;then echo "exists" ; else echo "does not exist" ; fi' exists $ bash -c 'if < /bin/noexist ;then echo "exists" ; else echo "does not exist" ; fi' $ bash: /bin/noexist: No such file or directory does not exist
or with silencing the error message:
$ 2>/dev/null < /etc/noexist || echo "nope" nope
use external program such as
stat
$ if ! stat /etc/noexist 2> /dev/null; then echo "doesn't exist"; fi doesn't exist
or
find
command:$ find /etc/passwd /etc/passwd $ find /etc/noexist find: ‘/etc/noexist’: No such file or directory

Sergiy Kolodyazhnyy
- 105,154
- 20
- 279
- 497
-
1Redirection is not an effective test, since it can fail for lack of read permissions (
[ -r file ]
vs[ -e file ]
). – muru Oct 31 '17 at 02:21 -
[ -f <filename> ] && echo "1" || echo "0"
– Brethlosze Jan 15 '19 at 22:22