LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2016, 03:47 PM   #1
jzoudavy
Member
 
Registered: Apr 2012
Distribution: Ubuntu, SUSE, Redhat
Posts: 188

Rep: Reputation: Disabled
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
 
Old 04-29-2016, 04:30 PM   #2
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
You may want to replace the contents of a file.
 
Old 04-29-2016, 04:49 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,678

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
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.
 
Old 04-29-2016, 05:00 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash stdout redirection danielbmartin Programming 6 01-15-2016 02:09 PM
bash redirection help clifford227 Programming 6 07-30-2012 02:36 PM
Bash I/O redirection problem R00ts Programming 1 10-26-2006 07:50 PM
bash: socket redirection? Thinking Linux - Software 1 04-15-2006 12:20 PM
Bash redirection and subshells nx5000 Programming 2 11-03-2005 06:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration