LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-03-2018, 02:27 AM   #1
whjeon
Member
 
Registered: Feb 2018
Location: Seoul,South Korea
Distribution: Debian
Posts: 88

Rep: Reputation: Disabled
append text in new or old text file methods


Dear Mentors
I'm quite Newbie here!
And Thanks for your help every day!

Today, I have a question about creating a new file or append some text to an old file.

I know there are two methods.

First, 'echo'
Code:
$echo 'something to write' > file.txt
Second, 'cat > file.txt << "EOF"
Code:
$cat > file.txt << "EOF"
something to write
EOF
Which one do you prefer? or What advantage and disadvantage each method have?

Thanks for your help again!
 
Old 05-03-2018, 02:43 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,330
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Why do you ask?

I'd add that actually there are at least four methods.

Third,

Code:
$ echo 'something to write' | tee file.txt
Fourth,

Code:
$ cat | tee file.txt  << "EOF"
something to write
EOF
The even-numbered ones as listed above are using Here Documents. What have you learned of them so far?
 
Old 05-03-2018, 02:48 AM   #3
whjeon
Member
 
Registered: Feb 2018
Location: Seoul,South Korea
Distribution: Debian
Posts: 88

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Why do you ask?
Oh! I'm just wondering if there are some recommended situations to use specific method.
If not, I can use anything.
 
Old 05-03-2018, 03:07 AM   #4
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,504

Rep: Reputation: Disabled
Be careful with the redirectors, a single > creates or overwrites what was there before, whilst a double >> adds to it.

Edit: I usually need to add something to a file, like a config file, so my most used is the >>.

Last edited by fatmac; 05-03-2018 at 03:09 AM.
 
1 members found this post helpful.
Old 05-03-2018, 03:17 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,330
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Pretty much you can use anything. echo is good for short texts. tee is good if you have to write as another user because sudo can be locked down to allow writing to just that one file.

Code:
$ echo "something to write" | sudo -u anotheruser tee /path/to/some/file.txt
See "man sudo", "man sudoers", and "man tee" You can set tee to append if you like.

The Here Documents are good for when you have a lot of text on multiple lines. Pay attention to quotes. Notice that you used double quotes in your example. They are different from single quotes.

Code:
$ echo 'something from $USER' > file.txt
$ echo "something from $USER" > file.txt

$ cat > file.txt << 'EOF'
something to write from $USER
EOF
$ cat > file.txt << EOF
something to write from $USER
EOF
 
1 members found this post helpful.
Old 05-03-2018, 03:29 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
I long ago learnt there is rarely a "best" method in Linux.
So many smart people contributing, new options (maybe better) keep appearing.
 
1 members found this post helpful.
Old 05-03-2018, 06:01 AM   #7
xamaco
Member
 
Registered: Sep 2009
Location: Bastelicaccia, Corsica
Distribution: Crux
Posts: 48

Rep: Reputation: 7
Code:
cat > file.txt
is enough, just end input with a Ctrl-D
 
  


Reply



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] Pulling text from a text file and append to CSV fishy Linux - Newbie 5 03-02-2012 07:32 AM
How to append text to the second line of a file SentralOrigin Programming 23 11-06-2010 07:19 PM
append text to file cyberrate Linux - Software 3 12-02-2009 07:25 AM
want to prepend to a text file, not append urentity Programming 7 08-03-2009 12:45 AM
quickly append text to file blackzone Linux - General 3 09-07-2006 05:48 AM

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

All times are GMT -5. The time now is 08:24 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