Overwrite file preserving target permissions without shell invocation

Multi tool use
Multi tool use

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











up vote
1
down vote

favorite
1












I have this situation



$ ls -la
-rw-r--r-- 1 user user 123 Mar 5 19:32 file-a
-rwx---rwx 1 user user 987 Mar 5 19:32 file-b


I would like to overwrite file-b with file-a but I would like to preserver all permissions and ownership of file-b.



This does not work, because it uses permissions of file-a



cp file-a file-b # << edit: this works as expected! My fault!
mv file-a file-b


This works, but it can be called only from shell. Imagine the situation I can call only execve or similar function.



cat file-a > file-b


I know, that I can execute something like



sh -c "cat file-a > file-b"


but this introduce difficulties with escaping filenames so I don't want to use this.



Is there some common command that can do this or should I write my own helper c program pro this task?







share|improve this question


















  • 1




    When I do cp file-a file-b, the permissions and ownership of file-b do not change. Are you sure you aren’t running cp -p through an alias?
    – G-Man
    Mar 5 at 19:32










  • You are right! I don't know how I can miss this.
    – j123b567
    Mar 6 at 16:15















up vote
1
down vote

favorite
1












I have this situation



$ ls -la
-rw-r--r-- 1 user user 123 Mar 5 19:32 file-a
-rwx---rwx 1 user user 987 Mar 5 19:32 file-b


I would like to overwrite file-b with file-a but I would like to preserver all permissions and ownership of file-b.



This does not work, because it uses permissions of file-a



cp file-a file-b # << edit: this works as expected! My fault!
mv file-a file-b


This works, but it can be called only from shell. Imagine the situation I can call only execve or similar function.



cat file-a > file-b


I know, that I can execute something like



sh -c "cat file-a > file-b"


but this introduce difficulties with escaping filenames so I don't want to use this.



Is there some common command that can do this or should I write my own helper c program pro this task?







share|improve this question


















  • 1




    When I do cp file-a file-b, the permissions and ownership of file-b do not change. Are you sure you aren’t running cp -p through an alias?
    – G-Man
    Mar 5 at 19:32










  • You are right! I don't know how I can miss this.
    – j123b567
    Mar 6 at 16:15













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have this situation



$ ls -la
-rw-r--r-- 1 user user 123 Mar 5 19:32 file-a
-rwx---rwx 1 user user 987 Mar 5 19:32 file-b


I would like to overwrite file-b with file-a but I would like to preserver all permissions and ownership of file-b.



This does not work, because it uses permissions of file-a



cp file-a file-b # << edit: this works as expected! My fault!
mv file-a file-b


This works, but it can be called only from shell. Imagine the situation I can call only execve or similar function.



cat file-a > file-b


I know, that I can execute something like



sh -c "cat file-a > file-b"


but this introduce difficulties with escaping filenames so I don't want to use this.



Is there some common command that can do this or should I write my own helper c program pro this task?







share|improve this question














I have this situation



$ ls -la
-rw-r--r-- 1 user user 123 Mar 5 19:32 file-a
-rwx---rwx 1 user user 987 Mar 5 19:32 file-b


I would like to overwrite file-b with file-a but I would like to preserver all permissions and ownership of file-b.



This does not work, because it uses permissions of file-a



cp file-a file-b # << edit: this works as expected! My fault!
mv file-a file-b


This works, but it can be called only from shell. Imagine the situation I can call only execve or similar function.



cat file-a > file-b


I know, that I can execute something like



sh -c "cat file-a > file-b"


but this introduce difficulties with escaping filenames so I don't want to use this.



Is there some common command that can do this or should I write my own helper c program pro this task?









share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 16:16

























asked Mar 5 at 18:50









j123b567

255128




255128







  • 1




    When I do cp file-a file-b, the permissions and ownership of file-b do not change. Are you sure you aren’t running cp -p through an alias?
    – G-Man
    Mar 5 at 19:32










  • You are right! I don't know how I can miss this.
    – j123b567
    Mar 6 at 16:15













  • 1




    When I do cp file-a file-b, the permissions and ownership of file-b do not change. Are you sure you aren’t running cp -p through an alias?
    – G-Man
    Mar 5 at 19:32










  • You are right! I don't know how I can miss this.
    – j123b567
    Mar 6 at 16:15








1




1




