Friday, November 16, 2007

Changing the permissions only of the subdirectories in Linux

I use Linux as the operating system of my home computer for quite a long time now. I think it is a little bit more than 2 years, but I must confess I haven't mastered the command line yet. I do use the command line quite a lot, but my memory becomes weaker, so if I used something on the command line once or twice, I definitely don't remember it after some time. I thought of a solution to my problem. I will keep all these small tricks here, online, in my blog, as a "linuxlog"!

Well, today's bite sized tip is about changing the permissions of all the subdirectories of a directory, recursively, but only the directories and not the files within! The solution requires the use of the tools and find, xargs and chmod.

Here is the line you need:
find /path/to/directory/ -type d -print0 | xargs -0 chmod 755

First we use find to find all the subdirectories (-type d) within the directory (/path/to/directory/). The switch -print0 is used in order to separate the results with the null character instead of a new line. This way our command will work correctly even for directories with spaces in their name.

The result of the find is passed to xargs, which passed to chmod the results one by one. The switch -0 is used to inform xargs that the input items are separated by the null character and not a new line.

xargs executes the statement chmod 755 for every result that is read from find.

That's my first tip for the command line. Until next time, enjoy your linux operating system!

No comments: