Questions tagged [file-descriptors]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
183
votes
3answers
165k 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.
70
votes
7answers
37k 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 ...
58
votes
2answers
4k views

Is >&- more efficient than >/dev/null?

Yesterday I read this SO comment which says that in the shell (at least bash) >&- "has the same result as" >/dev/null. That comment actually refers to the ABS guide as the source of its ...
54
votes
5answers
12k views

How portable are /dev/stdin, /dev/stdout and /dev/stderr?

Occasionally I need to specify a "path-equivalent" of one of the standard IO streams (stdin, stdout, stderr). Since 99% of the time I work with Linux, I just prepend /dev/ to get /dev/stdin, etc., ...
50
votes
3answers
44k views

Understanding /dev and its subdirs and files

$ ls -l /dev/stdin /dev/fd/0 lrwx------ 1 tim tim 64 2011-08-07 09:53 /dev/fd/0 -> /dev/pts/2 lrwxrwxrwx 1 root root 15 2011-08-06 08:14 /dev/stdin -> /proc/self/fd/0 $ ls -l /dev/pts/2 /proc/...
32
votes
1answer
40k 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 < /...
28
votes
1answer
88k views

Limits on the number of file descriptors

I'm trying to install 389-ds, And it gives me this warning: WARNING: There are only 1024 file descriptors (hard limit) available, which limit the number of simultaneous connections. I understand ...
26
votes
3answers
4k 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 ...
25
votes
1answer
1k views

What is the bash '<(file contents)' syntax called?

This answer on Security StackExchange uses an interesting bash syntax to generate a file in-line: openssl req -new -x509 -nodes -newkey ec:<(openssl ecparam -name secp384r1) -keyout cert.key -out ...
25
votes
2answers
5k views

What's the purpose of the first argument to select system call?

From man select int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); nfds is the highest-numbered file descriptor in any of the three ...
24
votes
4answers
19k views

How can same fd in different processes point to the same file?

Say I have process 1 and process 2. Both have a file descriptor corresponding to the integer 4. In each process however the file descriptor 4 points to a totally different file in the Open File Table ...
19
votes
2answers
4k views

Portability of file descriptor links

I've always wondered this but never took the time to find out, so I'll do so now - how portable is the usage shown here of either /proc/$$/fd/$N or /dev/fd/$N? I understand POSIX guarantees /dev/null, ...
17
votes
1answer
22k views

What is the file descriptor 3 assigned by default?

$ ls -og /proc/self /proc/self/fd lrwxrwxrwx 1 64 Jun 18 11:12 /proc/self -> 32157 /proc/self/fd: total 0 lrwx------ 1 64 Jun 22 2012 0 -> /dev/tty1 lrwx------ 1 64 Jun 22 2012 1 -> /dev/...
17
votes
1answer
3k views

How to terminate a TCP connection established by bash itself?

I have used exec 3<>/dev/tcp/192.168.0.101/6435 to establish a TCP connection with 192.168.0.101:6435. And I have received as well as sent a few messages with the pipe command. Now, I want to ...
17
votes
3answers
2k views

find out which file descriptors share the same “open file description”

