One liner to check for file exists

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Objective: Check for presence of backup .tgz file containing today's date; output 1 for OK, 0 for no file.
I'm a sucker for one liners :)
For example in PHP (and pretty much similar in Javascript), in various scenarios I like to do something like
<?php
echo (date("d")==1)?"Monday":"Not Monday";
?>
Is there similar syntax in Bash? I know how to check for presence of a regular file using -f FILENAME, I only want 1 or 0 returned from my oneliner :)
bash files
add a comment |Â
up vote
2
down vote
favorite
Objective: Check for presence of backup .tgz file containing today's date; output 1 for OK, 0 for no file.
I'm a sucker for one liners :)
For example in PHP (and pretty much similar in Javascript), in various scenarios I like to do something like
<?php
echo (date("d")==1)?"Monday":"Not Monday";
?>
Is there similar syntax in Bash? I know how to check for presence of a regular file using -f FILENAME, I only want 1 or 0 returned from my oneliner :)
bash files
1
Possible duplicate of Shell script check if file exists?
â mrc02_kr
24 mins ago
2
Why isn't-fenough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?
â terdonâ¦
20 mins ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Objective: Check for presence of backup .tgz file containing today's date; output 1 for OK, 0 for no file.
I'm a sucker for one liners :)
For example in PHP (and pretty much similar in Javascript), in various scenarios I like to do something like
<?php
echo (date("d")==1)?"Monday":"Not Monday";
?>
Is there similar syntax in Bash? I know how to check for presence of a regular file using -f FILENAME, I only want 1 or 0 returned from my oneliner :)
bash files
Objective: Check for presence of backup .tgz file containing today's date; output 1 for OK, 0 for no file.
I'm a sucker for one liners :)
For example in PHP (and pretty much similar in Javascript), in various scenarios I like to do something like
<?php
echo (date("d")==1)?"Monday":"Not Monday";
?>
Is there similar syntax in Bash? I know how to check for presence of a regular file using -f FILENAME, I only want 1 or 0 returned from my oneliner :)
bash files
bash files
asked 36 mins ago
DavDav
375
375
1
Possible duplicate of Shell script check if file exists?
â mrc02_kr
24 mins ago
2
Why isn't-fenough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?
â terdonâ¦
20 mins ago
add a comment |Â
1
Possible duplicate of Shell script check if file exists?
â mrc02_kr
24 mins ago
2
Why isn't-fenough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?
â terdonâ¦
20 mins ago
1
1
Possible duplicate of Shell script check if file exists?
â mrc02_kr
24 mins ago
Possible duplicate of Shell script check if file exists?
â mrc02_kr
24 mins ago
2
2
Why isn't
-f enough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?â terdonâ¦
20 mins ago
Why isn't
-f enough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?â terdonâ¦
20 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
You can simply do this :
#to check if it's a regular file
[ -f "/you/file.file" ] && echo 1 || echo 0
#to check if a file exist
[ -e "/you/file.file" ] && echo 1 || echo 0
In shell this mean [ test -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.
This should work in shell and bash
add a comment |Â
up vote
5
down vote
if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi
2
@mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
â terdonâ¦
22 mins ago
add a comment |Â
up vote
2
down vote
You can use this command:
test -e *$(date).tgz && echo 1 || echo 0
3
Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use thetest -epart and forget the rest. Thetestcommand already returns 0 or 1 depending on its status.
â terdonâ¦
21 mins ago
add a comment |Â
up vote
0
down vote
With zsh:
()echo $# *"$(date +%Y-%m-%d)"*.tgz(DN)
Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number.
To use as the condition in an if statement, you can do:
if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
echo found
else
echo none found
fi
In bash, the equivalent could be:
(shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")
and
if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
echo found
else
echo none found
fi
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
You can simply do this :
#to check if it's a regular file
[ -f "/you/file.file" ] && echo 1 || echo 0
#to check if a file exist
[ -e "/you/file.file" ] && echo 1 || echo 0
In shell this mean [ test -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.
This should work in shell and bash
add a comment |Â
up vote
6
down vote
You can simply do this :
#to check if it's a regular file
[ -f "/you/file.file" ] && echo 1 || echo 0
#to check if a file exist
[ -e "/you/file.file" ] && echo 1 || echo 0
In shell this mean [ test -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.
This should work in shell and bash
add a comment |Â
up vote
6
down vote
up vote
6
down vote
You can simply do this :
#to check if it's a regular file
[ -f "/you/file.file" ] && echo 1 || echo 0
#to check if a file exist
[ -e "/you/file.file" ] && echo 1 || echo 0
In shell this mean [ test -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.
This should work in shell and bash
You can simply do this :
#to check if it's a regular file
[ -f "/you/file.file" ] && echo 1 || echo 0
#to check if a file exist
[ -e "/you/file.file" ] && echo 1 || echo 0
In shell this mean [ test -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.
This should work in shell and bash
edited 18 mins ago
terdonâ¦
124k29234408
124k29234408
answered 30 mins ago
Kiwy
5,66153454
5,66153454
add a comment |Â
add a comment |Â
up vote
5
down vote
if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi
2
@mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
â terdonâ¦
22 mins ago
add a comment |Â
up vote
5
down vote
if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi
2
@mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
â terdonâ¦
22 mins ago
add a comment |Â
up vote
5
down vote
up vote
5
down vote
if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi
if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi
edited 18 mins ago
terdonâ¦
124k29234408
124k29234408
answered 28 mins ago
Goro
7,83453472
7,83453472
2
@mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
â terdonâ¦
22 mins ago
add a comment |Â
2
@mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
â terdonâ¦
22 mins ago
2
2
@mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
â terdonâ¦
22 mins ago
@mrc02_kr the definition of a "one liner" is not very precise. The point is a simple, short command. That doesn't mean we need to put it all on one line, not when it is obviously a single command.
â terdonâ¦
22 mins ago
add a comment |Â
up vote
2
down vote
You can use this command:
test -e *$(date).tgz && echo 1 || echo 0
3
Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use thetest -epart and forget the rest. Thetestcommand already returns 0 or 1 depending on its status.
â terdonâ¦
21 mins ago
add a comment |Â
up vote
2
down vote
You can use this command:
test -e *$(date).tgz && echo 1 || echo 0
3
Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use thetest -epart and forget the rest. Thetestcommand already returns 0 or 1 depending on its status.
â terdonâ¦
21 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can use this command:
test -e *$(date).tgz && echo 1 || echo 0
You can use this command:
test -e *$(date).tgz && echo 1 || echo 0
answered 30 mins ago
mrc02_kr
857319
857319
3
Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use thetest -epart and forget the rest. Thetestcommand already returns 0 or 1 depending on its status.
â terdonâ¦
21 mins ago
add a comment |Â
3
Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use thetest -epart and forget the rest. Thetestcommand already returns 0 or 1 depending on its status.
â terdonâ¦
21 mins ago
3
3
Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use the
test -e part and forget the rest. The test command already returns 0 or 1 depending on its status.â terdonâ¦
21 mins ago
Note that this doesn't actually return 1 or 0. It prints 1 or 0 whihc is a different thing. If you want to return 1 or 0, just use the
test -e part and forget the rest. The test command already returns 0 or 1 depending on its status.â terdonâ¦
21 mins ago
add a comment |Â
up vote
0
down vote
With zsh:
()echo $# *"$(date +%Y-%m-%d)"*.tgz(DN)
Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number.
To use as the condition in an if statement, you can do:
if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
echo found
else
echo none found
fi
In bash, the equivalent could be:
(shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")
and
if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
echo found
else
echo none found
fi
add a comment |Â
up vote
0
down vote
With zsh:
()echo $# *"$(date +%Y-%m-%d)"*.tgz(DN)
Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number.
To use as the condition in an if statement, you can do:
if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
echo found
else
echo none found
fi
In bash, the equivalent could be:
(shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")
and
if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
echo found
else
echo none found
fi
add a comment |Â
up vote
0
down vote
up vote
0
down vote
With zsh:
()echo $# *"$(date +%Y-%m-%d)"*.tgz(DN)
Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number.
To use as the condition in an if statement, you can do:
if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
echo found
else
echo none found
fi
In bash, the equivalent could be:
(shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")
and
if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
echo found
else
echo none found
fi
With zsh:
()echo $# *"$(date +%Y-%m-%d)"*.tgz(DN)
Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number.
To use as the condition in an if statement, you can do:
if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
echo found
else
echo none found
fi
In bash, the equivalent could be:
(shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")
and
if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
echo found
else
echo none found
fi
answered 9 mins ago
Stéphane Chazelas
288k53532868
288k53532868
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474244%2fone-liner-to-check-for-file-exists%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password

1
Possible duplicate of Shell script check if file exists?
â mrc02_kr
24 mins ago
2
Why isn't
-fenough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?â terdonâ¦
20 mins ago