io redirection is the process of changing where the output and input of a program originate

learn more… | top users | synonyms (1)

0
votes
2answers
1k views

How to redirect output of a 'xterm -hold -e' command

I know you can use tee to log output from a command in a terminal, e.g.: df -h | tee ~/log.txt However, how can I do this with a more complex command like this: xterm -hold -e program arg1 arg2 ...
0
votes
2answers
33 views

Why cant I overwrite a file with processed value?

This question prompted a thought I was not sure I understood well. I know that it is not possible or correct to use pipelines like cat myfile | grep -v mypattern > myfile due to how file handles ...
4
votes
1answer
823 views

KSH styling text based menu using STDERR

Is it possible to format the STDERR in order to have a better looking menu using the select command? I have a simple select select oChoice in $(<tempMenu.menu) ; do case "$oChoice" in ...
5
votes
2answers
30 views

What exactly happens if input and output file is the same?

Let's say that I execute following command: tr a-z A-Z < file > file There are two redirections: < file and > file. Both are processed before tr command and as far as I know, if there ...
0
votes
1answer
30 views

unzip without replacing original file and report to log

I want to write a bash script to decompress a file without overwriting the original one and redirect the operation result into a log file. I found an answer to the first part of my question at: How ...
8
votes
4answers
536 views

Bash program not executed if redirection would fail

In bash, I notice that if a command using redirection would fail, any programs which run prior to that are not run. For example, this program opens the file "a" and writes 50 bytes to file "a". ...
2
votes
1answer
5k views

Correct syntax to avoid bash 'ambiguous redirect' message

I am trying to execute a command which accumulates stdout into an existing file and sends the error messages to another using the command below. commmand >> /home/user/accumulate_output.log ...
0
votes
1answer
248 views

Ambigous redirect done >$2

I have finished my bash script but I am still getting two errors. I've also searched for related issues but none worked: #!/bin/bash avem_element () { local e for e in "${@:2}"; do [[ "$e" == ...
2
votes
2answers
238 views

Error message on terminal despite using 2> symbol

Why does this command show error message despite using 2> symbol? cat < foo 2>bar bash : cannot open foo : No such file or directory
6
votes
1answer
112 views

Extra output on input redirection

I have a folder with three files: $ ls a b c If I pipe the output of ls to wc, I get the right result: $ ls | wc -l 3 However, when I specify the input to wc as the output of ls, I get extra ...
7
votes
2answers
241 views

Redirecting standard error output to bash variable

Here's a C code snippet that will cause segfault: // segfault.c #include <string.h> int main() { memset((char *)0x0, 1, 100); return 1; } Compile it with: gcc segfault.c -o ...
2
votes
2answers
282 views

How can I redirect the output of a child process?

I am running the following git clone command through sudo and bash and I want to redirect STDOUT to a log file: % sudo -u test_user bash -c "git clone https://github.com/scrooloose/nerdtree.git ...
2
votes
1answer
20 views

Unable to redirect stdout/stderr to log file [duplicate]

I have a script tmp.sh #!/bin/bash echo hello sleep 3 echo 3 I want to log the script running time (as well as script stdout) to a log file. user$ time ./tmp.sh >& log.tmp real ...
39
votes
5answers
14k views

When would you use an additional file descriptor?

I know you can create a file descriptor and redirect output to it. e.g. exec 3<> /tmp/foo # open fd 3. echo a >&3 # write to it exec 3>&- # close fd 3. But you can do the same ...
1
vote
0answers
29 views

Deleted file being written to by running process, can I recreate it? [duplicate]

I've started a long running process with nohup and redirected it's output to a log file Example: nohup ./bin/zkServer.sh start-foreground > /usr/local/myuser/logs/zkout.log 2>&1 & I ...
2
votes
1answer
22 views

Connect via telnet and doing certain operations

I'm trying to code a script where I can connect via telnet to another server and execute certain operations, but I'm having problems with the result of this process. The objective is to connect to a ...
4
votes
4answers
3k views

