Questions tagged [io-redirection]

I/O redirection is used in shell scripting. This feature enables one to take the output of a command and redirect it as an input for another command, or a command can take the input from a file instead of from the standard input stream or from the terminal. The whole process helps connect data streams in various ways.

Filter by
Sorted by
Tagged with
26 votes
2 answers
29k views

What's the difference between STDIN and arguments passed to command?

I could use the either form to execute the cat method: cat file_name cat < file_name The result is the same Then I want to execute man in the format of stdin man < file_name While file_name ...
0 votes
2 answers
45 views

Is it possible to execute a command before creating the file in which I want to redirect its output

I want to execute the following command: dune exec -- ocaml-print-intf file.ml And redirect its output to file.mli The issue is that I can not write dune exec -- ocaml-print-intf file.ml > file....
6 votes
1 answer
2k views

Logging the output of remote commands on multiple ssh servers without delay

I've written a simple bash script that ssh's into 3 hosts (2 remote, 1 my own for testing) and runs a long running gui program that outputs text to the terminal that I'd like to log. #!/bin/bash ssh ...
0 votes
3 answers
36 views

Is there an equivalent to /dev/stdout for file descriptors 3-255?

I know that one can often trick command line utilities that expect a file to pipe into stdout out by passing /dev/stdout as the file name. I'm wondering whether it's possible to make a tool pipe to a ...
0 votes
1 answer
23 views

Is it possible to create a ncat loop?

If you had two computers, wanting to control one from another using only ncat, without using telnet, ssh or other networking tools and only two shells, how could i go about that? I have successfully ...
0 votes
1 answer
52 views

redirect TTY input to output without serial cable

I'm using simulation software for unix (ucsim) that runs two simulated programs that supposed to connect to each other via its own simulated serial ports. The software provides me with these methods ...
1 vote
2 answers
2k views

Limit in saving program output to file in Linux

I know I can use: $ my_program > output.txt To redirect the output to a file, but the problem I have is that when the file reaches 64 Kb no more is written in the file and I lose all information ...
3 votes
1 answer
98 views

"Ambigous Redirect" for redirecting text to a variable

I'm making a shell script to limit the amount of time someone spends on a program but it gives me these errors: ./time_limit: line 26: $Log: ambiguous redirect ./time_limit: line 16: [: root: unary ...
2 votes
1 answer
553 views

Why is using "|" or "&" provide different sizes of the result file?

Here is my explanation: strings *.bin > bin.txt | sort -n bin.txt > logs1.txt that gives me logs1.txt, but it has 621 KB. strings *.bin > bin.txt & sort -n bin.txt > logs1.txt that ...
14 votes
3 answers
10k views

How to make a variable from a subshell available in the parent shell

I've written a quick-and-dirty script to time some reports from a web service: BASE_URL='http://example.com/json/webservice/' FIRST=1 FINAL=10000 for report_code in $(seq 1 $FINAL); do (time -p ...
0 votes
1 answer
32 views

How to get color in terminal but no color in output file [duplicate]

To show output in both terminal and to a file, tee can be used like below ls --color=auto | tee opfile But now, there is no color in the terminal. If I force colors, opfile also has colors which ...
-1 votes
1 answer
20 views

Using a pipe with extra arguments at the end. Correctly directing the pipe instruction with multiple arguments

General description: I had a couple of times the same issue, that I solved by doing an step by step script. But I know that with pipe, one-liners would make my life way easier. Simply: command1 -flag ...
3 votes
3 answers
89 views

How to redirect a list of human readable paths to another command?

When working with the output of commands such as locate which produce lists of paths in "human readable form" (i.e. without \ in front of spaces), how do you redirect their output to another command? ...
0 votes
1 answer
1k views

monitoring a process each 1 sec and directing output to a file

I have a task in which I have to analyze a file transfer between a ftp client and a server. I know about htop command. If the file transfer takes 15 secs, then I want the htop command to run each ...
4 votes
1 answer
77 views

Bash redirection weird behavior

I was messing around with trapping exit codes and redirecting stdout and stderr of a case statement when I ran into an interesting outcome that I am hoping someone can shine some light into. I am ...
4 votes
1 answer
165 views

feed output of command to another command as file

I can make config file of tor with cat <<EOF command as follows : cat <<EOF SocksPort 9050 ControlPort 9150 DataDirectory /usr/var/lib/tor/torrc EOF but it can save output with redirect &...
3 votes
2 answers
6k views

Log the output of Expect command

