Score:2

How to redirect the output of 'cat' into a different folder?

us flag

I have a Python script, EulerianCycle.py, and an input file, euleriancycle.txt.

I am able to get the correct results by doing py EulerianCycle euleriancycle.txt > cat euleriancycleout.txt into the current folder (py is an alias for python3).

However, I have another folder in this current one called outputs, to which I want all my output files be directed.

I've tried py EulerianCycle.py euleriancycle.txt | cd outputs/ | cat > euleriancycleout.txt

And py EulerianCycle.py euleriancycle.txt | cat >cd outputs/euleriancycleout.txt

which gives me the broken pipe error.

Score:7
hr flag

If py EulerianCycle.py euleriancycle.txt writes to the standard output stream (which I assume it does, since otherwise you wouldn't be able to pipe it to cat) then cat is entirely superfluous here - you can redirect standard output directly, specifying either absolute or relative path to your output file:

py EulerianCycle.py euleriancycle.txt > outputs/euleriancycleout.txt

(note: the directory outputs/ must already exist).


Neither of your other commands works the way you might imagine.

  • in py EulerianCycle euleriancycle.txt > cat euleriancycleout.txt, the shell creates a file named cat in the current directory, and redirects the output of py EulerianCycle to it, passing both euleriancycle.txt and euleriancycleout.txt to it as input arguments.

  • in py EulerianCycle.py euleriancycle.txt | cat >cd outputs/euleriancycleout.txt, the shell creates a file named cd in the current directory, cat reads outputs/euleriancycleout.txt and writes it to file cd, ignoring standard input from the pipe (cat only reads standard input when it is given no input files, or an explicit -).

Perhaps what you were aiming for here was to pipe the output to a subshell like:

py EulerianCycle.py euleriancycle.txt | (cd outputs; cat > euleriancycleout.txt)

or

py EulerianCycle.py euleriancycle.txt | (cd outputs && cat > euleriancycleout.txt)

Here, cat reads the subshell's standard input - which is provided by the pipe - after changing to the target directory. The second version only creates euleriancycleout.txt if the cd command succeeds; the first creates it in the current directory if the cd fails.

mook765 avatar
cn flag
It would be great if you could add something about the initial broken pipe error which I think occurs due to the `cd`-shell-builtin which doesnt accept std-in.
trinity avatar
us flag
okay yes this makes sense. Your code works as intended. I also did see a random file named "cd" in the cwd. Thanks for explaining the unintentional actions of the commands I tried as well. for @mook765, the broken pipe error is as follows: py EulerianCycle.py euleriancycle.txt | cat >cd outputs/euleriancycleout.txt Traceback (most recent call last): File "/home/user/.../EulerianCycle.py", line 39, in <module> print('->'.join(eulerian_cycle(graph))) BrokenPipeError: [Errno 32] Broken pipe
hr flag
@Rukhan so presumably that's because `cat` is reading (instead of writing to) the intended output file, so there's noting actually reading what your python program is sending down the pipe
trinity avatar
us flag
@steeldriver Understood! Did not realize that cat's use is for stdin explicitly like cat EulerianCycle.py, where I was trying to make it read stdout and send it down a pipe
hr flag
@Trinity `cat` will do that - if you either give it no input files, or give it `-` as a pseudo input file. But it is not needed here.
trinity avatar
us flag
@steeldriver noted with thanks, have a great one!
Score:1
it flag

Additionally, you can use:

EulerianCycle.py | tee euleriancycleout.txt

to send the content to the text file and to stdout concurrently in 2 seperate streams. In other words the content will end up both in the text file and printed in the terminal.

I always do it this way as it shows me what wrote, and saves me having to open up vim to check my work.

Nate T avatar
it flag
Note: This is meant only to provide additional context, in support of the answer above.
trinity avatar
us flag
Thanks for the info, really useful!
Score:-1
us flag

Figured out I can use py EulerianCycle.py euleriancycle.txt | cat > euleriancycleout.txt | mv -t outputs/ euleriancycleout.txt

OR

py EulerianCycle.py euleriancycle.txt | cat > euleriancycleout.txt & mv -t outputs/ euleriancycleout.txt

Still open to a possible command that will do this more concisely. :)

edit: code presented is not efficient, see green check marked answer

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.