LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 06-09-2014, 04:08 AM   #1
bot.anist
LQ Newbie
 
Registered: Jun 2014
Location: Southern shores of Africa
Distribution: Debian wheezy
Posts: 6

Rep: Reputation: Disabled
How to chmod multiple files based on multiple --reference files ?


Hi Linux pro's,

OK, so I made a mistake while trying to move /usr, /var and /tmp to separate partitions (my single / partition was getting filled up). For some daft reason I copied (using cp -Rp) these dirs to an external hdd which was formatted as a ntfs filesystem, and then copied them back to the new partitions, edited fstab accordingly etc. etc. and deleted the original dirs from /. (yes I booted from a live-cd to do this)

So after rebooting I realized all the permissions was changed on these new partitions by the fact that it was copied first to a ntfs filesystem and then back to the new partitions. Lots of error messages etc. etc. which I managed to fix here and there but the system is still a bit wonky with some stuff not working.

Anyway, luckily some sanity prevailed and I backed up the original / directory which I subsequently restored (and mounted) on a loop image on another empty partition on my system.

Now I would like to change the permissions and owners of all files in /usr, /tmp and /var according to the restored original files in the mounted image file - perhaps using chmod --reference, although doing this one by one would be extremely time-consuming.

Question 1: Is there a way to do this perhaps recursively like using chmod -R --reference on the directories?

Question 2: If yes, how would I go about it ?

I suppose I could just boot from a live-cd and copy everything again (using cp -a or perhaps tar ?) in their respective new partions from the restored image file, but I am not sure if my nerves would handle this procedure again...

Many thanks in advance !
 
Old 06-09-2014, 04:24 AM   #2
rockstar05
Member
 
Registered: Dec 2011
Posts: 64

Rep: Reputation: Disabled
How to chmod multiple files based on multiple --reference files ?

as i understnd ur theory & question better way to use * option,
#chmod <permissions> *
 
Old 06-09-2014, 04:59 AM   #3
bot.anist
LQ Newbie
 
Registered: Jun 2014
Location: Southern shores of Africa
Distribution: Debian wheezy
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for the quick reply.

I am not sure I understand your solution. Are you suggesting I should try something like:

