Change owner of files recursively, but not directories

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.


Solution 1:

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.

Solution 2:

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.