5 ms·
Don't cat a file and pipe it into pv. Use "pv file" as a replacement for "cat file" and it will show you the progress as a percentage. When it's in the middle o
by electrum 10y ago
Don't cat a file and pipe it into pv. Use "pv file" as a replacement for "cat file" and it will show you the progress as a percentage. When it's in the middle of a pipeline, it doesn't know the total size (unless you tell it with -s), so it can only show the throughput.
- apostacy 10y agoActually, that's not completley true. pv will detect the file size if you use the shell to read a file into it, like pv < file.
- angry_octet 10y agoAt first I thought, no, that's not possible. Then I thought, no, they wouldn't do THAT would they? But I guess they do... http://stackoverflow.com/questions/1734243/in-c-how-do-i-print-filename-of-file-that-is-redirected-as-input-in-shell http://stackoverflow.com/questions/1734243/in-c-how-do-i-pri... I've seen that kind of brokenness from programs trying to find their binary image on disk. Don't do it, it's bad.
- kam 10y agoIt doesn't need to hunt for the directory entry, just needs to call `fstat()` on the stdin file descriptor.
- angry_octet 10y agoOops you're right, has size in the inode.
- rwmj 10y agoUnless the input file is a device, and then you need to call: ioctl(STDIN_FILENO, BLKGETSIZE64, &size)
- ithkuil 10y agoBut I guess this is orthogonal to where the file descriptor comes from (i.e. stdin or opening a file whose name is passed in the args)
- rwmj 10y agoIt's an orthogonal issue, yes, but calling stat or fstat on any block device whether from stdin or argv will return .st_size == 0, so your progress bar won't display the correct answers (or could display better answers if it used the ioctl).
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- adrianratnapala 10y agoIf all they want is the size of the file, then what is wrong with using fstat? It's not the same thing as trying to walk the FS to look for the filename is silly.
- gaius 10y agoIt makes a decent interview question tho', "explain the difference between cat file|./prog and ./prog <file". It doesn't even matter if they get it wrong, that they even know there is a difference is a very good sign.
- rfw 10y agoI think most people wouldn't know the difference (I had no idea!) and the knowledge might fall into the realm of obscure trivia.
- gaius 10y agoPeople who have come into SA work via being C programmers usually figure it out, they make the best SAs because they are mentally equipped to reason about a system from first principles.
- therein 10y agopv file.bin | dd of=/dev/sdb is nice too. Honestly while the OP sparks a nice conversation, it is actually complaining about the use of dd out of lack of knowledge.