?
Solved

Find and change permission in linux

Posted on 2009-05-18
22
Medium Priority
?
619 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
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 800 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 400 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
 
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 800 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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

752 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