LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2008, 01:43 PM   #1
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Rep: Reputation: 15
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
 
Old 05-26-2008, 02:20 PM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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.
 
Old 05-27-2008, 03:19 AM   #3
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by raskin View Post
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!
 
Old 05-27-2008, 03:32 AM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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..
 
Old 05-27-2008, 03:39 AM   #5
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
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

Last edited by zerocool22; 05-27-2008 at 04:01 AM.
 
Old 05-27-2008, 10:03 AM   #6
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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.
 
  


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
bash backup script kristof_v Programming 4 07-24-2007 07:33 PM
Need help with a bash script that does a backup jamtech Programming 8 06-07-2007 11:36 PM
Writing a script to backup up /home/username ginda Programming 3 01-05-2006 06:47 AM
Backup script in bash gauge73 Programming 13 10-17-2005 06:25 AM
bash shell backup script turnip Programming 0 04-03-2003 06:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 PM.

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