chmod -R --reference=/mnt/res/tmp/tmp/* /tmp/*

?

Cheers.
 
Old 06-09-2014, 05:23 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Wouldn't it be easier to just copy over the files with the correct permissions again?
 
Old 06-09-2014, 05:34 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Files in /usr, /tmp and /var may be owned by different users and have different access permissions. This should work:
Code:
find /mnt/res/tmp/ -printf "%U %G %m %p\n"|sed -e "s|/mnt/res/tmp||g"|while read LINE; do 
 LINE=( $LINE ); _UID=${LINE[0]}; _GID=${LINE[1]}; _PERMS=${LINE[2]}; unset LINE[0] LINE[1] LINE[2]; TGT=${LINE[*]}
 echo chown ${_UID}:${_GID} "${TGT}"; echo chmod ${_PERMS} "${TGT}"; 
done
Run as root, see if it looks sane and remove the "echo" to apply changes. If any files fail to have anything applied they're probably in use or otherwise won't allow changes. For those files use a Live CD and rinse & repeat.

Last edited by unSpawn; 06-09-2014 at 09:17 AM. Reason: //Removing a superfluous ';' and '/'.
 
1 members found this post helpful.
Old 06-09-2014, 05:36 AM   #6
bot.anist
LQ Newbie
 
Registered: Jun 2014
Location: Southern shores of Africa
Distribution: Debian wheezy
Posts: 6

Original Poster
Rep: Reputation: Disabled
Yes it seems it would, although I suffer from a disease called "like to complicate life for myself".

Also, I am intrigued by the possibilities that can be accomplished by using clever code and perhaps learn something in the process, so I thought I'd delay copying everything again and search for an alternative solution.

But perhaps not. Thanks anyway !

Cheers.
 
Old 06-09-2014, 06:16 AM   #7
bot.anist
LQ Newbie
 
Registered: Jun 2014
Location: Southern shores of Africa
Distribution: Debian wheezy
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks unSpawn !

Wow, the echo definitely looks sane from what I can see. I'll do the actual apply tonight using a live cd, and let you know.

-there was a small typo with a duplicated ";" in your code above, but easily removed.

Being a complete bash code newbie, I am for sure going to learn much from your code above once I have figured out what it means exactly.

Cheers.
 
Old 06-09-2014, 09:16 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Quote:
Originally Posted by bot.anist View Post
-there was a small typo with a duplicated ";" in your code above, but easily removed.
Ah, thanks.


Quote:
Originally Posted by bot.anist View Post
Being a complete bash code newbie, I am for sure going to learn much from your code above once I have figured out what it means exactly.
Well, let's break it down then:

Code:
find /mnt/res/tmp/ 
# only echo the numerical user Id, group Id, permissions in octal (needed for /tmp dir itself and various files in /usr/*bin/ and /var) and file name.
# Often "-print0" will be better due to LFS ('man bash') though.
-printf "%U %G %m %p\n"
# 'man sed': remove the string that's the base directory where the backup files reside so you get the actual path (I'll correct that above as there's one slash too many)
|sed -e "s|/mnt/res/tmp/||g"
# use BASH readline to populate a variable
|while read LINE; do 
 # Make an array out of the variable and assign the elements to other variables
 LINE=( $LINE ); _UID=${LINE[0]}; _GID=${LINE[1]}; _PERMS=${LINE[2]};; unset LINE[0] LINE[1] LINE[2]; TGT=${LINE[*]}
 # Using 'echo' is a good way to test if the result looks OK
 echo chown ${_UID}:${_GID} "${TGT}"; echo chmod ${_PERMS} "${TGT}"; 
done
 
1 members found this post helpful.
Old 06-11-2014, 03:43 AM   #9
bot.anist
LQ Newbie
 
Registered: Jun 2014
Location: Southern shores of Africa
Distribution: Debian wheezy
Posts: 6

Original Poster
Rep: Reputation: Disabled
OK, so I tried it and for some reason it did not work right (permissions changed but not reflecting the reference originals), so I did it the easy way - copying everything again (by using 'cp -a' from a live-cd).

I think there may have been some files that were added or removed from the original files or some bug that slipped in somewhere, as it seems the code above works well. Perhaps I did not inspect the echo output as well as I should have. Anyway no harm done.

Thanks for the code breakdown above - I am a wiser man today, but clearly has still lots to learn.

Cheers.
 
Old 06-12-2014, 05:01 PM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Quote:
Originally Posted by bot.anist View Post
so I did it the easy way - copying everything again (by using 'cp -a' from a live-cd).
Interesting method ;-p


Quote:
Originally Posted by bot.anist View Post
I think there may have been some files that were added or removed from the original files or some bug that slipped in somewhere, as it seems the code above works well. Perhaps I did not inspect the echo output as well as I should have. Anyway no harm done.
Next time please post part of the output. That could help us help you better.


Quote:
Originally Posted by bot.anist View Post
Thanks for the code breakdown above - I am a wiser man today, but clearly has still lots to learn.
As do I, as do I...
 
Old 06-13-2014, 03:54 AM   #11
bot.anist
LQ Newbie
 
Registered: Jun 2014
Location: Southern shores of Africa
Distribution: Debian wheezy
Posts: 6

Original Poster
Rep: Reputation: Disabled
:-)

Actually I did preserve the output:

chown: cannot access `': No such file or directory
chmod: cannot access `': No such file or directory
chown: cannot dereference `usr/lib/i386-linux-gnu/libOSMesa.so': No such file or directory
chmod: cannot operate on dangling symlink `usr/lib/i386-linux-gnu/libOSMesa.so'
chown: cannot dereference `usr/share/java/werken.xpath.jar': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/java/werken.xpath.jar'
chown: cannot dereference `usr/share/bug/vlc-plugin-pulse': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/bug/vlc-plugin-pulse'
chown: cannot dereference `usr/share/bug/vlc-nox': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/bug/vlc-nox'
chown: cannot dereference `usr/share/bug/vlc': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/bug/vlc'
chown: cannot dereference `usr/share/bug/libvlc5': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/bug/libvlc5'
chown: cannot dereference `usr/share/bug/vlc-plugin-notify': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/bug/vlc-plugin-notify'
chown: cannot dereference `usr/share/dict/words': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/dict/words'
chown: cannot dereference `usr/share/doc/kde/HTML/en_GB/kcontrol/kalarmd/common': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/doc/kde/HTML/en_GB/kcontrol/kalarmd/common'
chown: cannot dereference `usr/share/doc/kde/HTML/en_GB/knotes/common': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/doc/kde/HTML/en_GB/knotes/common'
chown: cannot dereference `usr/share/doc/kde/HTML/en_GB/kleopatra/common': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/doc/kde/HTML/en_GB/kleopatra/common'
chown: cannot dereference `usr/share/doc/kde/HTML/en_GB/knode/common': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/doc/kde/HTML/en_GB/knode/common'
chown: cannot dereference `usr/share/doc/kde/HTML/en_GB/kalarm/common': No such file or directory
chmod: cannot operate on dangling symlink `usr/share/doc/kde/HTML/en_GB/kalarm/common'


So I guessed these were the files that I had to change via a live-cd as they were probably open during the procedure likw you said (being dangling symlinks). Except perhaps the first two lines... not sure what that was.

I promptly rebooted and did a 'sudo apt-get update' (just to check), only to find a message saying the sudoers list being open for anybody to write/change or something to that effect. "Eish!" (to use some local slang expressing disappointment, dissatisfaction or regret, even alarm sometimes). So I figured I should just redo the copy procedure but in the right way this time. Don't want anybody to get access to my top secret and highly classified files on my system if you know what I mean :-)

From the little background I have with R code, I wonder if perhaps a clause in your code above to first match filenames before changing permissions/owners would have been better (although I have no idea if this would be feasible or possible).

Cheers.
 
  


Reply


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
how do you rename multiple files based on strings in a text file? Holering Programming 9 01-08-2013 11:22 PM
script to created folders based on multiple input files prravin1 Programming 1 05-04-2012 07:42 AM
using chmod on multiple files. penduleum Linux - Software 2 04-04-2009 07:43 AM
using chmod for multiple files matrix13 Linux - General 3 02-14-2007 05:45 AM


All times are GMT -5. The time now is 06:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration