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

Clash 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?
files io-redirection cat output input
add a comment |Â
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?
files io-redirection cat output input
poor catÂ
â John Militer
Apr 12 at 2:44
add a comment |Â
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?
files io-redirection cat output input
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?
files io-redirection cat output input
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
add a comment |Â
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
add a comment |Â
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:
- the output (fd 0) is to a regular file; and
- the device (
st_devfield) and inode (st_inofield) are the same for both the output fd and the input file under examination; and - 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.
What implementation ofcatis able to give these warnings? The redirection is not part of the command line, socatwould have no idea where its output goes. Is itbash's "noclobber" message that this refers to maybe?
â Kusalananda
Apr 12 at 6:51
@Kusalananda That's whatfstatis for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNUcat, for example.
â Michael Homer
Apr 12 at 6:57
Would you have to use some special flags withcatfor this to take effect? The GNUcatthat 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 fileworks 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
 |Â
show 1 more comment
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:
- the output (fd 0) is to a regular file; and
- the device (
st_devfield) and inode (st_inofield) are the same for both the output fd and the input file under examination; and - 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.
What implementation ofcatis able to give these warnings? The redirection is not part of the command line, socatwould have no idea where its output goes. Is itbash's "noclobber" message that this refers to maybe?
â Kusalananda
Apr 12 at 6:51
@Kusalananda That's whatfstatis for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNUcat, for example.
â Michael Homer
Apr 12 at 6:57
Would you have to use some special flags withcatfor this to take effect? The GNUcatthat 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 fileworks 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
 |Â
show 1 more comment
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:
- the output (fd 0) is to a regular file; and
- the device (
st_devfield) and inode (st_inofield) are the same for both the output fd and the input file under examination; and - 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.
What implementation ofcatis able to give these warnings? The redirection is not part of the command line, socatwould have no idea where its output goes. Is itbash's "noclobber" message that this refers to maybe?
â Kusalananda
Apr 12 at 6:51
@Kusalananda That's whatfstatis for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNUcat, for example.
â Michael Homer
Apr 12 at 6:57
Would you have to use some special flags withcatfor this to take effect? The GNUcatthat 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 fileworks 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
 |Â
show 1 more comment
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:
- the output (fd 0) is to a regular file; and
- the device (
st_devfield) and inode (st_inofield) are the same for both the output fd and the input file under examination; and - 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.
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:
- the output (fd 0) is to a regular file; and
- the device (
st_devfield) and inode (st_inofield) are the same for both the output fd and the input file under examination; and - 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.
edited Apr 12 at 7:13
answered Apr 12 at 2:22
Michael Homer
42.4k6108148
42.4k6108148
What implementation ofcatis able to give these warnings? The redirection is not part of the command line, socatwould have no idea where its output goes. Is itbash's "noclobber" message that this refers to maybe?
â Kusalananda
Apr 12 at 6:51
@Kusalananda That's whatfstatis for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNUcat, for example.
â Michael Homer
Apr 12 at 6:57
Would you have to use some special flags withcatfor this to take effect? The GNUcatthat 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 fileworks 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
 |Â
show 1 more comment
What implementation ofcatis able to give these warnings? The redirection is not part of the command line, socatwould have no idea where its output goes. Is itbash's "noclobber" message that this refers to maybe?
â Kusalananda
Apr 12 at 6:51
@Kusalananda That's whatfstatis for. You just have to check if the device & inode are the same as one of the inputs. Here it is in GNUcat, for example.
â Michael Homer
Apr 12 at 6:57
Would you have to use some special flags withcatfor this to take effect? The GNUcatthat 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 fileworks 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
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
poor catÂ
â John Militer
Apr 12 at 2:44