[SOLVED] How to chmod multiple files based on multiple --reference files ?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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...
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 '/'.
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.
-there was a small typo with a duplicated ";" in your code above, but easily removed.
Ah, thanks.
Quote:
Originally Posted by bot.anist
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
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.
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
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
Thanks for the code breakdown above - I am a wiser man today, but clearly has still lots to learn.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.