I have made the below expect script and I need to log the output of that script. SOURCE_FILE=`ls -l *.txt --time-style=+%D | grep ${DT} | grep -v '^d' | awk '{print $NF}' ` if [ -n "${SOURCE_FILE}" ] ...
0 votes
1 answer
23 views

How to redirect while ignoring some of the text

Some potential duplicates may be the following: How to ignore specific lines from being redirected BUT I can't extrapolate from that to solve my problem. So, I am asking here instead. So, here is what ...
4 votes
1 answer
186 views

Why I need to type press Enter key to finish this command? [duplicate]

I'm a new Linux user and I was doing some experiments and trying to understand Process Substitution. I believe I already have a basic understanding of it. But here is a case that I don't know why. I'm ...
0 votes
1 answer
89 views

How to keep the output order when redirecting both stdout and stderr?

To describe the issue, I created a simple C program. root@u2004:~# cat test.c #include <stdio.h> int main(){ printf("output 1\n"); fprintf(stderr, "error 1\n"); ...
3 votes
3 answers
683 views

Display last n lines of the output of a command

I am trying to run docker build command. I wanted to see only last 'n' lines of the output continuously. For example, usual docker build command give me something like: Building myimage Step 1/15 : ...
0 votes
1 answer
31 views

stdin/stderr redirection, piping and color

I've seen a zillion permutations on this question but I think none is an exact duplicate. Can I copy only stderr (or eventually stderr and stdin) to a file, while keeping the colorized output on the ...
1 vote
1 answer
34 views

How does Input/Output Redirection work in Linux/Unix?

Example: cat < test.txt Is the content of the file test.txt written/passed to stdin of the cat, and then the cat reads its stdin? OR Does the file test.txt itself become the stdin of the cat? ...
1 vote
1 answer
1k views

Best Practice: How to maintain/rotate log files (from redirected output)

I read several question for the mentioned topic, but I'm not sure if they really answer the question for my problem. I have an application running and I redirect the output to a log file, i.e., ...
1 vote
3 answers
2k views

CSH and all output (with errors) to file

I need save all output from compilation to file. I have many errors "undefined reference" and I want see all of these errors. I try "makeall >& out", but in file "out" aren't any of error "...
0 votes
0 answers
24 views

pass output as grep pattern [duplicate]

I wish to pass each line of a file as a grep pattern to be searched in data.txt file. Thus, i need cat allsearchstrings.txt | grep <should be lines of allsearchstrings.txt> data.txt cat ...
-1 votes
1 answer
40 views

What actually happens when I enter the following prompt on the CLI: cat demo.txt >> cat sample.txt?

Let say we have a text file demo.txt that only contains the word 'demo' and a text file sample.txt that only contains the word 'sample'. I understand that cat demo.txt >> sample.txt would append ...
2 votes
0 answers
22 views

bash: debug (xtrace) target of redirections

Is there a way to show redirections with bash -x? $ bash -exc 'for word in foo bat baz; do echo $word >$word; done' + for word in foo bat baz + echo foo + for word in foo bat baz + echo bat + for ...
5 votes
2 answers
7k views

Duplication of file descriptors in redirection

From the The GNU Bash Reference Manual, section 3.6 Redirections: Bash handles several filenames specially when they are used in redirections, as described in the following table: /dev/fd/fd If fd is ...
0 votes
1 answer
126 views

Can redirecting stderr and stdout to one file result in an error?

