1

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 ?

  • 1
    You can divert stat output to /dev/null. stat file >/dev/null – coffeMug Jun 8 '16 at 14:59
  • 1
    See here also for bash way of checking file existence. tldp.org/LDP/abs/html/fto.html – coffeMug Jun 8 '16 at 15:00
  • Feel free to add answers. Both seems pretty good for me, I will be happy upvote. I may have a preference for the /dev/null since it should work with other shells – Pierre de LESPINAY Jun 8 '16 at 15:03
2

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).

  • 2
    No need for bash (note that [[...]] comes from ksh), you can use the standard [ aka test utility in all shells. As in [ -e ./last_dump.sql ] && .... Note that both [ -e and the ksh [[ -e...]] do a stat() system call while GNU stat does a lstat(). Use stat -L as an equivalent to [ -e and [ -e file ] || [ -L file ] as an equivalent to stat without -L. – Stéphane Chazelas Jun 8 '16 at 16:21
  • @StéphaneChazelas Thanks for mentioning this. Did not know [[ ]] comes from ksh – coffeMug Jun 8 '16 at 16:26
1

Calling stat is only useful to get information that you can't get from basic shell commands, such as a file's modification time or permissions. To just test the existence of a file, use the test command, also spelled [. This command is built into most shells.

if [ -e last_dump.sql ]; then mysql -D my_database < last_dump.sql; fi

The test -e tests whether the specified file exists; the file can be of any type, including broken symbolic links. If you only want regular files (including symlinks to regular files), use -f instead.

Using [ … ] is preferable to stat for many reasons:

  • Calling stat just to discard its output is an obfuscated way of testing whether a file exists. Think of the people who will read the code (such as you in six months): [ -e file ] plainly says “does file exist?”, whereas calling stat says “give me the file's metadata” and testing for existence is merely a side effect.
  • [ … ] is a standard command, whereas stat doesn't exist on all Unix variants and has incompatible command line options where it exists. In this specific case it isn't so bad because.you aren't using any command line options.
  • stat is slower since it's an external command.

You should also refrain from using && when you mean “if”. This too is markedly less readable. It's also less robust since under set -e (telling the shell to exit if any program breaks), [ -e last_dump.sql ] && mysql … aborts the script if last_dump.sql doesn't exist, whereas if [ -e last_dump.sql ]; then …; fi keeps going.

  • I see. Great answer. I learnt several things here. – Pierre de LESPINAY Jun 9 '16 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.