How to copy a Read-Only file and make the copy writable with a single cp command?

15,450

You should look at the install command.

From man install

SYNOPSIS
       install [OPTION]... [-T] SOURCE DEST
       install [OPTION]... SOURCE... DIRECTORY
       install [OPTION]... -t DIRECTORY SOURCE...
       install [OPTION]... -d DIRECTORY...

DESCRIPTION
       This  install  program copies files (often just compiled) into 
       destination locations you choose.  If you want to download and install
       a ready-to-use package on a GNU/Linux system, you should instead be 
       using a package manager like yum(1) or apt-get(1).

       In the first three forms, copy SOURCE to DEST or multiple SOURCE(s) to
       the existing DIRECTORY, while setting permission modes and owner/group.
       In  the  4th form, create all components of the given DIRECTORY(ies).
...
       -m, --mode=MODE
          set permission mode (as in chmod), instead of rwxr-xr-x
Share:
15,450
user389238
Author by

user389238

Updated on September 17, 2022

Comments

  • user389238
    user389238 almost 2 years

    How to copy a Read-Only file in Linux and make the copy writable with a single cp command in Linux (Ubuntu 10.04)?

    The --no-preserve and --preserve seemed to be good candidates, except that they should "and" the mode flags, while what I am looking for is something that will "or" them (add +w mode).

    More details:

    I have to import a repository from GIT to Perforce. I want that all Perforce depot files are Read-Only (that is how Perforce was designed), while all other files that were derived/copied from depot files are writable. Currently if a Makefile tries to copy a Read-Only file then the derived file will also be Read-only. This leads to build-errors when cp tries to overwrite Read-Only file second time. Of course the --force is a workaround here but then the derived file is also Read-Only. Also I do not want to mess with "chmod" after each "cp" command - I will do that only as the last resort.

  • user389238
    user389238 over 13 years
    It seems that "install" can do what I need. I also found out that "rsync" with "--chmod" is even more superior.
  • linuxgnuru
    linuxgnuru almost 8 years
    or just use umask?