19

I try to write to a file date > file but Linux says

'cannot overwrite existing file'.

I tried chmod 755 but still cannot write to the file. What should I do?

5 Answers 5

32

If you are running bash, the noclobber option has been set. Use >| if you want to temporarily override this.

1
  • How to redirect both stderr and stdout and temporarily override noclobber at the same time? I tried &>| without luck.
    – Tu Bui
    Commented Aug 24, 2018 at 9:35
7

Turn off noclobber in current shell session:

set +o noclobber

Turn on noclobber in current shelll session:

set -o noclobber

See all bash settings:

set -o

References:
https://www.victordodon.com/to-clobber-or-to-noclobber/ https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html

2

It is possible that the file was set as immutable with chattr. It's rarely used, but it's a realistic possibility. In that case:

chattr -i [filename]

... should be able to remove that attribute.

1
  • 1
    Writing to an "immutable" file would result in "Operation not permitted.". Commented Mar 8, 2011 at 12:44
2

If you 're running oh-my-bash you can change this behavior in:

/home/user/.oh-my-bash/lib/shopt.sh

You comment out the following:

#set -o noclobber
-1
  1. Go to your home folder
  2. Press Ctrl + H to show hidden files
  3. Check whether any of the folders are locked
  4. Right click on the folder then click "properties". In that click "permissions".
  5. Under owner, change folder access to "create and delete files."
  6. Click "Apply Permissions to Enclosed files"

If you cannot see any locked folders then select all folders and do the above

1
  • 2
    Somehow, I don't expect this to work in every case ;-) Hint: some people don't use the same DE as you do, or even any GUI at all.
    – Psirus
    Commented Jan 31, 2012 at 14:39

You must log in to answer this question.

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