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

learn more… | top users | synonyms (1)

96
votes
1answer
5k views

Bash: What does “>|” do?

I have just seen this written down; $ some-command >| /tmp/output.txt Vertical pipes are used in standard redirects "piping" the output of one command to another, is >| in fact completely ...
43
votes
5answers
3k views

Can a command line program prevent its output being redirected?

I have become so used to do this: someprogram >output.file I do it whenever I want to save the output that a program generates to a file. I am also aware of the two variants of this IO ...
43
votes
5answers
4k views

Redirecting stdout to a file you don't have write permission on

When you attempt to modify a file without having write permissions on it, you get an error: > touch /tmp/foo && sudo chown root /tmp/foo > echo test > /tmp/foo zsh: permission ...
33
votes
4answers
17k views

How can I send stdout to multiple commands?

There are some commands which filter or act on input, and then pass it along as output, I think usually to stdout - but some commands will just take the stdin and do whatever they do with it, and ...
30
votes
10answers
12k views

How do I reuse the last output from the command line?

This is a noob question, but I'd like to know how to reuse the last output from the console, ie: pv-3:method Xavier$ python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" ...
29
votes
5answers
12k 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 ...
27
votes
7answers
19k views

How to grep standard error stream (stderr)?

I am using ffmpeg to get the meta info of an audio clip. But I am unable to grep it. $ ffmpeg -i 01-Daemon.mp3 |grep -i Duration FFmpeg version SVN-r15261, Copyright (c) 2000-2008 Fabrice ...
27
votes
2answers
1k views

Command line: <<< instead of <<

Why is the command md5sum <<< 'ddd' (output: d6d88f2e50080b9602da53dac1102762 -) right, and md5sum << 'ddd' not? What does <<< mean?
26
votes
4answers
3k views

What is the correct name for the “>” command

I was trying to find how to pass some text to a file without overwriting what's there already using the > command and I realised I don't know what it's called. Searching for right arrow or right ...
25
votes
2answers
1k views

What does a “< <(…)” redirection mean?

I've seen that rvm (ruby version manager) is installed using the following command: bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer ) So as I ...
24
votes
6answers
2k views

Why can't I redirect a path name output from one command to “cd”?

I am trying to get cd to accept a directory name redirected to it from another command. Neither of these methods work: $ echo $HOME | cd $ echo $HOME | xargs cd This does work: $ cd $(echo $HOME) ...
24
votes
3answers
11k views

Reading lines from a file with bash: for vs. while

I'm trying to read a text file and do something with each line, using a bash script. So, I have a list that looks like this: server1 server2 server3 server4 I thought I could loop over this using ...
23
votes
4answers
15k views

How to redirect output of wget as input to unzip?

I have to download a file from this link. The file download is a zip file which I will have to unzip in the current folder. Normally, I would download it first, then run the unzip command. $ wget ...
23
votes
1answer
578 views

How do I redirect command completion output?

On the command line, I can redirect or pipe output of a command to a file or another command using the > or | operator after the command. I have come across a less standard situation that I would ...
22
votes
2answers
10k 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 ...
21
votes
1answer
7k views

Why is redirecting STDERR to /dev/null done this way?

This does not make sense to me. wibble > /dev/null 2>&1 I think it would make more sense if it was something like this: wibble 2>&1 > /dev/null In other words Commands ...
19
votes
5answers
11k views

Using while loop to ssh to multiple servers

I have a file servers.txt, with list of servers: server1.mydomain.com server2.mydomain.com server3.mydomain.com when I read the file line by line with while and echo each line, all works as ...
19
votes
4answers
3k views

Process substitution and pipe

I was wondering how to understand the following: Piping the stdout of a command into the stdin of another is a powerful technique. But, what if you need to pipe the stdout of multiple ...
19
votes
4answers
3k views

What happens to the output of a process that has been disowned and lost its terminal?

If I close the virtual terminal, where some process was started, does the output just go straight to /dev/null, or can it pollute memory somehow? Can I anyhow grab the output to continue reading it at ...
17
votes
1answer
2k views

Is there a way in bash to redirect output and still have it go to stdout?

Okay. If I wanted to redirect the output of a program to a file, I'd do something like this prog > file If I wanted to redirect both stdout and stderr to that file, then I'd do prog > file ...
17
votes
2answers
10k 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 ...
16
votes
3answers
2k views

How to redirect output of a running program to /dev/null

I know that in order to suppress the output of a program I can redirect it to /dev/null, for example to hide all error and warning messages from chromium I can start the program like this ...
16
votes
2answers
462 views

Why does this command create an enormously large file?

I was experimenting today with some append operations and, in my curiosity, ran this (where file1.txt was non-empty and file2.txt was empty): $ cat file1.txt >> file2.txt >> file1.txt ...
15
votes
2answers
536 views

How can I stream data to a program that expects to read data from a file that is given as a argument?

I have a huge gzipped file and I want a program (4s-import in this case) to read it. It takes a lot of time to first unzip the file and then call the program with the path to the file as an argument. ...
15
votes
2answers
2k views

Does redirecting output to a file apply a lock on the file?

If I have a command $ ./script >> file.log that gets called twice, with the second call occurring before the first one ends, what happens? Does the first call get an exclusive lock on the ...
14
votes
1answer
523 views

Grep alias - line numbers unless it's in a pipeline

