Bash – Silent stat to check file existence

bashstat

I'm running a command if a file is present in one line of bash.

Here is what I'm doing for now:

$ stat /last_dump.sql && mysql -D my_database < last_dump.sql

Is it the right way to do ?

It works fine, but the stat report is outputted

  File: '/last_db.sql'
  Size: 42161       Blocks: 88         IO Block: 4096   regular file
Device: 26h/38d Inode: 108         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    me)   Gid: (    0/    me)
Access: 2016-06-08 08:07:15.886741191 +0000
Modify: 2016-06-08 08:07:06.218593606 +0000
Change: 2016-06-08 08:07:06.218593606 +0000
 Birth: -

How can I not show this stat report ?

Best Answer

You can also use file test operators:

[[ -e ./last_dump.sql ]] && mysql -D my_database < ./last_dump.sql

See here for more info about these operators: File test operators

But if you prefer to use stat you can divert its output to /dev/null:

stat ./last_dump.sql >/dev/null && mysql -D my_database < ./last_dump.sql

Notice ./ in the commands above which means current directory. Also I should note that if you want to make stat completely silent (even in case of errors) divert errors to /dev/null as well using &>/dev/null or >/dev/null 2>&1 (have same result).