Find and change permission in linux

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.
LVL 2
smksaAsked:
Who is Participating?
 
lanboyoConnect With a Mentor Commented:
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
 
ai_ja_naiCommented:
too many parameters to chmod: get rid of that '-x'
0
 
woolmilkporcCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
ai_ja_naiCommented:
(lol, the ol' far west quick draw challenge)
0
 
woolmilkporcCommented:
At least I came off second-best! (OK, not very useful in some far west scenarios, I fear ...)
0
 
lanboyoCommented:
Third.
0
 
omarfaridCommented:
which one is not working?

I am not sure if chmod accepts -x option

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

0
 
ai_ja_naiCommented:
@omarfarid: chmod can't accept both symbolic and octal parameters at the same time

@woolmikporc: LOL
0
 
Deepak KosarajuConnect With a Mentor DevOps EngineerCommented:
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
 
omarfaridCommented:
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
 
ai_ja_naiCommented:
guh! :p
0
 
omarfaridCommented:
sorry for my ignorance , but what ' guh! :p ' means?
0
 
ai_ja_naiCommented:
stupified surprise due to some sort of realisation, with a tongue smile :)
0
 
smksaAuthor Commented:
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
 
woolmilkporcCommented:
Yu'll have to escape the semicolon for -exec to work properly -
find /home/USERNAME/public_html -name "*.cgi" | xargs chmod 755 \;

0
 
woolmilkporcConnect With a Mentor Commented:
... pardon, overlooked that it's 'xargs.
Try this -
find /home/USERNAME/public_html -name "*.cgi" | xargs -i{}  chmod 755 {}
0
 
omarfaridCommented:
why do you need the ; ? just remove it
0
 
ai_ja_naiCommented:
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
 
smksaAuthor Commented:
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
 
ai_ja_naiCommented:
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
 
omarfaridCommented:
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
 
smksaAuthor Commented:
Thanks to all
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.