Operating System - HP-UX
1833589 Members
5188 Online
110061 Solutions
New Discussion

Re: How to change the file & directory permission?

 
SOLVED
Go to solution
praveen..
Super Advisor

How to change the file & directory permission?

Hi,
In one directory, There are so many sub directories & files.

and under sub-directories, there are also so many sub-directories,


At present, all files & subdirectories mode is 777.

i want to change it 755 for sub-ditectories & 644 for files.

how can i do it because there are so many files & sub directories and i can not change it one by one.

Please suggest.

Thanks in advance
11 REPLIES 11
Jonathan Fife
Honored Contributor
Solution

Re: How to change the file & directory permission?

You could use two find commands -- one for directories and one for files.

find /parent/dir -type d -exec chmod 755 {} \;
find /parent/dir -type f -exec chmod 644 {} \;
Decay is inherent in all compounded things. Strive on with diligence
Calandrello
Trusted Contributor

Re: How to change the file & directory permission?

For I understood it, the command that voce needs and chmod - R x.x.x.x
Ivan Ferreira
Honored Contributor

Re: How to change the file & directory permission?

He must use Jonathan's solution because he wants differents permissions for files and directories.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor

Re: How to change the file & directory permission?

Hi Praveen:

If you want to recursively change the permissions and/or ownership of all files and directories beneath a directory, simply do:

# chmod -R 644 /pathname

...and/or:

# chown -R root:sys /pathname

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: How to change the file & directory permission?

Hi (again) Praveen:

Yes, I should read better. Jonathan is correct --- you need to handle directories and files separately. *However* it is much more efficient to do:

# find /path -xdev -type f | xargs chmod 644

# find /path -xdev -type d | xargs chmod 755

Note htat the '-xdev' option prevents 'find' from traversing mountpoints.

The use of 'xargs' instead of '-exec' means that 'xargs' wil bundle many arguments into one execution at a time (automatically!). Instead of creating a separate process for every file or directory 'find' finds, you will create substantially fewer, thereby *greatly* speeding up your execution time. Your users will thank you!

Regards!

...JRF...
praveen..
Super Advisor

Re: How to change the file & directory permission?

Hi All,
I think Jonathan & lvan are right,

james: I dont want recursive change.

I want to change it 755 for sub-ditectories & 644 for files.

under sub directories, Again there are also so many sub-directories & files.

Should i go with these commadns?

#find /parent/dir -type d -exec chmod 755 {} \;
#find /parent/dir -type f -exec chmod 644 {} \;

what is in last column ( is it \-slash and ;- semi colon)

DCE
Honored Contributor

Re: How to change the file & directory permission?


Praveen,

If you want to be sure, run the following commands:
/usr/bin/find -type f -xdev -perm -0002 >>directory.list 2>/dev/null

/usr/bin/find -type d -xdev -perm -0002 >>file.list 2>/dev/null

This will create two files: one that lists all of the world writable directories, and one that lists all of the world writable files.

You can then review the contents for accuracy, and then add the following line to start of each line in the file
chmod o-w

Make the above script executable and run it to flip the world writable flag
James R. Ferguson
Acclaimed Contributor

Re: How to change the file & directory permission?

Hi Praveen:

Please understand that 'find' is *recursive*. That is, unless you skip an entity below the starting directory, 'find' will descend every sub-directory in the tree that you specify for it.

My second post, simply pointed out that you could handle directories and files independently, *and* that less resources would be used if you used a piped 'xargs' rather than an '-exec' with 'find'.

Regards!

...JRF...
Jonathan Fife
Honored Contributor

Re: How to change the file & directory permission?

Praveen --

James' commands will work more efficiently than the ones I posted -- especially if there is a very large number (thousands) of directories and files being changed. The -xdev flag will keep find from traversing different filesystems, so keep that in mind if you have filesystems mounted underneath the directory you are wanting to do this for.

As an aside, the characters at the end are indeed a backslash \ and a semi-colon ;
Decay is inherent in all compounded things. Strive on with diligence
Sandman!
Honored Contributor

Re: How to change the file & directory permission?

imho you can do it in one find(1) command as:

# find . -type f -exec chmod 644 {} \; -o -type d -exec chmod 755 {} \;

however for performance it is better to pipe the output to xargs as the man page for find(1) says...excerpt below.


Change permissions on all regular files in a directory subtree to mode 444, and permissions on all directories to 555:

find pathname -type f -print | xargs chmod 444
find pathname -type d -print | xargs chmod 555

Note that output from find was piped to xargs(1) instead of using
the -exec primary. This is because when a large number of files
or directories is to be processed by a single command, the -exec
primary spawns a separate process for each file or directory,
whereas xargs collects file names or directory names into
multiple arguments to a single chmod command, resulting in fewer
processes and greater system efficiency. The + delimiter for the
-exec primary can be used to achieve the same efficiency.
Bill Hassell
Honored Contributor

Re: How to change the file & directory permission?

Just so everyone knows:

chmod -R is one of the three MOST DANGEROUS commands in UNIX. This is because everyone forgets that specific permissions are required for both functional as well as secure operations. chmod -R changes everything and you can read here in the forums several stories of how one command destroyed the entire computer operation. Never use chmod -R unless there is no other way (and there is always another way).

The 3 most dangerous (root) commands (in order of popularity for catatrophes):

1. rm -rf
2. chmod -R
3. dd


Bill Hassell, sysadmin