LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script 2 /home /backup (https://www.linuxquestions.org/questions/programming-9/bash-script-2-home-backup-644900/)

zerocool22 05-26-2008 01:43 PM

bash script 2 /home /backup
 
Hey, i have to folders /home/ and /backup/ Backup is just has the same folders as the one in from /home/ so /home/jacky and /backup/jacky but they all contain different content and files.
The folders from /home/ has user and group permissions set to it, and the ones from /backup/ doesnt.

So now i need a script that set the user and group permissions for every folder from backup to the same from /home/.

Any help?

Just in plain words, with correct commands will help me out alot.
Thx

raskin 05-26-2008 02:20 PM

Hm, what I would do is probably
Code:

cd /home; find . -type d -exec chmod --reference='{}' ';' ../backup/'{}'
Explanation:
find is to list or uniformly process all the objects meeting a certain criteria.
find . : means that you want to traverse current directory (you are in /home , and relative directory paths with respect to home/ can be used as relative paths with respect to backup/)
-type d : means you are only interested in directories.
-exec : you do not want to simply print files, you want a command executed on them. In the command (all the following arguments) any occurence of {} (we have to escape it from shell, so '{}') is replaced with file name, and the command must be terminated with ; (again, shell would like to interpret it as an end-of-command and eat it, so ';')
chmod --reference=file another_file : change permissions of the second file, trying to reproduce permissions of the first file.

man find, man chmod will give you more information.

zerocool22 05-27-2008 03:19 AM

Quote:

Originally Posted by raskin (Post 3165232)
Hm, what I would do is probably
Code:

cd /home; find . -type d -exec chmod --reference='{}' ';' ../backup/'{}'
Explanation:
find is to list or uniformly process all the objects meeting a certain criteria.
find . : means that you want to traverse current directory (you are in /home , and relative directory paths with respect to home/ can be used as relative paths with respect to backup/)
-type d : means you are only interested in directories.
-exec : you do not want to simply print files, you want a command executed on them. In the command (all the following arguments) any occurence of {} (we have to escape it from shell, so '{}') is replaced with file name, and the command must be terminated with ; (again, shell would like to interpret it as an end-of-command and eat it, so ';')
chmod --reference=file another_file : change permissions of the second file, trying to reproduce permissions of the first file.

man find, man chmod will give you more information.

thx for the reply, but however, im not good at this, so i just copied your code into mine command line. Not sure if i need to change anything to your script. There like 380 folders so doing them manually is not an option. So i runned youre script and it says:
find: pathnames must first have expressions
Use: find [-H] [-L] [-P] [path..] [expression]

Can you help me out once more?

thx!

raskin 05-27-2008 03:32 AM

Hm, I typed must be terminated with ; in explanation and messed it up in the command. ';' should be the last parameter to find, not one-before-last.

But in general, if you have a sample, an explanation and references, it is usually better to try to verify explanation against manual page references and make sure that you understand the command a complete stranger offers you to run in /home/. I do hope that on LQ a post with hidden destructive commands get deleted, but it can be spotted too late..

zerocool22 05-27-2008 03:39 AM

ok thx mate, now it runs, but it says for my files
chmod: cannot get acces to ../backup/./jacky file/or folder does not excist
chmod: cannot get acces to ../backup/./dirk file/or folder does not excist
chmod: cannot get acces to ../backup/./tommy file/or folder does not excist
.....

Is it giving error to the backup/./dirk? or is it giving error to ../backup? my backup folder is just /backup/jacky /backup/dirk and so on, if this is any help?

[edit]
if i change the script to:
cd /backup; find . -type d -exec chmod --reference='{}' ';' ../home/'{}'
it does run?

So what am i doing here? I need to set every folder in backup to the same properties from /home/?

Cheers

raskin 05-27-2008 10:03 AM

Well, it should. I thought you have mirror directory structure. ';' must be moved back to the end; if you work in /backup, you want to chmod '{}' and use /home/'{}' as reference, not the other way round. Also with most mass-execution commands it is good to run it with the mass command prepended by "echo", i.e. replace chmod with echo chmod. If you believe that the commands printed will be correct (it is much easier to verify a single chmod than a complicated find), run without echo.


All times are GMT -5. The time now is 11:42 AM.