Getting stdin from a named pipe

What I am trying to do is run python in a terminal window and redirect it's stdin from a named pipe. Then I write to the named pipe in another terminal and have that command execute on python. ...
0
votes
1answer
31 views

How to generate a text report in Linux?

I have a command I'm using to find all of the *.xml files on our server. I'm using: find -type f -exec grep -il "xml" {} \; This is great, but there are maybe a thousand files like this on the ...
1
vote
1answer
151 views

Sed regex printing into a file.txt

I'm connected to a MQTT stream, and I filter out the data by entering : mosquitto_sub -d -t +/# 2>&1 | sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;p}" into the Ubuntu terminal. ...
1
vote
2answers
77 views

Why is this script looping on the first line of the txt file?

#!/bin/bash usernameFile="/home/netadmin/username_list.txt" logFile="/var/log/netvpn-mag-archive/netvpn-mag-20160" while read -r line < $usernameFile; do if [[ "$line" != " " ]]; then ...
1
vote
5answers
3k views

Why does the command shuf file > file leave an empty file?

I know this is sort of a duplicate of another question (Why this sort command gives me an empty file?) but I wanted to expand on the question in response to the answers given. The command shuf ...
35
votes
5answers
22k views

How can I make iconv replace the input file with the converted output?

I have a bash script which enumerates through every *.php file in a directory and applies iconv to it. This gets output in STDOUT. Since adding the -o parameter ( in my experience ) actually writes ...
2
votes
2answers
245 views

xargs and < and > to the same files

I want to run git stripspace on all my source code files, on each build (or precommit or just manually), to remove extraneous whitespace, and later maybe to format the code in a standard way, So ...
0
votes
6answers
107 views

Is there a way to do a “full file buffering” inside a chain of piped commands?

Very often I do simple operations on a file, for example: cat file1.txt|sed -r 's/^ *//' to remove spaces at the beginning of the line. If I want to overwrite the file, the only way I know is: cat ...
8
votes
6answers
1k views

IO redirection and the head command

I was trying to quickly edit an .hgignore file from the Cygwin bash shell today, and I added a line that was a mistake. I'm not sure if this was the best way to do it, but I quickly thought of using ...
36
votes
5answers
46k views

How to view the output of a running process in another bash session?

I have left a script running on a remote machine from when I was locally working at it. I can connect over SSH to the machine as the same user and see the script running in ps. $ ps aux | grep ...
19
votes
5answers
18k views

view stdout for another pts [duplicate]

