I'm trying to write a backup script, using the method from this answer from superuser to show a progress bar.

I invoke tar like this:

tar cf --exclude-vcs --exclude-ignore=.tarignore - $backup_files -P | pv -s $(du -csb $backup_files |awk '{print $1}'|tail -n 1)  | gzip > "$target_file"

What I'm getting is the error message "tar -: Can not stat: No such file or directory" but the script keeps running. What exactly is going on here? Why is tar not iterpreting - as "use stdout instead of an output file"?

1 Answer 1


The issue was that tar the f flag means the next argument is interpreted as a filename, rather than the first file operand like an older document suggested. I found a file in my working directory named --exclude-vcs.

The correct order of arguments is

tar cf - --exclude-vcs --exclude-ignore=.tarignore $backup_files -P

I'm not sure why the script didn't exit when tar did.

  • 3
    Since you resolved your issue, please don't forget to accept your own answer so it doesn't pop up as "Unanswered" somewhere down the line. Feb 2, 2023 at 15:16
  • I would bet tar didn't exit as - further away in command line mean : read stdin, just hitting CTRL-D should have done the job.
    – Archemar
    Feb 2, 2023 at 15:29
  • @Archemar, it interprets the - as the name of a file to store in the archive, hence the error. As in echo foo > ./-; tar cf foo.tar - stores a file called -. The dash means stdin/stdout only as an argument to f/-f... The script doesn't exit because there's no reason to. Even if tar there fails, the pipeline as a whole finishes fine. You'd need set -o pipefail to have it otherwise (and set -e).
    – ilkkachu
    Feb 2, 2023 at 15:53
  • @haem note that that document knows nothing about dashed options, so for that tar, ` --exclude-vcs` would seem to be the first file operand...
    – ilkkachu
    Feb 2, 2023 at 15:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .