# How to redirect the output of run to a file

**URL:** <https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 17, 2017, 8:02am UTC](https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765 "2017-05-17T08:02:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 17, 2017, 8:02am UTC](https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765/1 "2017-05-17T08:02:07Z")

</div>

MWE: suppose I wanted to do something equivalent to the shell command

```nohighlight
ls /tmp > /tmp/list

```

in Julia. I came up with

```julia
open(io->run(pipeline(`ls /tmp`, stdout=io)), "/tmp/list", "w")

```

but I fear that I am overcomplicating it.

---

<div class="post-metadata">

**Author:** ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)\
**Post date:** [October 12, 2017, 3:24pm UTC](https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765/2 "2017-10-12T15:24:21Z")

</div>

So, is there an answer to that question? I just came across that issue and I am wondering how to redirect in a run command.

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [October 12, 2017, 3:36pm UTC](https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765/3 "2017-10-12T15:36:26Z")

</div>

You can use the `@capture_out` macro from [Suppressor.jl](https://github.com/Ismael-VC/Suppressor.jl), then write it to a file:

```julia
using Suppressor

output = @capture_out run(`ls`)

open("/var/log/myprogram.txt", "w") do io
    write(io, output)
end

```

---

<div class="post-metadata">

**Author:** ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)\
**Post date:** [October 12, 2017, 3:38pm UTC](https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765/4 "2017-10-12T15:38:33Z")

</div>

Ah, thanks, interesting. This looks complicated though.

The following seems to work, but I am not sure how to deal with more complex commands (e.g. with &\> etc)

```
  run(pipeline(`ls /tmp`, stdout = "/tmp/list"))

```

The `|>` operator used to [work](http://blog.leahhanson.us/post/julia/julia-commands.html) for redirection, but the feature seems to have been removed.

---

<div class="post-metadata">

**Author:** ![NaOH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naoh/32/5632_2.png) [@NaOH](https://discourse.julialang.org/u/NaOH)\
**Post date:** [November 4, 2017, 6:41pm UTC](https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765/5 "2017-11-04T18:41:35Z")

</div>

This is the most julian:

```julia
julia> open("out.txt", "w") do file                      
           write(file, readstring(`ls -l`))              
       end                                               
383                                                      
                                                         
shell> cat out.txt                                       
total 13                                                 
-rw-r--r-- 1 Ismael 197609 1203 Nov 4 00:17 LICENSE.md  
-rw-r--r-- 1 Ismael 197609 791 Nov 4 05:44 README.md   
-rw-r--r-- 1 Ismael 197609 25 Nov 4 00:17 REQUIRE     
-rw-r--r-- 1 Ismael 197609 2195 Nov 4 00:17 appveyor.yml
-rw-r--r-- 1 Ismael 197609 0 Nov 4 12:40 out.txt     
drwxr-xr-x 1 Ismael 197609 0 Nov 4 00:17 src         
drwxr-xr-x 1 Ismael 197609 0 Nov 4 00:17 test        

```

---

<div class="post-metadata">

**Author:** ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)\
**Post date:** [November 4, 2017, 6:47pm UTC](https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765/6 "2017-11-04T18:47:43Z")

</div>

Nice. Even shorter:

```
    write("output.txt", readstring(`ls -l`))

```

---

<div class="post-metadata">

**Author:** ![NaOH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naoh/32/5632_2.png) [@NaOH](https://discourse.julialang.org/u/NaOH)\
**Post date:** [November 4, 2017, 6:50pm UTC](https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765/7 "2017-11-04T18:50:57Z")

</div>

Neat! I didn’t know about that. 😃

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 15, 2019, 12:14pm UTC](https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765/8 "2019-07-15T12:14:45Z")

</div>

Can someone post an update of this? `readstring` is no longer available in Julia 1.0.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [July 15, 2019, 12:17pm UTC](https://discourse.julialang.org/t/how-to-redirect-the-output-of-run-to-a-file/3765/9 "2019-07-15T12:17:20Z")

</div>

Just do

```julia
write("out.txt", read(`ls -l`))

```
