Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

Copy files preserving the permissions

3 Comments
Posted by Anirudh on October 1, 2009 at 8:52 PM

You might have come across a situation where your regular usage of the cp command for copying files in your linux box was not enough because while copying you needed to preserve the permissions of the files. Fret not, for help is at hand. Take a look at the man page for cp :

-p     same as --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, all

So that’s it. just use the -p flag to do the copying. Here’s an example where I copy a single file while preserving permissions.

cp -p oldfile ~/new_dest/

To copy a whole directory, I use the -r flag as well.

cp -rp some_dir ~/new_dest/

You can use the –preserve flag to keep whatever attributes you need. For the record, there’s one more related flag

--no-preserve=ATTR_LIST don't preserve the specified attributes

Related posts:

  1. Copying files from one unix box to another with scp
  2. How to join split files
  3. Useful vim commands
  4. vimdiff – the cool way to diff for vim users
You can leave a comment, or trackback from your own site.

3 Comments

  • On November 2, 2009 at 7:21 PM Cyriac Thomas said

    Never knew something like this was in there. Cool!

  • On October 18, 2011 at 1:33 PM Query said

    What if the remote site is password protected?

  • On October 21, 2011 at 10:19 AM Anirudh said

    cp does not work on remote sites. You would need to use scp. Format is something like scp src_username@src_host:PATH dst_username@dst_host:PATH . You can check man scp for details

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>