LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-12-2018, 09:13 AM   #1
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
Cloning files permissions and ownership


I have to clone files permissions and ownership for entire /usr directory. I can't just copy files because the source and the destination files built with different cflags and different options. I just need to clone permissions on built files.

There is --reference=RFILE option for chown/chmod commands, but I don't know how to use these command recursively with that option.

I doubt that
Code:
chown --reference=/usr /mount_point/usr -R
will work
though
Code:
chown owner:owner /mount_point/usr -R
works

Any thoughts or ideas?
 
Old 02-12-2018, 09:44 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
You can do a loop for all the files:

Code:
cd /usr 
for file in $(find .)
do chown --reference=$file /mount_point/usr/$file
done
 
1 members found this post helpful.
Old 02-12-2018, 10:39 AM   #3
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Original Poster
Rep: Reputation: 142Reputation: 142
it gives output like this:
Code:
chown: cannot access '/media/usr/./share/apps/amarok/images/smallstar.png': No such file or directory
so I modified your script a bit:
Code:
for file in $(find  /usr)
do chown --reference=$file /media$file
done
now it finds filenames with absolute path instead of relative "./"

Thanks.
 
Old 02-12-2018, 12:23 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Glad you got it solved.

I notice you changed name of the target path between your initial post and your final one.

Also you may have missed that I said you should cd to the source directory, /usr, before running the find. The syntax I wrote worked properly when I did a test here. "find ." means "find from this location so if you're sitting in /usr it would do the find there but wouldn't include the "/usr" in the output. Either way should work.
 
Old 02-12-2018, 01:14 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202
Use chown -h so it also changes ownership of symlinks. (Otherwise follows the symlinks, that can be dangerous in some scenarios.)
Also, quote variables in command arguments.
Code:
for file in $(cd /usr && find .)
do chown -h --reference="$file" /media/usr/"$file"
done
 
2 members found this post helpful.
Old 02-12-2018, 02:50 PM   #6
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Original Poster
Rep: Reputation: 142Reputation: 142
@MensaWater
"find ." returns relative filename, it starts with "./" symbols.
In fact, related to the current directory, it gives right filename, but when "./" prepended with the destination directory (like /media/usr) it gives wrong file name: "/media/usr/./some_sub_dir" with the dot in the middle.
I posted above the sample of erroneous filename
So, to fix this error we need to get absolute file name like "/usr/some_sub_dir" in $file variable which being concatenated with "/media" gives correct path: "/media/usr/some_sub_dir" without any dot in the middle
And "find /usr" returns absolute filename.
This case "cd /usr" command is unnecessary.
 
Old 02-12-2018, 03:57 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202
Effectively an extra ./ does not change anything.
For example
Code:
ls /usr/bin
ls /usr/./bin
ls /usr/././bin
are effectively identical.
 
Old 02-13-2018, 02:58 AM   #8
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Original Poster
Rep: Reputation: 142Reputation: 142
Not sure why it occurs but with extra dot I getting a zillion errors "No such file or directory"
 
  


Reply



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
Compiling: permissions, ownership of files ... JosephS Slackware 1 12-22-2007 01:04 PM
Copy files retaining ownership/permissions stefaandk Linux - Newbie 4 09-07-2006 08:25 PM
Group Permissions and Ownership of files and folders bugeyemonster Linux - General 1 04-19-2006 01:52 AM
Apache can't write files despite permissions/ownership linuxChique Linux - Software 2 02-20-2006 10:08 AM
changing permissions and ownership on many files and folders Cinematography Linux - General 4 08-01-2005 03:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:01 AM.

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