I want to create a bash alias for grep that adds line numbers: alias grep='grep -n' But that, of course, adds line numbers to pipelines as well. Most of the time (and no exceptions come to mind) I ...
13
votes
2answers
449 views

What does redirection with >| do?

When should you use >| for redirecting output to a file instead of just >? The explanation given when I was shown this was that it guaranteed the target file was truncated first. But I thought ...
13
votes
5answers
10k views

view stdout for another pts

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

Redirect all subsequent commands' stderr using exec

I have a bash file that I need to redirect all output to one file, debug log as well as to the terminal. I need to redirect both stdout and stderr to the debug and log it for all commands in the ...
13
votes
4answers
942 views

Is it possible for a sysadmin to eavesdrop on his/her users' terminals?

When logged in to a machine, I can find out each user's pseudo-terminal device(s) from the output of w. Being a sysadmin, is it possible for me to eavesdrop on this terminal without the user being ...
13
votes
2answers
742 views

Redirect stderr from an already running script

I've been running a script for several days now. I redirected stdout to $HOME/mylog, but didn't redirect stderr since I thought there would be nothing on it. Suddenly thousands of lines started coming ...
12
votes
7answers
365 views

How do you remember the syntax for 2>&1 redirection [closed]

Do you have a mnemonic or system? This has bothered me for years I always have to look it up
12
votes
3answers
354 views

How to redirect whatever I enter in terminal to file

I am a novice just learning Fedora 13. I just wanted to know if its possible to redirect whatever I type in the terminal to a file, so that I would be able to see from the file, that a particular ...
12
votes
2answers
7k views

/dev/tcp listen instead of nc listen

With a netcat listener like: nc -l <port> < ~/.bashrc I can grab my .bashrc on a new machine (doesn't have nc or LDAP) with: cat < /dev/tcp/<ip>/<port> > ~/.bashrc My ...
12
votes
3answers
386 views

When to use redirection to stderr in shell scripts

I know that well-behaved utilities like grep output "normal" messages to stdout, and error messages to stderr. $ grep '^foo' file1 file2 file1:foo grep: file2: No such file or directory When I'm ...
12
votes
3answers
842 views

Practical use for moving file descriptors

According to the bash man page: The redirection operator [n]<&digit- moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is ...
12
votes
5answers
11k views

Show only stderr on screen but write both stdout and stderr to file

How can I use BASH magic to achieve this? I want to only see stderr output on the screen, but I want both stdout and stderr to be written to a file. Clarification: I want both stdout and stderr to ...
11
votes
2answers
641 views

Why is it that these two 'cat' commands result differently?

Let's assume that infile contains a specific text, and I were to execute the following set of commands: exec 3<infile cat -n <&3 cat -n <&3 The first instance of cat ...
11
votes
3answers
3k views

How can I pipe output to another process, but retain the error state of the first process? [duplicate]

Possible Duplicate: Get exit code of process that's piped to another I am using the following command line (in a makefile) to pipe the verbose error messages from my compiler through a ...
11
votes
6answers
6k views

xargs with stdin/stdout redirection

I would like to run: ./a.out < x.dat > x.ans for each *.dat file in the directory A. Sure, it could be done by bash/python/whatsoever script, but I like to write sexy one-liner. All I could ...
11
votes
2answers
13k views

Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1

Just looking for the difference between 2>&- 2>/dev/null |& &>/dev/null >/dev/null 2>&1 and their portability with non-Bourne shells like tcsh, mksh, etc.
11
votes
4answers
13k views

How do I completely silence a cronjob to /dev/null/?

On my Ubuntu-Desktop and on my debian-server I have a script which needs to be executed each minute (a script that calls the minute-tic of my space online browsergame). The problem is that on ...
11
votes
1answer
23k views

Is there a way to redirect nohup output to a log file other than nohup.out?

I frequently use the program nohup so that my processes are immune to hangups. So if I want to make the program program immune to hangups, I use the command nohup program & where & puts ...
11
votes
4answers
18k views

Difference between 2>&1 > output.log and 2>&1 | tee output.log

I wanted to know the difference between the following two commands 2>&1 > output.log and 2>&1 | tee output.log I saw one of my colleague use second option to redirect. I know ...
11
votes
2answers
3k views

How to diff a file and output from the command?

Normally you would write: diff file1 file2 But I would like to diff a file and output from the command (here I make command a trivial one): diff file1 <(cat file2 | sort) Ok, this work when I ...
11
votes
2answers
206 views

Why does cat x >> x loop?

The following bash commands go into an infinte loop: $ echo hi > x $ cat x >> x I can guess that cat continues to read from x after it has started writing to stdout. What is confusing, ...
11
votes
2answers
205 views

Output order with process substitution

This is what I usually do to run grep and wc on a file without having to scan it twice <file.txt tee >(grep LITERAL) >(wc -l) >/dev/null However, this yields EXEC LITERAL 32 ...
10
votes
5answers
1k views

Difference between cat and '>' to zero out a file

Are these two commands any different on how they go about zero-ing out files? Is the latter a shorter way of doing the former? What is happening behind the scenes? Both $ cat /dev/null > ...
10
votes
3answers
23k 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 ...
10
votes
5answers
8k views

How to limit log file size using >>

How can I limit the size of a log file written with >> to 200MB? $ run_program >> myprogram.log