Overwrite file preserving target permissions without shell invocation
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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?
permissions file-copy
add a comment |Â
up vote
1
down vote
favorite
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?
permissions file-copy
1
When I docp file-a file-b
, the permissions and ownership offile-b
do not change.âÂÂAre you sure you arenâÂÂt runningcpâ¯-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
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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?
permissions file-copy
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?
permissions file-copy
edited Mar 6 at 16:16
asked Mar 5 at 18:50
j123b567
255128
255128
1
When I docp file-a file-b
, the permissions and ownership offile-b
do not change.âÂÂAre you sure you arenâÂÂt runningcpâ¯-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
add a comment |Â
1
When I docp file-a file-b
, the permissions and ownership offile-b
do not change.âÂÂAre you sure you arenâÂÂt runningcpâ¯-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
add a comment |Â
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
This is also useful. I also forget this trivial solution. Thank you!
â j123b567
Mar 6 at 16:19
add a comment |Â
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.
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 likecat 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
add a comment |Â
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
This is also useful. I also forget this trivial solution. Thank you!
â j123b567
Mar 6 at 16:19
add a comment |Â
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
This is also useful. I also forget this trivial solution. Thank you!
â j123b567
Mar 6 at 16:19
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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.
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 likecat 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
add a comment |Â
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.
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 likecat 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
add a comment |Â
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.
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.
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 likecat 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
add a comment |Â
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 likecat 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
add a 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%2f428346%2foverwrite-file-preserving-target-permissions-without-shell-invocation%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
1
When I do
cp file-a file-b
, the permissions and ownership offile-b
do not change.âÂÂAre you sure you arenâÂÂt runningcpâ¯-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