LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to chmod multiple files based on multiple --reference files ? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-chmod-multiple-files-based-on-multiple-reference-files-4175507465/)

bot.anist 06-09-2014 03:08 AM

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 !

rockstar05 06-09-2014 03:24 AM

How to chmod multiple files based on multiple --reference files ?
 
as i understnd ur theory & question better way to use * option,
#chmod <permissions> *

bot.anist 06-09-2014 03:59 AM

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.

TobiSGD 06-09-2014 04:23 AM

Wouldn't it be easier to just copy over the files with the correct permissions again?

unSpawn 06-09-2014 04:34 AM

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.

bot.anist 06-09-2014 04:36 AM

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.

bot.anist 06-09-2014 05:16 AM

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.

unSpawn 06-09-2014 08:16 AM

Quote:

Originally Posted by bot.anist (Post 5185015)
-there was a small typo with a duplicated ";" in your code above, but easily removed.

Ah, thanks.


Quote:

Originally Posted by bot.anist (Post 5185015)
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


bot.anist 06-11-2014 02:43 AM

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.

unSpawn 06-12-2014 04:01 PM

Quote:

Originally Posted by bot.anist (Post 5186158)
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 (Post 5186158)
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 (Post 5186158)
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...

bot.anist 06-13-2014 02:54 AM

:-)

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.


All times are GMT -5. The time now is 11:18 PM.