0

I have such command.

#echo -e "ls\npwd" >test
#bash
#exec <test

After this code.You will find

#ls /dev/fd

0 1 2 3

#cd /dev/fd;ls

0 1 2 255

Fun,the directory /dev/fd have difference content..What's happen?

1 Answer 1

1

/dev/fd is not a fixed directory, but a symlink that points to a directory on a per-process basis. In your two invocations of ls, you are listing the contents of two different "directories".

Your first output gives the file descriptors associated with ls. The second output gives the file descriptors associated with bash. The echo and exec preceding the directory listings do not change this.

Consider:

foo:~$ ls -ln /dev/fd/
total 0
lrwx------ 1 1000 1000 64 Jul 10 12:13 0 -> /dev/pts/19
lrwx------ 1 1000 1000 64 Jul 10 12:13 1 -> /dev/pts/19
lrwx------ 1 1000 1000 64 Jul 10 12:13 2 -> /dev/pts/19
lr-x------ 1 1000 1000 64 Jul 10 12:13 3 -> /proc/20115/fd
foo:~$ cat /dev/fd/../cmdline && echo
cat/dev/fd/../cmdline

foo:~$ cd /dev/fd ; ls -ln
total 0
lrwx------ 1 1000 1000 64 Jul  8 05:01 0 -> /dev/pts/19
lrwx------ 1 1000 1000 64 Jul  8 05:01 1 -> /dev/pts/19
lrwx------ 1 1000 1000 64 Jul  8 05:01 2 -> /dev/pts/19
lrwx------ 1 1000 1000 64 Jul  8 05:01 255 -> /dev/pts/19
foo:/dev/fd$ cat ../cmdline && echo
bash
foo:/dev/fd$ pwd -P
/proc/2689/fd
2
  • I'm sorry,I need some time to go through. Commented Jul 11, 2017 at 3:55
  • @yode keep in mind that the /proc, /dev, and /sys filesystems are not disk files, but interfaces to kernel and OS data via a file-like interface, for a gross simplification. Commented Jul 11, 2017 at 10:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.