chmod through loop

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
datasheet
Lance Naik
Posts: 16
Joined: Thu Apr 27, 2006 6:51 pm

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

wacky
Naik
Posts: 94
Joined: Thu Jun 10, 2004 7:42 pm
Location: London, UK

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


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

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


LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

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

Farrukh Ahmed

lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

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


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

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

**********************************************

wacky
Naik
Posts: 94
Joined: Thu Jun 10, 2004 7:42 pm
Location: London, UK

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.

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

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



Return to “Programming”

Who is online

Users browsing this forum: No registered users and 1 guest