I'm writing a script that takes files (filename...) as arguments, and redirects both the stdout and stderr to a file of name filename.output. In the files are commands, and I'd only like to bash once (...
0 votes
0 answers
52 views

"> $logfile" does not truncate, file size goes to 0 and a second later, is back to full size [duplicate]

I have a script that writes to a logfile like this: $ nohup myscript.sh > myscript.out 2>&1 & when the log file gets very large, I need to truncate it like this: > myscript.out I ...
0 votes
2 answers
27 views

ps aux --headers problem on openSUSE Leap 15.3 when redirect output

The following works ok on openSUSE Leap 15.3. As in the header line is repeated for each screen of output when I scroll the screen back with the scroll bar on the terminal: ps aux --headers But when ...
0 votes
2 answers
41 views

Why do these two commands to write text-processing results back to the input file behave so differently?

I have a file authorized_keys and want to deduplicate the content, i.e. remove duplicate entries. I found two possible solutions to achieve this: Use cat and uniq, then redirect the output to the ...
7 votes
5 answers
20k views

When I redirect the output of ls to a file, the filename is included in that file. How can I avoid this?

Observe: $ ls $ ls > list $ cat list list This appears to indicate that when ls is executed that the redirection into file list has already begun and the list file is already created. A fine ...
1 vote
0 answers
26 views

Why does redirection cause output to be duplicated? [duplicate]

In zsh, why does echo hi >&2 | cat produce the output hi hi ? My understanding is >&2 causes dup2(STDERR_FILENO, STDOUT_FILENO) to be called. | cat causes dup2(fd, STDOUT_FILENO) to ...
1 vote
1 answer
50 views

Redirect Output Back to File [duplicate]

I issued the following command, thinking it would take the contents of foo.txt, process it, and write it back to the original file. Instead when I opened the file, it was empty. cat foo.txt | tr "...
94 votes
4 answers
107k views

What does "3>&1 1>&2 2>&3" do in a script?

I saw this line in a script: DEVICE=`dialog --inputbox "Festplatten-Laufzeit auslesen. Gebe Sie das gewünschte Device an: " 0 70 "" 3>&1 1>&2 2>&3` What is 3>&1 1>&...
107 votes
3 answers
339k views

How can I assign the output of a command to a shell variable?

I want to assign the result of an expression (i.e., the output from a command) to a variable and then manipulate it – for example, concatenate it with a string, then echo it.  Here's what I've got: #!/...
103 votes
6 answers
45k views

How can I create a /dev/null-like "blackhole" directory?

I would like to create a "/dev/null" directory (or a "blackhole" directory) such that any files written to it are not really written, but just disappear. I have an application that writes out large ...
85 votes
3 answers
171k views

Can sed save its output to a file?

> can do it. echo "text" > file tee can do it. echo "test" | tee file Can sed do it without using either of the above? Is it possible to save the output of a sed command to a file without ...
0 votes
0 answers
57 views

How do I capture binary output from a process different from the console output?

I'm currently able to launch a process from within Java's ProcessBuilder, which continually appends to a (binary) file output.WAV, and is continually dumping lines of console updates. That works ...
0 votes
2 answers
36 views

Redirect bash script output of application to file keeping the application interactive

I'm running a bash script expecting occasional user input to continue processing the script. Is it possible to redirect the script output to a file while keeping the terminal output and keeping the ...
0 votes
0 answers
22 views

File I/O redirection help in Bash for running badblocks into gzip and monitor with zcat [duplicate]

I am running this on a 10 TB bad drive, so the log grows out of proportions in an hour or so. This was my idea, not working until I kill the badblocks: badblocks -v -b 4096 -c 98304 -w -s /dev/disk/by-...
2 votes
0 answers
78 views

What is the purpose of a pipe after stdout redirect?

I was reading the documentation for Kaniko and saw a shell redirect-then-pipe construct that I haven't seen before: echo -e 'FROM alpine \nRUN echo "created from standard input"' > ...
0 votes
1 answer
91 views

grep screen output differs from output to file

When using grep (and sed also) the results I get in the terminal are different than what I get if I direct the output to file. My starting file is the captured output from a darknet yolo mAP run and ...
14 votes
2 answers
5k views

Why are < or > required to use /dev/tcp

When trying to call /dev/tcp/www.google.com/80, by typing /dev/tcp/www.google.com/80 Bash says no such file or directory. When looking at other people's code online, they use syntax such as 3<&...
0 votes
1 answer
165 views

echo "Hello World" > /dev/stdout prints nothing

Scenario: $ echo "Hello World" > /dev/stderr Hello World $ echo "Hello World" > /dev/stdout $ uname -a CYGWIN_NT-10.0 xxx 3.3.4(0.341/5/3) 2022-01-31 19:35 x86_64 Cygwin ...
530 votes
10 answers
1.1m views

How to append multiple lines to a file

I am writing a bash script to look for a file if it doesn't exist then create it and append this to it: Host localhost ForwardAgent yes So "line then new line 'tab' then text" I think its a ...
2 votes
1 answer
76 views

POSIX Shell: Output to log and prepend lines with timestamps using a function

I would like a shell script to have all it's output for stdout and stderr logged to an external file with timestamps prepended to each line (plus a few extra items), without changing how the script is ...
0 votes
1 answer
145 views

CRON 2>&1 >/dev/null vs >/dev/null 2>&1 [duplicate]

I just created a tiny shell script to be run every minute. What happened was that it started flooding my mail. $ crontab -l * * * * * /home/user/bin/test 2>&1 >/dev/null I changed the ...

1
2 3 4 5
28