If I do (in a Bourne-like shell): exec 3> file 4>&3 5> file 6>> file File descriptors 3 and 4, since 4 was dup()ed from 3, share the same open file description (same properties, ...
16
votes
2answers
13k views

What happens when I close() a file descriptor?

I am trying to get the whole picture with file descriptors. Say I have process1 which initially has these file descriptors: _process1_ | | | 0 stdin | | 1 stdout | | 2 stderr | |__________|...
16
votes
3answers
3k 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 ...
15
votes
3answers
46k views

How to list the open file descriptors (and the files they refer to) in my current bash session

I am running in an interactive bash session. I have created some file descriptors, using exec, and I would like to list what is the current status of my bash session. Is there a way to list the ...
14
votes
3answers
2k views

In the code “{ exec >/dev/null; } >/dev/null” what is happening under the hood?

When you redirect a command list that contains an exec redirection, the exec >/dev/null doesn't seem to still be applied afterwards, such as with: { exec >/dev/null; } >/dev/null; echo "Hi" "...
13
votes
1answer
17k views

What does exec 3<&1 do?

I understand that exec can do I/O redirection on the current shell, but I only sees usage like: exec 6<&0 # Link file descriptor #6 with stdin. # Saves stdin. exec 6>&1 ...
12
votes
2answers
2k views

Why doesn't SSH -t wait for background processes?

Why is it that ssh -t doesn't wait for background jobs to finish? Example: ssh user@example 'sleep 2 &' This works as expected, since ssh returns after 2 seconds, whereas ssh user@example -t '...
12
votes
2answers
27k views

How to find speed of wlan interface?

I'm trying to find speed of network interface using file-descriptor. It's easy to do it for ethX, just calling cat /sys/class/net/eth0/speed. Unfortunately this method doesn't work with wireless ...
12
votes
2answers
2k views

SSH: Provide additional “pipe” fds in addition to stdin, stdout, stderr

When connecting to a host with SSH, usually three "pipes" are provided between host and guest, for stdin, stdout, and stderr. Is there a command-line option to create forwards for additional file ...
11
votes
4answers
993 views

Program output redirection

When trying to redirect program output with the "some number greater than" syntax (eg foo 2> myfile), what are the possible numbers here and what do they represent? I believe 1 is /dev/stdout, 2 ...
11
votes
2answers
5k views

file descriptor vs. file name

I was wondering what differences and relations are between file descriptors and file names. Are they all used to access files? If yes, in the same way? For example, /dev/fd/0, /dev/stdin, and /proc/...
11
votes
2answers
420 views

What's the lifespan of a file descriptor?

As described here, redirections use open() to write to a file. There's an inner (?) file descriptor created in the shell, and then used when needed. Is the inner descriptor created for the whole ...
11
votes
1answer
2k views

Why doesn't the process substitution <() work with ssh -F

I have some vagrant virtual machines. To log into them I issue the vagrant ssh command. I want to log into them using regular ssh command. The vagrant ssh-config outputs the suitable config file $ ...
11
votes
2answers
719 views

What is the difference between &6 and /dev/fd/6?

To read from file descriptor 6 I can use <&6 or </dev/fd/6 (aka /proc/self/fd/6). Usually both work equally well. However if that file descriptor happens to be a socket, strange things ...
11
votes
2answers
8k views

stderr over ssh -t

This sends output to STDERR, but does not propagate Ctrl+C (i.e. Ctrl+C will kill ssh but not the remote sleep): $ ssh localhost 'sleep 100;echo foo ">&2"' This propagates Ctrl+C (i.e. Ctrl+C ...
11
votes
2answers
418 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 sometimes ...
10
votes
4answers
2k views

Temporary folder that automatically destroyed after process exit

Can we use temporary folders like temporary files TMP=$(mktemp ... ) exec 3<>$TMP rm $TMP cat <&3 which will be destroyed automatically after this shell exit?
10
votes
5answers
7k views

Testing if a file descriptor is valid

I'd like to make a bash script output additional information to file descriptors (FDs) greater than or equal to 3, when they are open. To test whether an FD is open, I devised the following trick: if ...
10
votes
3answers
5k views

How can a log program continue to log to a deleted file?

From the Unix Power Tools, 3rd Edition: Instead of Removing a File, Empty It section: If an active process has the file open (not uncommon for log files), removing the file and creating a new one ...
10
votes
3answers
2k views

Named pipes, file descriptors and EOF

Two windows, same user, with bash prompts. In window-1 type: $ mkfifo f; exec <f So bash is now attempting to read from file descriptor 0, which is mapped to named pipe f. In window-2 type: $ ...
10
votes
5answers
19k views

Close all file descriptors in bash

Is there a way to close all the open file descriptors, without having an explicit list of them beforehand?
10
votes
4answers
10k views

Why can't I `tail -f /proc/$pid/fd/1`?

I wrote a simple script which echo-es its PID: #/bin/bash while true; do echo $$; sleep 0.5; done I'm running said script (it says 3844 over and over) in one terminal and trying to tail the ...
10
votes
2answers
17k views

Execute command in remote active terminal

Suppose you have a terminal emulator (T1) open with a PID of 6350. From another terminal, type this command (C1): echo "ls\n" > /proc/6350/fd/0 This writes ls and the new line in T1 but does not ...
10
votes
1answer
899 views

OS X, bash: less works on open file descriptors, cat doesn't

In a bash script I'm working on (which has to run on Ubuntu and OS X), I need to redirect the output of hundreds of commands to a file. Rather than appending &>... to all of them, I simply do ...
9
votes
2answers
8k views

Writing to stdin of a process

As far as I understand if i type the following... python -i ... the python-interpreter will now read from stdin, behaving (obviously) like this: >>> print "Hello" Hello I would expect ...
9
votes
2answers
3k views

How to read user input when using script in pipe

General problem I want to write a script that interacts with the user even though it is in the middle of a chain of pipes. Concrete example Concretely, it takes a file or stdin, displays lines (...
9
votes
1answer
23k views

Fixing ulimit: open files: cannot modify limit: Operation not permitted

I tested this on different GNU/Linux installations: perl -e 'while(1){open($a{$b++}, "<" ,"/dev/null") or die $b;print " $b"}' System A and D The first limit I hit is 1024. It is easily raised ...
8
votes
2answers
14k views

File descriptor and fork

When a child is forked then it inherits parent's file descriptors, if child closes the file descriptor what will happen ? If child starts writing what shall happen to the file at the parent's end ? ...
8
votes
2answers
16k views

How to circumvent “Too many open files” in debian

If I want to see all relevant log files of my apache2 server at once, I use tail -f /var/kunden/logs/*log /var/kunden/logs/*log /var/log/apache2/*log |grep -v robots|grep -v favicon But since those ...
8
votes
1answer
145 views

Etymology of “descriptor” in “file descriptor”

The choice of the word descriptor has always struct me as odd. "Index" or "id" seem like more obvious alternatives. Is there a known rationale for the choosing the word "descriptor"? I guess "...
7
votes
3answers
10k views

Something's special about /dev/fd/3

I've been trying to learn about file descriptors. When I type "ls -l /dev/fd/" I get lrwx------ 1 me users 64 May 2 16:02 0 -> /dev/pts/5 l-wx------ 1 me users 64 May 2 16:02 1 -> /home/me/...
7
votes
2answers
2k views

Process substitution with input redirection

I am trying to understand input redirection in combination with process substituation. I am using bash 3 An example with tr is the following $ tr "o" "a" <(echo "Foo") tr: extra operand `/dev/fd/...
7
votes
2answers
3k views

Does OpenBSD have a limit to the number of file descriptors?

What's the maximum number of file descriptors that a process can get on OpenBSD successfully? For example, there is an openfiles limit in login.conf(5) in OpenBSD. If I want to have as many file ...
7
votes
1answer
3k views

Flock doesn't seem to be working

I've recently been trying to create a shell script for a minor project, and for some reason, the flock command isn't working for me properly. Whenever I invoke it in a subshell the atomic way and put ...
7
votes
1answer
1k views

What am I supposed to use `O_PATH` for, and how?

I use a Linux 4.x-based distribution, and I've recently noticed the kernel's open() system call supports an O_PATH open flag. While the man page for it does have a list of system calls it could ...
7
votes
1answer
593 views

How can I send reads and writes on a single file descriptor to different places?

I have a Linux executable foo that reads input from fd 0 and writes output to fd 0 (not fd 1). This works just fine for interactive use in the terminal. From the shell command line, how can I execute ...