chmod through loop

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.

chmod through loop

Postby datasheet » Mon Dec 18, 2006 10:56 pm

hello,
I have 20 directories under /home folder and I need to chmod to all of them as a+w... how can I do that without doing them manually?

this is what I want to look like

root#chmod a+x ${mydir}

where mydir is the variable name which should come from file one at a time.

Thanks,
Data Sheet
datasheet
Lance Naik
 
Posts: 16
Joined: Thu Apr 27, 2006 6:51 pm

Postby wacky » Tue Dec 19, 2006 2:24 am

Assuming all dirs are in a file named dirlist - one per line :

Code: Select all

for dir in `cat dirlist`
do
      chmod a+x $dir
done


or

If you want to recursively change permissions on all dirs under /home, then use this command:

find /home -type d -exec chmod a+x {} \;
wacky
Naik
 
Posts: 94
Joined: Thu Jun 10, 2004 7:42 pm
Location: London, UK

Postby datasheet » Tue Dec 19, 2006 2:35 am

Thank Wacky,
I will try that command and will post result here.
datasheet
Lance Naik
 
Posts: 16
Joined: Thu Apr 27, 2006 6:51 pm

Re:

Postby LinuxFreaK » Tue Dec 19, 2006 9:04 am

Dear datasheet,
Salam,

find command is better then for loop :)

Best Regards.
Farrukh Ahmed
LinuxFreaK
Site Admin
 
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
ICQ: 82075802
Website: http://www.linuxpakistan.net/wiki/index.php?pagename=LinuxFreak
WLM: f4fahmed@hotmail.com
Yahoo Messenger: f4fahmed@yahoo.com
AOL: linuxpakistan@aol.com
Location: Karachi

Postby lambda » Tue Dec 19, 2006 1:02 pm

use xargs. it's there to help you!

xargs chmod a+w < dirlist

find /home -type d -print | xargs chmod a+w

the difference between find ... -exec ... and find ... | xargs ... is that the former runs the chmod command for every directory, and the latter runs it for every 5,000 or so directories. it's a lot faster that way.
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby kashif » Wed Dec 20, 2006 3:02 pm

Asslam 0 alikum!!

If you want to change the permission of only directries then use command as mentioned by lambda

If you want ot change permisson of all files and directories under /home it is so simple.

chmod -R a+rw /home


Allah Hafiz
**********************************************

As-Salaatu was-Salaamu Alaika Ya Sayyidi Ya Rasool ALLAH

**********************************************
kashif
Naib Subedar
 
Posts: 305
Joined: Wed Oct 15, 2003 2:44 am
Location: Okara

Postby wacky » Wed Dec 20, 2006 10:39 pm

Point made by Lambda about speed of execution is an important one. If one is dealing with hundreds or thousands of files/dirs, then xargs is the tool to use. Here is a simple comparison:

# time find . -print -exec ls {} \; > /dev/null

real 0m10.108s
user 0m2.050s
sys 0m5.460s

# time find . -print | xargs ls > /dev/null

real 0m0.309s
user 0m0.090s
sys 0m0.230s

0.3 secs elapsed time against 10 secs is about 30 times faster.
wacky
Naik
 
Posts: 94
Joined: Thu Jun 10, 2004 7:42 pm
Location: London, UK

Postby datasheet » Tue Dec 26, 2006 7:34 pm

I was able to change the permission on all directories under /home with the help of FOR LOOP.. This was easy for me to use instead of find or xargs...

Thanks Wacky,

Data sheet
datasheet
Lance Naik
 
Posts: 16
Joined: Thu Apr 27, 2006 6:51 pm


Return to “%s” Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron