LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   what does it mean when bash redirection truncates? (https://www.linuxquestions.org/questions/linux-newbie-8/what-does-it-mean-when-bash-redirection-truncates-4175578651/)

jzoudavy 04-29-2016 03:47 PM

what does it mean when bash redirection truncates?
 
Hi all

I have the following example:
Code:

$ cat file | tr -s ' ' > file
And if I check my file later, it had some text before but now is blank.

but this example
Code:

$ cat file | tr -s ' ' >> file
If I check my file later, it contains my output exactly as it should from
Code:

tr
.

I looked it up and it says this:

Quote:

Redirecting output
N > TARGET
The TARGET is truncated before writing starts.

Appending redirected output
N >> TARGET
The TARGET is not truncated before writing starts.
I am assuming truncated is the reason example 1 gives me a blank file while example 2 is not truncated and the reason why my file still contains useful information.

But what is truncated and what does it mean? Why would you want to truncate the file?

Thanks

Emerson 04-29-2016 04:30 PM

You may want to replace the contents of a file.

michaelk 04-29-2016 04:49 PM

This might be easier to understand.

For command > file, if file does not exist it will be created otherwise it will be overwritten. Overwritten means all existing file contents will be deleted.

For command >> file, if the file does not exist it will be created otherwise data will be appended. Appended mean new data is written at the end of the file and existing data remains.

Habitual 04-29-2016 05:00 PM

http://www.tldp.org/LDP/abs/html/io-redirection.html


All times are GMT -5. The time now is 06:42 AM.