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

learn more… | top users | synonyms (1)

44
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 ...
26
votes
3answers
11k 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 ...
9
votes
1answer
8k views

File descriptors & shell scripting

I am having a very hard time understanding how does one use file descriptors in shell scripts. I know the basics such as exec 5 > /tmp/foo So fd 5 is attached to foo for writing. exec 6 < ...
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 ...
13
votes
5answers
12k 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 ...
10
votes
2answers
509 views

Order of redirections

I don't quite understand how the computer reads this command. cat file1 file2 1> file.txt 2>&1 If I understand, 2>&1 simply redirect Standard Error to Standard Output. By that ...
37
votes
4answers
20k 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 ...
32
votes
10answers
13k 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()" ...
27
votes
7answers
20k 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 ...
13
votes
2answers
4k 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 ...
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) ...
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 ...
9
votes
3answers
2k views

Can I make `cut` change a file in place?

The man page doesn't give me much hope, but I'm hoping it's an undocumented (and/or GNU-specific) feature.
7
votes
5answers
605 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 ...
15
votes
5answers
11k 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 ...
16
votes
3answers
3k 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 ...
6
votes
2answers
3k views

what is meant by connecting STDOUT and STDIN?

This is a newbie question. I'm reading a book, it says: Every process has at least three communication channels available to it: “standard input” (STDIN), “standard output” (STDOUT), and ...
2
votes
1answer
2k views

Why the behavior of while loop and for loop is different?

I am trying to read user and server details from file tempo.txt and then check the disk space usage of the file system on that unix account using another script server_disk_space.sh.But I am not able ...
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 ...
11
votes
4answers
17k views

How to read from two input files using while loop

I wanted to know if there is any way of reading from two input files in a nested while loop one line at a time. For example, lets say I have two files FileA and FileB. FileA: [jaypal:~/Temp] cat ...
9
votes
1answer
22k views

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

I want to assign the result of an expression to a variable and concatenate it with a string, then echo it. Here's what I've got: #!/bin/bash cd ~/Desktop; thefile= ls -t -U | grep -m 1 "Screen Shot"; ...
12
votes
2answers
8k 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 ...
10
votes
2answers
933 views

Closing a file descriptor, >&- vs <&-

In the bash tutorial I am reading, it says that if you open a file descriptor for reading, i.e. exec 3< echolist Then you must close it like this, exec 3<&- However, if you open a file ...
9
votes
2answers
2k views

Use HERE file and redirect output of command

I have the following code in a batch script: mpirun -np 6 ./laplace <<END 100 100 100 0.01 100 3 2 1 END | tail -n 1 > output But it isn't working. What I want it to do is to use the HERE ...
4
votes
1answer
8k views

nohup: ignoring input and redirecting stderr to stdout

I am starting my application in the background using nohup as mentioned below - root@phx5qa01c:/bezook# nohup java -jar ./exhibitor-1.5.1/lib/exhibitor-1.5.1-jar-with-dependencies.jar -c file ...
8
votes
2answers
257 views

When should I use input redirection?

I used the following two commands to produce the same results:- [root@localhost ~]# grep line comments The line should start with a single quote to comment in VB scripting. Double slashes in the ...
2
votes
3answers
2k views

How to change the output redirection of a running process?

I know how to redirect output and how to suppress them in bash. Now, suppose I accidentally forgot to append the output redirection part to the command (e.g. 2>&1 or > /tmp/mystdout) and my ...
8
votes
3answers
2k views

How do you process an entire command as sudo that involves redirecting or piping?

I am trying to follow what I assume is best practises of using sudo instead of root account. I am running a simple concat file operation such as: sudo echo 'clock_hctosys="YES"' >> ...
6
votes
1answer
3k views

Why can't sudo redirect stdout to /etc/file, but sudo 'nano' or 'cp' can?

Why does redirection, using sudo give me an error for the following commands? $ sudo printf "foo" >/etc/file bash: /etc/file: Permission denied $ sudo printf "foo" ~/file; cat ~file ...
3
votes
2answers
174 views

Do redirection operators always open file descriptors in parallel?

1. Consider snippet#1: $ cat test.txt > test.txt cat: test.txt: input file is output file It seems that cat makes its input file descriptor point to test.txt and then when it tries to set its ...
3
votes
3answers
760 views

Concatenating thousands of files: > vs >>

I found two seemingly contradictory answers on StackOverflow to the following questions: Concatenating Thousands of Text Files Across Hundreds of Directories (while keeping some structure) How do I ...
1
vote
2answers
88 views

Redirect stdin and stdout to ports

I'd like to take a program P that reads from stdin & writes to stdout, but connect it to nc or whatever such that it reads from a certain port and outputs to another port. # The reading is easy, ...
0
votes
4answers
572 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 ...
17
votes
2answers
16k 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.
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
3answers
12k 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 ...
11
votes
4answers
6k views

How to redirect stderr,out to different files and also display in terminal?

To make it clear, I want to see the output of a command in the terminal as if there were no redirection. Also stderr need to be redirected to err.log and stdout need to be redirected to stdout.log. ...
30
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
2answers
2k views

How does `cat <> file` work?

cat < file prints the contents of file to stdout. cat > file reads stdin until Ctrl+D is detected and the input text is written to file. cat <> file, at least in my version of Bash, ...
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?
15
votes
2answers
567 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. ...
20
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 ...
14
votes
1answer
546 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 ...
11
votes
6answers
7k 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
5answers
2k views

Using text from previous commands' output

I know this is not how terminals work, but I find myself often wishing there was an easy way of using text (copying it, modifying it, etc) that is already on my terminal window history from some ...
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 ...
10
votes
3answers
280 views

Is this a typo in Bash manual's redirection section?

Note that the order of redirections is significant. For example, the command ls > dirlist 2>&1 directs both standard output and standard error to the file dirlist, while ...
5
votes
2answers
267 views

How to capture ordered STDOUT/STDERR and add timestamp/prefixes?

I have explored almost all available similar questions, to no avail. Let me describe the problem in detail: I run some unattended scripts and these can produce standard output and standard error ...
12
votes
3answers
36k 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 ...
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 ...