Solved

Find and change permission in linux

Posted on 2009-05-18
22
607 Views
Last Modified: 2013-12-21
Hi,

I would like to find and change the file/folder permission using the following command, but it seems to not working :

find /home/username/public_html -type d | xargs chmod -x 755
find /home/username/public_html -type f | xargs chmod -x 644
find /home/username/public_html -name *.cgi | xargs chmod -x 755
find /home/username/public_html -name *.pl | xargs chmod -x 755

Is there any wrong with the above command ?

Thank you.
0
Comment
Question by:smksa
  • 7
  • 5
  • 4
  • +3
22 Comments
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 24411447
too many parameters to chmod: get rid of that '-x'
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24411457
Hi,
don't intermix symbolic and numeric formats of chmod.
So either the '-x' (remove execute permission) or the 755 has to be left out.
wmp
 
0
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 24411469
(lol, the ol' far west quick draw challenge)
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24411495
At least I came off second-best! (OK, not very useful in some far west scenarios, I fear ...)
0
 
LVL 10

Accepted Solution

by:
lanboyo earned 200 total points
ID: 24411524
Well you are mixing chmod syntaxes. -x means remove the execute, while the numbers are explicitly setting the permissions. Should just be chmod 755 FILENAME

xargs just confuses me though.

I like to use the -exec directive in find, which would be like this. I don't know if it is supported on your system. The backslashes in front of the * and ; are to  make it shell script ready. Might not need them from the command line.

find /home/username/public_html  -name \* -type d  -exec chmod 755 {}  \;

This replaces the {} with the results of the find, and executes once for each result.

the command

find /home/username/public_html  -name \* -type d  -exec chmod 755 {} +

Does the same except that it batches the files into groups and feeds it to the command, great for when you need to delete a million files and the args list is too long for the shell.
0
 
LVL 10

Expert Comment

by:lanboyo
ID: 24411526
Third.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24411553
which one is not working?

I am not sure if chmod accepts -x option

consider putting " or ' around *.cgi  & *.pl

0
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 24411625
@omarfarid: chmod can't accept both symbolic and octal parameters at the same time

@woolmikporc: LOL
0
 
LVL 14

Assisted Solution

by:Deepak Kosaraju
Deepak Kosaraju earned 100 total points
ID: 24411699
You cannot user -x and 755 either user character type permission like a-x  (or) number code 755. Following are the only options available for chmod.
So try either of the following
find /home/username/public_html -name "*.cgi" | xargs chmod a-x
(or)
find /home/username/public_html -name "*.cgi" | xargs chmod 755

you can also try following through -exec
find /home/username/public_html -name "*.cgi"  -exec chmod 755 {} \;

find /home/username/public_html -name "*.cgi" | xargs chmod a-x

(or)

find /home/username/public_html -name "*.cgi" | xargs chmod 755
 

you can also try following through -exec

find /home/username/public_html -name "*.cgi"  -exec chmod 755 {} \;
 
 
 

OPTIONS

       Change the mode of each FILE to MODE.
 

       -c, --changes

              like verbose but report only when a change is made
 

       --no-preserve-root

              do not treat / specially (the default)
 

       --preserve-root

              fail to operate recursively on /
 

       -f, --silent, --quiet

              suppress most error messages
 

       -v, --verbose

              output a diagnostic for every file processed
 

       --reference=RFILE

              use RFILEs mode instead of MODE values
 

       -R, --recursive

              change files and directories recursively
 

       --help display this help and exit

Open in new window

0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24412212
thank you ai_ja_nai :) what I was trying to highlight to question author is that -x is not an option like -R , etc. maybe I should have used more clear statement
0
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 24412245
guh! :p
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 40

Expert Comment

by:omarfarid
ID: 24412285
sorry for my ignorance , but what ' guh! :p ' means?
0
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 24412308
stupified surprise due to some sort of realisation, with a tongue smile :)
0
 
LVL 2

Author Comment

by:smksa
ID: 24412717
Hi,

I have tried :

 find /home/USERNAME/public_html -name "*.cgi" | xargs chmod 755;

But the following error comeout :

chmod: missing operand after `755'
Try `chmod --help' for more information.

Hope somebody can help.

Thanks.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24412740
Yu'll have to escape the semicolon for -exec to work properly -
find /home/USERNAME/public_html -name "*.cgi" | xargs chmod 755 \;

0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 200 total points
ID: 24412750
... pardon, overlooked that it's 'xargs.
Try this -
find /home/USERNAME/public_html -name "*.cgi" | xargs -i{}  chmod 755 {}
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24412763
why do you need the ; ? just remove it
0
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 24412846
the error is here:

> find /home/USERNAME/public_html -name "*.cgi" | xargs chmod 755;
                                                                                                             ^

remove the ';'  , otherwise you'll terminate the command before xargs passes the argument to chmod
0
 
LVL 2

Author Comment

by:smksa
ID: 24412895
Hi,

I have removed the ";" but it make no different wit hthe error.

But the "find /home/USERNAME/public_html -name "*.cgi" | xargs -i{}  chmod 755 {}" helps .

Thanks.
0
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 24413392
I don't know what you did, but the syntax

find /my/custom/path -name "*.myextension" | xargs chmod 755

worked for me. What errors did it throw?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24413479
you may face problem if file name contain spaces, try

find /home/username/public_html -type d | while read file
do
    chmod 755 "$file"
done
0
 
LVL 2

Author Closing Comment

by:smksa
ID: 31582567
Thanks to all
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
The viewer will learn how to count occurrences of each item in an array.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now