chmod command on long list of files (532 Views)
Reply
Advisor
Records Management
Posts: 47
Registered: ‎01-11-2005
Message 1 of 9 (532 Views)

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?
Honored Contributor
Tim Nelson
Posts: 3,727
Registered: ‎06-01-2000
Message 2 of 9 (532 Views)

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 {} \;

Trusted Contributor
SKR_1
Posts: 494
Registered: ‎03-25-2008
Message 3 of 9 (532 Views)

Re: chmod command on long list of files

do chmod permissions * (inside that directory)

Thanks

SKR
Honored Contributor
Steven Schweda
Posts: 9,089
Registered: ‎02-23-2005
Message 4 of 9 (532 Views)

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.)
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 9 (532 Views)

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...

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 9 (532 Views)

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...
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 7 of 9 (532 Views)

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.
Honored Contributor
Steven Schweda
Posts: 9,089
Registered: ‎02-23-2005
Message 8 of 9 (532 Views)

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.
Acclaimed Contributor
Dennis Handly
Posts: 25,058
Registered: ‎03-06-2006
Message 9 of 9 (532 Views)

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.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.