Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

chmod command on long list of files

Highlighted
Records Management
Frequent Advisor

chmod command on long list of files

I have a directory that has 99,999 files in it.

I want to do a chmod command to change the permissions on all 99,999 files. When I do the chmod command I get:

sh: /usr/bin/chmod: The parameter list is too long.

Do you know how to get me around this?
8 REPLIES
Tim Nelson
Honored Contributor

Re: chmod command on long list of files

If the files are all files in a directory try

chmod -R xxx /my/directory

This will change the directory and all files in it.

Or.

find /my/directory -type f -exec chmod xxx {} \;

SKR_1
Trusted Contributor

Re: chmod command on long list of files

do chmod permissions * (inside that directory)

Thanks

SKR
Steven Schweda
Honored Contributor

Re: chmod command on long list of files

> When I do the chmod command [...]

This is where it would have been helpful to
reveal the actual failing chmod command.

> do chmod permissions * (inside that
> directory)

That might help, but other non-"*" methods
would be safer. (And, for all we know,
that's exactly what you did to get the
original failure.)
James R. Ferguson
Acclaimed Contributor

Re: chmod command on long list of files

Hi:

If you want to only act on the files in the directory and not any of the subdirectories, another way is:

# find /path -xdev -type f -print|xargs chown newuser:newgroup

This avoids the potential for an "argument list too long" and avoids forking a separate task for every argument processed.

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: chmod command on long list of files

Hi (again):

Oh, sorry 'chmod' not 'chown':

# find /path -xdev -type f -print|xargs chmod 644

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: chmod command on long list of files

All command lines are limited in maximum length. So it helps to understand what happens when you use * to choose the files. chmod has no idea what to do with *. To see this, try a chmod 644 "*" or chmod 644 \* -- both these techniques remove the special meaning of * from the shell. What the shell does is to replace the * with all the filenames that match the * character. So the names of all 999999 files were placed on the command line (until it overflowed).

Now you see the really big problems in managing massively large collections of files in a single directory. You cannot use simple * characters but must think carefully how to make sweeping changes to the files. You must use commands that can find the filenames without shell expansion. A good sysadmin will never allow this type of directory structure to be put into production.


Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: chmod command on long list of files

> A good sysadmin will never allow [...]

Where does a system manager get to make a
decision like that?

I'd say that a good system manager would
explain to the users the known problems
associated with such a directory structure,
including the easily anticipated "parameter
list is too long" complaints, and what to do
to avoid them ("find", "xargs", ...).

This allows the system manager to get a good
laugh whenever some here> has a problem of this type, while he
points to the published FAQ document
containing the explanation.
Dennis Handly
Acclaimed Contributor

Re: chmod command on long list of files

>Steven: This is where it would have been helpful to reveal the actual failing chmod command.

I'm surprised your 1950 vintage cloudy crystal ball can't figure this one out. ;-)

>JRF: # find /path -xdev -type f -print|xargs chmod 644

Or use -exec +:
# find /path -xdev -type f -exec chmod 644 {} +

>Tim: chmod -R xxx /my/directory

Good trick!
If you don't want to change the directory, save the permissions and then put them back later.