When I do cp file-a file-b, the permissions and ownership of file-b do not change. Are you sure you aren’t running cp -p through an alias?
– G-Man
Mar 5 at 19:32




When I do cp file-a file-b, the permissions and ownership of file-b do not change. Are you sure you aren’t running cp -p through an alias?
– G-Man
Mar 5 at 19:32












You are right! I don't know how I can miss this.
– j123b567
Mar 6 at 16:15





You are right! I don't know how I can miss this.
– j123b567
Mar 6 at 16:15











2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










A simple command to copy a file without copying the mode is



dd if=file-a of=file-b


but then you get dd’s verbose status message written to stderr. 
You can suppress that
by running the command in a shell and adding 2> /dev/null,
but then you’re back to square 1. 
If you have GNU dd, you can do



dd if=file-a of=file-b status=none





share|improve this answer




















  • This is also useful. I also forget this trivial solution. Thank you!
    – j123b567
    Mar 6 at 16:19

















up vote
2
down vote













rsync --inplace file-a file-b should do what you want.



https://linux.die.net/man/1/rsync




--inplace
This option changes how rsync transfers a file when the file's data needs to be updated: instead of the default method of creating a new copy of the file and moving it into place when it is complete, rsync instead writes the updated data directly to the destination file.





share|improve this answer




















  • It seems slightly odd to call upon something like rsync from a C program (or whatever he's writing) just to preserve permissions when copying a file...
    – Kusalananda
    Mar 5 at 19:11











  • Agreed, though he seems to be looking for an option that is either shell executable or easy to call from code (without indirection like cat file-a > file-b).
    – Johnride
    Mar 5 at 19:18










  • Thank you for the alternative. It is not a "core" command but it is also nice to know.
    – j123b567
    Mar 6 at 16:20











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%2f428346%2foverwrite-file-preserving-target-permissions-without-shell-invocation%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










A simple command to copy a file without copying the mode is



dd if=file-a of=file-b


but then you get dd’s verbose status message written to stderr. 
You can suppress that
by running the command in a shell and adding 2> /dev/null,
but then you’re back to square 1. 
If you have GNU dd, you can do



dd if=file-a of=file-b status=none





share|improve this answer




















  • This is also useful. I also forget this trivial solution. Thank you!
    – j123b567
    Mar 6 at 16:19














up vote
1
down vote



accepted










A simple command to copy a file without copying the mode is



dd if=file-a of=file-b


but then you get dd’s verbose status message written to stderr. 
You can suppress that
by running the command in a shell and adding 2> /dev/null,
but then you’re back to square 1. 
If you have GNU dd, you can do



dd if=file-a of=file-b status=none





share|improve this answer




















  • This is also useful. I also forget this trivial solution. Thank you!
    – j123b567
    Mar 6 at 16:19












up vote
1
down vote



accepted







up vote
1
down vote



accepted






A simple command to copy a file without copying the mode is



dd if=file-a of=file-b


but then you get dd’s verbose status message written to stderr. 
You can suppress that
by running the command in a shell and adding 2> /dev/null,
but then you’re back to square 1. 
If you have GNU dd, you can do



dd if=file-a of=file-b status=none





share|improve this answer












A simple command to copy a file without copying the mode is



dd if=file-a of=file-b


but then you get dd’s verbose status message written to stderr. 
You can suppress that
by running the command in a shell and adding 2> /dev/null,
but then you’re back to square 1. 
If you have GNU dd, you can do



dd if=file-a of=file-b status=none






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 5 at 19:24









G-Man

11.5k82656




11.5k82656











  • This is also useful. I also forget this trivial solution. Thank you!
    – j123b567
    Mar 6 at 16:19
















  • This is also useful. I also forget this trivial solution. Thank you!
    – j123b567
    Mar 6 at 16:19















This is also useful. I also forget this trivial solution. Thank you!
– j123b567
Mar 6 at 16:19




This is also useful. I also forget this trivial solution. Thank you!
– j123b567
Mar 6 at 16:19












up vote
2
down vote













rsync --inplace file-a file-b should do what you want.



https://linux.die.net/man/1/rsync




--inplace
This option changes how rsync transfers a file when the file's data needs to be updated: instead of the default method of creating a new copy of the file and moving it into place when it is complete, rsync instead writes the updated data directly to the destination file.





share|improve this answer




















  • It seems slightly odd to call upon something like rsync from a C program (or whatever he's writing) just to preserve permissions when copying a file...
    – Kusalananda
    Mar 5 at 19:11











  • Agreed, though he seems to be looking for an option that is either shell executable or easy to call from code (without indirection like cat file-a > file-b).
    – Johnride
    Mar 5 at 19:18










  • Thank you for the alternative. It is not a "core" command but it is also nice to know.
    – j123b567
    Mar 6 at 16:20















up vote
2
down vote













rsync --inplace file-a file-b should do what you want.



https://linux.die.net/man/1/rsync




--inplace
This option changes how rsync transfers a file when the file's data needs to be updated: instead of the default method of creating a new copy of the file and moving it into place when it is complete, rsync instead writes the updated data directly to the destination file.





share|improve this answer




















  • It seems slightly odd to call upon something like rsync from a C program (or whatever he's writing) just to preserve permissions when copying a file...
    – Kusalananda
    Mar 5 at 19:11











  • Agreed, though he seems to be looking for an option that is either shell executable or easy to call from code (without indirection like cat file-a > file-b).
    – Johnride
    Mar 5 at 19:18










  • Thank you for the alternative. It is not a "core" command but it is also nice to know.
    – j123b567
    Mar 6 at 16:20













up vote
2
down vote










up vote
2
down vote









rsync --inplace file-a file-b should do what you want.



https://linux.die.net/man/1/rsync




--inplace
This option changes how rsync transfers a file when the file's data needs to be updated: instead of the default method of creating a new copy of the file and moving it into place when it is complete, rsync instead writes the updated data directly to the destination file.





share|improve this answer












rsync --inplace file-a file-b should do what you want.



https://linux.die.net/man/1/rsync




--inplace
This option changes how rsync transfers a file when the file's data needs to be updated: instead of the default method of creating a new copy of the file and moving it into place when it is complete, rsync instead writes the updated data directly to the destination file.






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 5 at 19:06









Johnride

1212




1212











  • It seems slightly odd to call upon something like rsync from a C program (or whatever he's writing) just to preserve permissions when copying a file...
    – Kusalananda
    Mar 5 at 19:11











  • Agreed, though he seems to be looking for an option that is either shell executable or easy to call from code (without indirection like cat file-a > file-b).
    – Johnride
    Mar 5 at 19:18










  • Thank you for the alternative. It is not a "core" command but it is also nice to know.
    – j123b567
    Mar 6 at 16:20

















  • It seems slightly odd to call upon something like rsync from a C program (or whatever he's writing) just to preserve permissions when copying a file...
    – Kusalananda
    Mar 5 at 19:11











  • Agreed, though he seems to be looking for an option that is either shell executable or easy to call from code (without indirection like cat file-a > file-b).
    – Johnride
    Mar 5 at 19:18










  • Thank you for the alternative. It is not a "core" command but it is also nice to know.
    – j123b567
    Mar 6 at 16:20
















It seems slightly odd to call upon something like rsync from a C program (or whatever he's writing) just to preserve permissions when copying a file...
– Kusalananda
Mar 5 at 19:11





It seems slightly odd to call upon something like rsync from a C program (or whatever he's writing) just to preserve permissions when copying a file...
– Kusalananda
Mar 5 at 19:11













Agreed, though he seems to be looking for an option that is either shell executable or easy to call from code (without indirection like cat file-a > file-b).
– Johnride
Mar 5 at 19:18




Agreed, though he seems to be looking for an option that is either shell executable or easy to call from code (without indirection like cat file-a > file-b).
– Johnride
Mar 5 at 19:18












Thank you for the alternative. It is not a "core" command but it is also nice to know.
– j123b567
Mar 6 at 16:20





Thank you for the alternative. It is not a "core" command but it is also nice to know.
– j123b567
Mar 6 at 16:20













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428346%2foverwrite-file-preserving-target-permissions-without-shell-invocation%23new-answer', 'question_page');

);

Post as a guest













































































BYcXm 7nUb 1ry 4wWmrMM,RuiSW fBUNa2k3aqZHs41jNRAnlwkN,h
oflrndp,kxOz GWcURauhS 97,db2DjSV0SaU,wpFJ,An YVS84Lf3lX4R6lWtwh t9 AnqKbxU,o1i

Popular posts from this blog

How to check contact read email or not when send email to Individual?

How many registers does an x86_64 CPU actually have?

Displaying single band from multi-band raster using QGIS