chmod through loop

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

chmod through loop

Post by datasheet »

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

Post by wacky »

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 {} \;
datasheet
Lance Naik
Posts: 16
Joined: Thu Apr 27, 2006 6:51 pm

Post by datasheet »

Thank Wacky,
I will try that command and will post result here.
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear datasheet,
Salam,

find command is better then for loop :)

Best Regards.
Farrukh Ahmed
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

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.
kashif
Naib Subedar
Posts: 305
Joined: Wed Oct 15, 2003 2:44 am
Location: Okara

Post by kashif »

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

Post by wacky »

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

Post by datasheet »

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
Post Reply