Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-14-2009, 06:50 PM   #1
LQ Newbie
Registered: Jan 2006
Posts: 10

Rep: Reputation: 0
modify owner and group names when copying on linux bash

I am simply trying to copy a folder dir and all files to another location , and I would like to custom name the owner and group permissions when I do the copy.

cp -v -a -p /home/joessho/public_html/caps/* /home/joessho/public_html

In this process how do I name the new owner and group of the new files all in one fell swoop with my above command?
Old 08-14-2009, 07:25 PM   #2
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927
You can't. It'll have to be a two-step process.
Old 08-14-2009, 08:29 PM   #3
LQ Newbie
Registered: Jan 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Oh. I that case. How would I apply an owner and group to the files once they are in the new folder?

Would this do the trick?

$ /home/joessho/public_html chown -Rv -r joessho

If I was in the dir?
Old 08-14-2009, 08:37 PM   #4
LQ Newbie
Registered: Jan 2006
Posts: 10

Original Poster
Rep: Reputation: 0
The thing is it's kind of specific what I need.

I don't want to change the ownership of the public_html folder itself. Only all files & folders recursive below it in the hierarchy.


------------- All folders and files owners and groups should be changed to:


And why are my ownerships and groups names showing as numbers and not actual account names? Is this a problem?
Old 08-14-2009, 08:49 PM   #5
Registered: Sep 2007
Location: Kerala, India
Distribution: ubuntu 10.04
Posts: 367

Rep: Reputation: 55
making public_html as your present working directory try the following command
sudo chown -R user:group *
it is working for me. Also ownership of the public_html is preserved

Last edited by mobinskariya; 08-14-2009 at 08:50 PM.
Old 08-14-2009, 09:32 PM   #6
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681
You can run "chown -R <user>.<group> <target_directory>/" and "chmod -R <mode> <target_directory>/" as root to change all of the files in the target directory recursively.

To change the ownership of files, you need to be the root user.

One thing you could look at is something like:
tar -C /home/joessho/public_html/caps/ --owner=jackm --group=users --mode='ug=rw,o=r' -f - | su -c 'tar -C /home/jackm/public_html -xf- .'
An actual tarfile isn't created. A pipe is used to transfer the tarball from the first tar command to the second. This is a way to copy directory structures to another directory. The --owner, --group and --mode options allow you to change the ownership and permissions of the files as they are being archived. Those are the properties they will have when they are extracted. There are two other options dealing with replacing timestamps you can use as well.

You could apply other options such as --newer or -g timestampfile to replicate the files from the first dir to the second one. Using rsh or ssh on the RHS you can run a script to copy or replicate files from one computer to another, even across the internet.

Be sure you read the tar info manual before using this, and test if out. There may be more option you need. Pay attention to things such as the order that things are backed up. E.G. files or directories first, and whether you want files or directories things overwritten.

Even if you copy the files, and then change the permissions, you need to be careful. For example, you may need to change the mode of the files separately from the mode of the directories. The 'x' bit needs to be set on directories, but shouldn't be set for non-programs.
find dest_dir/ -type f -exec -chmod u=rw,o=r '{}' \;
find dest_dir/ -type d -exec -chmod u=rwx,o=rx '{}' \;

As you probably notice, one disadvantage to recursively changing the permissions of the destination directory is that you will be processing all of the files, not just the ones you copied over. What do you want to do if the same file exists on the destination? Overwrite the file? Keep the original? Or maybe copy the original and create a backup.

Good Luck!

note: I didn't test my example. Please test them yourself it you decide to use it.
For example, I don't know if tar will clear the 'x' bit in my example for directories, or just for files.

Last edited by jschiwal; 08-14-2009 at 09:39 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
rsync not preserving owner and group graziano1968 Linux - Server 6 09-10-2008 07:52 AM
bash command that lists all members of a group by their real names quadmore Linux - Newbie 4 11-07-2005 10:46 AM
group owner etc MI3GTO Mandriva 3 02-27-2005 08:26 PM
Group as owner instead of a user dtournas Linux - General 1 09-11-2003 10:13 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:09 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration