14

I have a directory that has ~50k directories and ~1m files.

I want to change owner (or permission) of all files, but not the directories. This is because i'm trying to SCP additional files from a remote server, (~150k directories and ~3.6m files).

The directory I have is a part of the remote directory, but the scp failed...

If i run SCP again, it will start over by overwriting the files i already copied from the remote directory.

0

2 Answers 2

20

You can use find, it has an added advantage that the ARG_MAX will not be triggered in the process. From the parent directory:

find . -type f -exec chown newowner {} +

For chmod:

find . -type f -exec chmod 644 {} +

-type f will find only files.

2
  • 1
    Works really great! Thanks! +1 for both chmod and chown Oct 15, 2015 at 6:47
  • 2
    So find does not trigger ARG_MAX? I always thought it would have. You always learn something :)
    – kos
    Oct 15, 2015 at 13:18
5

Another alternative is to use xargs. You'll need to use the -print0 option with find and a corresponding -0 option of xargs:

find . -type f -print0 | xargs -0 chown newuser:newgroup

From man find:

-print0

print the full file name on the standard output, followed by a null character. This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs.

xargs will also correctly handle ARG_MAX.

2
  • 2
    find..exec does handle weird file names..what makes you think it does not?
    – heemayl
    Oct 15, 2015 at 10:33
  • 1
    @heemayl I ran some tests and it seems you're right. I stand corrected. Thank you for pointing it out, I updated my answer.
    – A.P.
    Oct 15, 2015 at 10:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .