Perl change ownership for directory paths

711 views Asked by At

I am trying to change the ownership of the list of directory such that

chown -LR deep:deep deep/list/of/50/subdirectories
chown -LR deep:deep deeps/list/of/50/subdirectories

But I am getting error for more than 10 sub directories. I thought of writing a perl script such that it is in the same folder as the deep and deeps level. I got to learn about File::path module in perl. But there are only make and rmtree functions in to. Please can somebody help regarding this problem. Thanks.

2

There are 2 answers

1
Barmar On BEST ANSWER

it sounds like you're running into the fact that Unix has a limit on the length of pathnames. If you're trying to access a deeply nested directory, the pathname will be too long.

You can get around this by cd'ing into each directory a few levels at a time:

( cd deep/list/of; cd some/more/levels; cd and/a/few/more; ...
  chown -LR deep:deep subdirectories )
( cd deeps/list/of; cd some/more/levels; cd and/a/few/more; ...
  chown -LR deep:deep subdirectories )

The parentheses are so that all the cd commands take place in a subshell; when it's done, you're returned to the original shell, starting in the same top-level directory, so you can then process the next subdirectory from there.

The default limit on pathname size is in <limits.h>:

#define PATH_MAX        4096    /* # chars in a path name including nul */

although individual filesystems may override this.

0
brian d foy On

If you want to do this in Perl, you'll have to walk the directories yourself, with something like File::Find. You can specify whether the code changes into the new directory (so the relative path is shorter).