Difference between > and >> while creating log fil

2019-07-14 18:16发布

Whats the difference between these

  Python abcd.py > abcd.logs

and

Python abcd.py >> abcd.logs

In either case, the output of the program is stored in the file whose name is provided after the redirection operator.

标签: shell
2条回答
萌系小妹纸
2楼-- · 2019-07-14 18:40

It might depend on the shell you are using but the common behaviour is that > will overwrite the target file, while >> will append to it. If the target file does not exist it will be created in both cases.

查看更多
The star\"
3楼-- · 2019-07-14 18:54

If you use >:

  • if the file exists: it will overwrite the file
  • if the file doesn't exist: it will create the file

if you use >>:

  • if the file exists: it will append to the file
  • if the file doesn't exist: it will create the file

However you often can change this default behavior. And there's also >| which you can encounter in script files.

Clobber - Option

The most common alteration to these rules are usually known as the "clobber"-option.

In bash you can achieve this with:

set -o noclobber # This will set the noclobber option
set +o noclobber # This will unset the noclobber option

In Zsh you can achieve this with:

setopt CLOBBER # This will set the CLOBBER option
unsetopt CLOBBER # This will unset the CLOBBER option

If the "clobber"-option is set (or the "noclobber"-option unset), it will work like the following:

If you use > with clobber-option:

  • if the file exists: error
  • if the file doesn't exist: it will create the file

if you use >> with clobber-option:

  • if the file exists: it will append to the file
  • if the file doesn't exist: error

if you use >| with clobber-option:

  • if the file exists: it will overwrite the file
  • if the file doesn't exist: it will create the file
查看更多
登录 后发表回答