Why can't an input file be an output file?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












Specifically in the case of cat x y > y and cat x >> x where x and y are files.



What would happen if I didn't get a "input file is output file" error? Basically, why am I not allowed to run those commands?







share|improve this question






















  • poor cat­
    – John Militer
    Apr 12 at 2:44














up vote
2
down vote

favorite












Specifically in the case of cat x y > y and cat x >> x where x and y are files.



What would happen if I didn't get a "input file is output file" error? Basically, why am I not allowed to run those commands?







share|improve this question






















  • poor cat­
    – John Militer
    Apr 12 at 2:44












up vote
2
down vote

favorite









up vote
2
down vote

favorite











Specifically in the case of cat x y > y and cat x >> x where x and y are files.



What would happen if I didn't get a "input file is output file" error? Basically, why am I not allowed to run those commands?







share|improve this question














Specifically in the case of cat x y > y and cat x >> x where x and y are files.



What would happen if I didn't get a "input file is output file" error? Basically, why am I not allowed to run those commands?









share|improve this question













share|improve this question




share|improve this question








edited Apr 12 at 9:57









Jeff Schaller

31.1k846105




31.1k846105










asked Apr 12 at 2:13









Rabbit

111




111











  • poor cat­
    – John Militer
    Apr 12 at 2:44
















  • poor cat­
    – John Militer
    Apr 12 at 2:44















poor cat­
– John Militer
Apr 12 at 2:44




poor cat­
– John Militer
Apr 12 at 2:44










1 Answer
1






active

oldest

votes

















up vote
2
down vote













cat x y > y would be the same as cp x y (because y would have been truncated by the time cat started) followed by cat y >> y.



cat x >> x would be an infinite loop until your disk was full or you hit the filesystem's file-size limit (because it keeps adding to the end of the file and then reading it back in to add again).



Note that this "error" doesn't mean that cat didn't do anything - it's just a notice that this particular file was skipped (in GNU cat, at least). You're "allowed" to run it, it just probably didn't do what you wanted and it's letting you know.