Here is the situation. I left my pc at home doing an rsync from a 2TB hard drive to another 2TB hard drive (it's going to take a while since they are both USB 2.0). I am now at work and I have ssh-ed ...
3
votes
2answers
2k views

How do I redirect only stderr?

I am having a bit of trouble doing this. I am required to run a compiled .java file and redirect only stderr to a file called error. So the .java file is named javaProgram.java. This is what I have ...
0
votes
2answers
41 views

How is redirection implemented?

Is redirection a concept of the shell, not of the OS kernel? How is redirection implemented in terms of system calls? Is redirection implemented similarly to pipe?
0
votes
1answer
27 views

Change a file while redirecting it as stdin input to a command

In Bash, while running mycommand < myfile, I modify myfile's content. What content of myfile does mycommand get? Does mycommand get the original content of myfile when i started running it, or ...
0
votes
0answers
21 views

Emptying a log file getting updated with 2>&1 output redirection [duplicate]

I have a server which constantly writes outputs and errors to stdout and stderr. The command line I use for running my server is: Server options-list > log.txt 2>&1 & Now the problem ...
0
votes
2answers
40 views

Piping sed/grep output does not work

If I run the commands bellow, only out1 has output, the out2 and out3 are empty. # this is just to generate a self-signed certificate openssl genrsa -out /tmp/ssl.key 2048 openssl req -sha256 -new ...
1
vote
2answers
62 views

How to redirect STDIN of background process?

In my Ubuntu, i run java application in background. I use bash script to run it and now it looks like: nohup java -jar app.jar & exit 0 The problem that i want to be able to write an input ...
37
votes
3answers
7k views

What are the shell's control and redirection operators?

I often see tutorials online that connect various commands with different symbols. For example: command1 | command2 command1 & command2 command1 || command2 command1 && command2 ...
6
votes
3answers
5k views

Write to a file without redirection?

I am writing a regular compiled application that needs to create a special file and write a magic cookie into it. I can’t write the file directly from the application, the system security model ...
10
votes
1answer
804 views

Shell variable $_ not behaving as expected

What the reason the number of lines differs? $ head -n 100000 ./access.log > ./data/log.sample $ cat $_ | wc -l 1933424
0
votes
1answer
20 views

Archlinux'es pacman + tee == unclear console IO [closed]

I have a small issue with capturing the pacman's IO with the tee utility. I try this command line: pacman -S group-1 group-2 | tee pacman.log And there is an issue: when the pacman query which ...
1
vote
1answer
38 views

“time > time.txt” refuses to work propely

I just started learning Linux commands. I was experimenting with the > command, that, as far as I understand, makes the command before it write it's output to the file after the sign. This ...
0
votes
2answers
33k views

Redirecting the content of a file to the command “echo”

I have a file named my_file.txt whose content is just the string Hello. How could I redirect its content to the command echo? I know I have the commands less, cat, more... but I need to do it with ...
0
votes
1answer
30 views

Redirection Error in Function [closed]

I'm trying to figure out why the code below doesn't work, and gives me an error Bad file descriptor. It is sort of a follow-up to this question as applies to the script I'm currently working on. ...
58
votes
4answers
185k views

How to append multiple lines to a file with bash

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 ...
3
votes
2answers
81 views

Returning a String and Understanding Redirection and Output

I'm trying to figure out how redirects could be used to selectively output a string from a bash function which needs to print multiple lines to the terminal. I found an answer on this question at ...
1
vote
2answers
47 views

Difference between 1> and >

Is there any difference between (# comments taken from documentation) command > filename # Docs: Redirect stdout to a file. and command 1> filename # Docs: Redirect stdout to file ...
2
votes
1answer
29 views

Sending `time` command into text file [duplicate]

I want to get the result of time command into a text file but it's not working it only put blank space in the text file. I already tried this commands, A- $ x=`time` $ echo $x > log.txt $ cat ...
4
votes
2answers
493 views

Redirect the output of a command in `time command`

I am trying to time something using: /usr/bin/time myCommand However, since /usr/bin/time writes to stderr, if myCommand also writes to stderr, I will get more than just time's output on the ...
52
votes
2answers
45k views

View stdout/stderr of systemd service

I have created a simple systemd service file for a custom application. The application works well when I run it manually, but my CPU gets maxed out when I run it with systemd. I'm trying do track ...
12
votes
4answers
11k views

bash: Use a variable to store stderr|stdout redirection

Is there any way to redirect stdout and stderr via variable like adding command options in script? For example I have a script: #!/bin/bash -x TEST=">/dev/null 2>&1" OPT='-p -v' mkdir $OPT ...
4
votes
1answer
63 views

File descriptors and redirect in bash

Can someone explain why this command works: gpg --enable-special-filenames --verify --batch \ <(curl -s https://www.gnupg.org/ftp/gcrypt/gnupg/gnupg-2.0.29.tar.bz2.sig) \ <(curl ...
4
votes
3answers
188 views

Trying to make sense of bash redirection syntaxes and their outputs

I'm new to Linux and trying to understand how redirections work. I have been testing various syntaxes for redirecting stdout and stderr to the same file, which don't all produce the same results. ...
0
votes
2answers
59 views

How can I clear/empty a file being piped to?

Say I have some process which pipes to a file: yes > foo I know want to clear/empty the file foo, but still want the process to write to the file, i.e. start filling the file again after ...