unix cp command

Posted on 2011-05-04
Last Modified: 2012-06-27
i want to cp files from one drive to another on the same server, but i would like to retain the owner, group, and user permissions. also, all the files i want to copy start with the user's name, so is there a way to use grep or something like it to cp just the files that start with this user's name? would it be something like

cp /backup/directory/username* /livedirectory/directory

Question by:JeffBeall
    LVL 68

    Accepted Solution

    Yes, but use "cp -p ..." to preserve ownership and permissions. Additionally consider using the "-r" flag to recurse into subdirs, if desired.

    LVL 29

    Assisted Solution

    Try cp -p

    p preserve the owner ship.

    LVL 6

    Assisted Solution

    Hi there,

    Use the -p switch of the cp command to retain the owner/group and other file attributes.

    So use cp -R -p /backup/directory/* /livedirectory/directory

    Not sure if your username* solution works to recursively copy all files starting with the username. If it doesn't work you could try using find and using its output as input for the cp command. You'd get something like this:

    find /backup/directory -type f -name "username*" -exec cp -p {} /livedirectory/directory;

    I didn't test this, so use at your own risk. ;)

    Hope this helps.
    LVL 8

    Assisted Solution

    could try

    find . /backup/directory/username* | cpio -pdumv --owner username:group /livedirectory/directory

    LVL 1

    Author Closing Comment

    thank you for the help, the cp -r worked great

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
    Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now