The test in GNU cat for generating that message is that:



  1. the output (fd 0) is to a regular file; and

  2. the device (st_dev field) and inode (st_ino field) are the same for both the output fd and the input file under examination; and

  3. the offset in the input file is less than the size of the file (so it's not empty; I can't think of a case where the offset wouldn't be zero at this point, but presumably there is one).

If all those apply, it prints the diagnostic and moves on to the next input file.



Some versions of the cat command don't make this check and you can make these things happen. I can imagine a system where they do make the check and it's not able to tell they're the same file as well, though I couldn't off-hand tell you of one. In either case, it probably ends badly. It's possible that, rather than a loop, you just end up with the file so far doubled, if the cat implementation only makes a single write of the entire memory-mapped file, but that's just a lesser kind of badly.






share|improve this answer






















  • What implementation of cat is able to give these warnings? The redirection is not part of the command line, so cat would have no idea where its output goes. Is it bash's "noclobber" message that this refers to maybe?
    – Kusalananda
    Apr 12 at 6:51










  • @Kusalananda That's what fstat is for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNU cat, for example.
    – Michael Homer
    Apr 12 at 6:57










  • Would you have to use some special flags with cat for this to take effect? The GNU cat that I have installed does not behave like this...
    – Kusalananda
    Apr 12 at 6:59






  • 2




    cd /tmp ; echo 1 > a ; echo 2 > b ; cat a b > b -> cat: b: input file is output file works for me. The code's been there forever too.
    – Michael Homer
    Apr 12 at 7:03










  • Ah, ok, I see now. I was testing on empty files.
    – Kusalananda
    Apr 12 at 7:05










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f437179%2fwhy-cant-an-input-file-be-an-output-file%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













cat x y > y would be the same as cp x y (because y would have been truncated by the time cat started) followed by cat y >> y.



cat x >> x would be an infinite loop until your disk was full or you hit the filesystem's file-size limit (because it keeps adding to the end of the file and then reading it back in to add again).



Note that this "error" doesn't mean that cat didn't do anything - it's just a notice that this particular file was skipped (in GNU cat, at least). You're "allowed" to run it, it just probably didn't do what you wanted and it's letting you know.



The test in GNU cat for generating that message is that:



  1. the output (fd 0) is to a regular file; and

  2. the device (st_dev field) and inode (st_ino field) are the same for both the output fd and the input file under examination; and

  3. the offset in the input file is less than the size of the file (so it's not empty; I can't think of a case where the offset wouldn't be zero at this point, but presumably there is one).

If all those apply, it prints the diagnostic and moves on to the next input file.



Some versions of the cat command don't make this check and you can make these things happen. I can imagine a system where they do make the check and it's not able to tell they're the same file as well, though I couldn't off-hand tell you of one. In either case, it probably ends badly. It's possible that, rather than a loop, you just end up with the file so far doubled, if the cat implementation only makes a single write of the entire memory-mapped file, but that's just a lesser kind of badly.






share|improve this answer






















  • What implementation of cat is able to give these warnings? The redirection is not part of the command line, so cat would have no idea where its output goes. Is it bash's "noclobber" message that this refers to maybe?
    – Kusalananda
    Apr 12 at 6:51










  • @Kusalananda That's what fstat is for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNU cat, for example.
    – Michael Homer
    Apr 12 at 6:57










  • Would you have to use some special flags with cat for this to take effect? The GNU cat that I have installed does not behave like this...
    – Kusalananda
    Apr 12 at 6:59






  • 2




    cd /tmp ; echo 1 > a ; echo 2 > b ; cat a b > b -> cat: b: input file is output file works for me. The code's been there forever too.
    – Michael Homer
    Apr 12 at 7:03










  • Ah, ok, I see now. I was testing on empty files.
    – Kusalananda
    Apr 12 at 7:05














up vote
2
down vote













cat x y > y would be the same as cp x y (because y would have been truncated by the time cat started) followed by cat y >> y.



cat x >> x would be an infinite loop until your disk was full or you hit the filesystem's file-size limit (because it keeps adding to the end of the file and then reading it back in to add again).



Note that this "error" doesn't mean that cat didn't do anything - it's just a notice that this particular file was skipped (in GNU cat, at least). You're "allowed" to run it, it just probably didn't do what you wanted and it's letting you know.



The test in GNU cat for generating that message is that:



  1. the output (fd 0) is to a regular file; and

  2. the device (st_dev field) and inode (st_ino field) are the same for both the output fd and the input file under examination; and

  3. the offset in the input file is less than the size of the file (so it's not empty; I can't think of a case where the offset wouldn't be zero at this point, but presumably there is one).

If all those apply, it prints the diagnostic and moves on to the next input file.



Some versions of the cat command don't make this check and you can make these things happen. I can imagine a system where they do make the check and it's not able to tell they're the same file as well, though I couldn't off-hand tell you of one. In either case, it probably ends badly. It's possible that, rather than a loop, you just end up with the file so far doubled, if the cat implementation only makes a single write of the entire memory-mapped file, but that's just a lesser kind of badly.






share|improve this answer






















  • What implementation of cat is able to give these warnings? The redirection is not part of the command line, so cat would have no idea where its output goes. Is it bash's "noclobber" message that this refers to maybe?
    – Kusalananda
    Apr 12 at 6:51










  • @Kusalananda That's what fstat is for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNU cat, for example.
    – Michael Homer
    Apr 12 at 6:57










  • Would you have to use some special flags with cat for this to take effect? The GNU cat that I have installed does not behave like this...
    – Kusalananda
    Apr 12 at 6:59






  • 2




    cd /tmp ; echo 1 > a ; echo 2 > b ; cat a b > b -> cat: b: input file is output file works for me. The code's been there forever too.
    – Michael Homer
    Apr 12 at 7:03










  • Ah, ok, I see now. I was testing on empty files.
    – Kusalananda
    Apr 12 at 7:05












up vote
2
down vote










up vote
2
down vote









cat x y > y would be the same as cp x y (because y would have been truncated by the time cat started) followed by cat y >> y.



cat x >> x would be an infinite loop until your disk was full or you hit the filesystem's file-size limit (because it keeps adding to the end of the file and then reading it back in to add again).



Note that this "error" doesn't mean that cat didn't do anything - it's just a notice that this particular file was skipped (in GNU cat, at least). You're "allowed" to run it, it just probably didn't do what you wanted and it's letting you know.



The test in GNU cat for generating that message is that:



  1. the output (fd 0) is to a regular file; and

  2. the device (st_dev field) and inode (st_ino field) are the same for both the output fd and the input file under examination; and

  3. the offset in the input file is less than the size of the file (so it's not empty; I can't think of a case where the offset wouldn't be zero at this point, but presumably there is one).

If all those apply, it prints the diagnostic and moves on to the next input file.



Some versions of the cat command don't make this check and you can make these things happen. I can imagine a system where they do make the check and it's not able to tell they're the same file as well, though I couldn't off-hand tell you of one. In either case, it probably ends badly. It's possible that, rather than a loop, you just end up with the file so far doubled, if the cat implementation only makes a single write of the entire memory-mapped file, but that's just a lesser kind of badly.






share|improve this answer














cat x y > y would be the same as cp x y (because y would have been truncated by the time cat started) followed by cat y >> y.



cat x >> x would be an infinite loop until your disk was full or you hit the filesystem's file-size limit (because it keeps adding to the end of the file and then reading it back in to add again).



Note that this "error" doesn't mean that cat didn't do anything - it's just a notice that this particular file was skipped (in GNU cat, at least). You're "allowed" to run it, it just probably didn't do what you wanted and it's letting you know.



The test in GNU cat for generating that message is that:



  1. the output (fd 0) is to a regular file; and

  2. the device (st_dev field) and inode (st_ino field) are the same for both the output fd and the input file under examination; and

  3. the offset in the input file is less than the size of the file (so it's not empty; I can't think of a case where the offset wouldn't be zero at this point, but presumably there is one).

If all those apply, it prints the diagnostic and moves on to the next input file.



Some versions of the cat command don't make this check and you can make these things happen. I can imagine a system where they do make the check and it's not able to tell they're the same file as well, though I couldn't off-hand tell you of one. In either case, it probably ends badly. It's possible that, rather than a loop, you just end up with the file so far doubled, if the cat implementation only makes a single write of the entire memory-mapped file, but that's just a lesser kind of badly.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 12 at 7:13

























answered Apr 12 at 2:22









Michael Homer

42.4k6108148




42.4k6108148











  • What implementation of cat is able to give these warnings? The redirection is not part of the command line, so cat would have no idea where its output goes. Is it bash's "noclobber" message that this refers to maybe?
    – Kusalananda
    Apr 12 at 6:51










  • @Kusalananda That's what fstat is for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNU cat, for example.
    – Michael Homer
    Apr 12 at 6:57










  • Would you have to use some special flags with cat for this to take effect? The GNU cat that I have installed does not behave like this...
    – Kusalananda
    Apr 12 at 6:59






  • 2




    cd /tmp ; echo 1 > a ; echo 2 > b ; cat a b > b -> cat: b: input file is output file works for me. The code's been there forever too.
    – Michael Homer
    Apr 12 at 7:03










  • Ah, ok, I see now. I was testing on empty files.
    – Kusalananda
    Apr 12 at 7:05
















  • What implementation of cat is able to give these warnings? The redirection is not part of the command line, so cat would have no idea where its output goes. Is it bash's "noclobber" message that this refers to maybe?
    – Kusalananda
    Apr 12 at 6:51










  • @Kusalananda That's what fstat is for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNU cat, for example.
    – Michael Homer
    Apr 12 at 6:57










  • Would you have to use some special flags with cat for this to take effect? The GNU cat that I have installed does not behave like this...
    – Kusalananda
    Apr 12 at 6:59






  • 2




    cd /tmp ; echo 1 > a ; echo 2 > b ; cat a b > b -> cat: b: input file is output file works for me. The code's been there forever too.
    – Michael Homer
    Apr 12 at 7:03










  • Ah, ok, I see now. I was testing on empty files.
    – Kusalananda
    Apr 12 at 7:05















What implementation of cat is able to give these warnings? The redirection is not part of the command line, so cat would have no idea where its output goes. Is it bash's "noclobber" message that this refers to maybe?
– Kusalananda
Apr 12 at 6:51




What implementation of cat is able to give these warnings? The redirection is not part of the command line, so cat would have no idea where its output goes. Is it bash's "noclobber" message that this refers to maybe?
– Kusalananda
Apr 12 at 6:51












@Kusalananda That's what fstat is for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNU cat, for example.
– Michael Homer
Apr 12 at 6:57




@Kusalananda That's what fstat is for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNU cat, for example.
– Michael Homer
Apr 12 at 6:57












Would you have to use some special flags with cat for this to take effect? The GNU cat that I have installed does not behave like this...
– Kusalananda
Apr 12 at 6:59




Would you have to use some special flags with cat for this to take effect? The GNU cat that I have installed does not behave like this...
– Kusalananda
Apr 12 at 6:59




2




2




cd /tmp ; echo 1 > a ; echo 2 > b ; cat a b > b -> cat: b: input file is output file works for me. The code's been there forever too.
– Michael Homer
Apr 12 at 7:03




cd /tmp ; echo 1 > a ; echo 2 > b ; cat a b > b -> cat: b: input file is output file works for me. The code's been there forever too.
– Michael Homer
Apr 12 at 7:03












Ah, ok, I see now. I was testing on empty files.
– Kusalananda
Apr 12 at 7:05




Ah, ok, I see now. I was testing on empty files.
– Kusalananda
Apr 12 at 7:05












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f437179%2fwhy-cant-an-input-file-be-an-output-file%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)