0

When I run base64 filename >> encodedfile the new content is appended to existing content. Is there any way I can replace entire old content with new content. Using any similar one line technique? I'd like to learn more such techniques, but I don't know what to search for?

EDIT: What if the new file is same file. Ex: base64 filename > filename. I tried this, the instead of the new content, I see the file gets blank...

aceph
  • 113

1 Answers1

2

The >> operator will indeed append to a file. It's a derivative of the > operator, which does what you want - truncates the file to zero length, then redirects the output to that file, replacing what was in the file with the output of the command you're running.

So:

base64 filename > encodedfile

will do what you're interested in doing.

John
  • 17,011
  • Thanks, it works, but I've edited my question for one more case. – aceph Mar 29 '16 at 19:05
  • 2
    You cannot do the redirection into the same file, it will be always empty because redirection happens first and then the other action. So by the time other action take place redirection creates the same file with no data. You can use a temp file in between and achieve the required output. – Pacifist Mar 29 '16 at 19:14