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 05-23-2017, 11:43 PM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 770

Rep: Reputation: Disabled
cat with a tee?


I often use the following, however, it doesn't work very well when root access is needed and I don't want to su as root.
Code:
cat > /path/to/my/file.conf <<'EoT'
bla
bla bal
bla
EoT
Could you explain what this is doing?
Code:
cat <<EoT | sudo tee -a /path/to/my/file.conf
bla
bla bal
bla
EoT
 
Old 05-24-2017, 12:02 AM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010
tee writes to 2 different outputs, usually STDOUT and a file. The "-a" means to append to the given file, so if it already has contents, tee will write the new output below the existing contents.
 
Old 05-24-2017, 12:51 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,675
Blog Entries: 3

Rep: Reputation: 2907Reputation: 2907Reputation: 2907Reputation: 2907Reputation: 2907Reputation: 2907Reputation: 2907Reputation: 2907Reputation: 2907Reputation: 2907Reputation: 2907
In the first example of a here document, the redirection is happening as the current account. If you want it to also happen under the effects of sudo, presumably as root, then you'd would have needed to wrap it in a shell:

Code:
sh -c "cat > /path/to/my/file.conf <<'EoT'
bla
bla bal
bla
EoT
"
But that puts the here document under root as well. If you use the sudo method, only tee runs under root and thus the file it writes can be owned by root.
 
Old 05-24-2017, 12:46 PM   #4
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,575

Rep: Reputation: 249Reputation: 249Reputation: 249
http://98.131.64.253/images/cat-t-shirt.jpg
 
Old 05-26-2017, 12:02 PM   #5
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
Using tee is like a redirect ">" to a file, but still echoes the output to the terminal to view it. I've used it to record a .WAV file and output a .mp3 at the same time. The -a appends so it's like a ">>" redirect. You don't "have to" use tee. Baring my use of outputting two media types in one pass at the same time. If the output to STDOUT is ignored, you can just redirect. It's kind of like having tail -f on a file and outputting to that file in two terminals, without needing two terminals.

The sudo in the example is to ensure permissions to write to that file. It's not the ONLY way to achieve permissions. But it's one of the few that doesn't require a lot of setup and customization.

$ arecord -D default -d 10 -c 2 -r 48000 -f S16_LE -t wav - | tee test.wav | lame -b 320 -q 0 - test.mp3
 
  


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] Where is the source for commands like 'cat' and 'tee' stored? WhiteHotLoveTiger Slackware 10 12-13-2012 01:22 PM
difference :: cat file_1 & cat < f_1 tushar_pandey Linux - Newbie 4 08-16-2012 09:19 AM
Cat 6 Cat 5e cable question babbab Linux - Networking 1 04-07-2010 02:33 PM
cat in asm/ cat --show-all option Tux Linux - Software 1 09-02-2006 09:31 PM
tee vs > or >> DotHQ Linux - General 3 08-23-2006 01:31 PM

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

All times are GMT -5. The time now is 08:48 AM.

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