Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A really handy one a colleague showed me yesterday was the /dev/fd/0|1|2 files, which are stdin/out/err respectively. Means you can use that file for utils that expect a file only.

E.g echo "This would be contents of file" | someCommand /dev/fd/0



These are also available as /dev/std{in,out,err}; the names of which are a little more self documenting :)

    $ ls -l /dev/fd /dev/std* | column -t
    lrwxrwxrwx  1  root  root  13  Jun  27  16:32  /dev/fd      ->  /proc/self/fd
    lrwxrwxrwx  1  root  root  15  Jun  27  16:32  /dev/stderr  ->  /proc/self/fd/2
    lrwxrwxrwx  1  root  root  15  Jun  27  16:32  /dev/stdin   ->  /proc/self/fd/0
    lrwxrwxrwx  1  root  root  15  Jun  27  16:32  /dev/stdout  ->  /proc/self/fd/1
You can even access the file descriptors of other processes with /proc/${pid}/fd/; which is handy for (say) un-deleting a file that a process still has open.

Another handy one is "process substitution" which lets you do the same thing with multiple streams by creating new file descriptors, and automatically turning them into /dev/fd/* paths:

    someCommand <(echo "contents of file 1") <(echo "contents of file 2")
which is somewhat explained by:

    $ echo someCommand <(echo "contents of file 1") <(echo "contents of file 2")
    someCommand /dev/fd/63 /dev/fd/62


"process substitution" is one of the new useful things I learned this year (switched to bash from csh over 20 years ago).

/proc/${pid}/fd/ is very useful for forensic purposes when you find some malware running that deleted itself and/or config files but still has a handle open to them.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: