Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am looking for a way to send the resulting file from one comamnd to another, So I dont have to save a copy on disk inbetween. For example, if I have rared files on disk and want to open them I would like to be able to run something like:

unrar x file.rar | kplayer

unrar x file.rar | gpg -c

gpg file.gpg | kplayer 

I am sure you see what I am looking for. I have tried to get unrar to send to standard output with -p but it will just send it to screen even when I think it should be connected with a | to kplayer. I suppose that is because kplayer can't get it's data from stdin.

But is there a way to pass files without saving them on disk? Is the best I can get to create a filesystem in memory and use that, I don't like the idea to reserve that much memory though.

share|improve this question
    
Even if kplayer doesn't read from stdin by default (haven't used it, don't know it), you can still use a fifo file as an argument (see mkfifo) as a workaround. But it will still not work if the media format does not support streaming. What exactly are you trying to feed kplayer? –  forcefsck Oct 16 '11 at 20:53

4 Answers 4

A lot of commands have an option to write to STDOUT. Unrar, for example, has the p option. A lot of commands then have the - option that can be used in cojunction to read from STDIN. So the first example you gave could be rewritten like this:

unrar px file.rar | kplayer -

GPG already works with piped input by default:

unrar px file.rar | gpg -c

Check a command's help and/or man/info page if sending the results to STDOUT is supported (many commands do this by default).

share|improve this answer
    
You have to be careful when using - for stdin and might need to prefix it with -- to inform the command that it is a file argument and not an option. –  Richm Oct 15 '11 at 17:17
1  
Another option instead of using - is to use /dev/stdin as a file or /dev/fd/0 . Both are available on linux but I do not thing that it will make kplayer work because it likely needs a seekable file and not a stream/pipe. –  Richm Oct 15 '11 at 17:18
    
@Richm Most programs recognize - as an operand, not an option. Both POSIX and GNU mandate that - be an operand. –  Gilles Oct 15 '11 at 23:00
    
Note that p and x are both commands and unrar accepts only one command. Using both as shown in the answer results a syntax error. As unrar writes its error messages to STDOUT instead of STDIN (probably a DOS legacy), it will be sent to kplayer instead of appearing in the terminal. –  manatwork Oct 16 '11 at 10:17
    
This is the key - a program has to support writing its output to stdout [or, in a pinch, to an arbitrary filename, for use with >(process substitution)], rather than creating a file. (in principle, the other program has to support picking up its input from either stdin or an arbitrary filename, but this is much more common) –  Random832 Oct 17 '11 at 14:42

If the command merely requires a file name, specify /dev/stdin or /dev/fd/0. These are special files; opening them actually duplicates standard input. Some commands accept - as an alias for standard input, too.

unrar p foo.rar somefile | somecommand -
unrar p foo.rar somefile | somecommand /dev/stdin

Occasionally a program imposes constraints on the file name, typically requiring a specific extension. You might be able to fool it with a symbolic link.

ln -s /dev/stdin myfile.ext
unrar p foo.rar somefile | somecommand myfile.ext

This won't always work, because some programs can't cope with piped input: they require seekable files, i.e., file where they can go back and forth at will. In that case, you need to make a temporary file.

In some cases, you can arrange for your data to appear as a regular file rather than as the output of a command. FUSE, in particular, can make all sorts of things appear as regular files. For example, with AVFS, archive members appear as files.

mountavfs
cd "~/.avfs$PWD/foo.rar\#"
somecommand somefile
share|improve this answer

If you're in a shell that supports process substitution, that might help with programs that can't take data from stdin.

You would do kplayer <(unrar p file.rar)

If you are quite desperate, you can use a ramdisk, and actually create files, but they won't be on disk.

share|improve this answer
1  
This is equivalent to using /dev/stdin: the data will still be coming from a pipe. The <(…) construct only becomes useful when you want to get input from several pipes (they can't all be stdin). –  Gilles Oct 15 '11 at 23:02
    
@Gilles It's not the same; data does not come from stdin. The <(…) is converted a filename, and that filename is passed to the program as an argument. So if the program does not read from standard in, but does take filename arguments, this is useful. –  Shawn J. Goff Oct 16 '11 at 16:41
2  
But the file name is /dev/fd/NUMBER or something like it, and the file is a pipe. –  Gilles Oct 16 '11 at 18:01
    
Exactly; it's a pipe, but not stdin, which is (I think) what the author was looking for. –  Shawn J. Goff Oct 16 '11 at 22:24

And thanks all for the input, i suppose there isn't any easy way to get what i want ;(

The following command should do and use the /dev/shm, So i need to build a nifty script :)

cp file.gpg /dev/shm && gpg -c file.gpg && rm file.gpg && kplayer file && rm file
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.