[CentOS] check permissions of all my files! -- find example, short script

Bryan J. Smith

b.j.smith at ieee.org
Tue May 17 20:52:47 UTC 2005


On Tue, 2005-05-17 at 14:39 -0500, israel.garcia at cimex.com.cu wrote:
> List, How can I check the permissions of all the files of my CentOS
> server?  I'm looking for some kind of report with this information..
> Is there some command? Some tool?

Here is a command that will give you the entire list of all files on
your system in the format:
  "user:group mode path"

$ find / -printf "%u:%g %m %p\n"

Note that it _will_ cross filesystems, including any NFS mounts.
Use the "-mount" option to keep it from crossing filesystems.

If you want to create a script to reapply all standard UNIX permissions
to all files in a tree, here is a script (saveperm.sh) that outputs such
a script:  

#!/bin/bash
mydir="${1}"
if [ "${mydir}" == "" ]; then
	echo "Syntax:  "
	echo "  saveperm.sh (path)"
	echo "NOTE:  saveperm.sh does not cross filesystems"
	echo "NOTE:  saveperm.sh does not handle spaces in filenames"
	exit 127
fi
if [ ! -d "${1}" ]; then
	echo "${1} is not a directory"
	exit 127
fi
echo "#!/bin/bash"
echo "# Created by saveperm.sh"
echo "# Run from `pwd` targeting ${mydir}"
echo ""
for mypath in `find ${mydir} -mount`; do
	find "${mypath}" -maxdepth 0 -printf "chown %u:%g %p\n"
	find "${mypath}" -maxdepth 0 -printf "chmod %m %p\n"
done

You'd typically run it with (examples):  

$ saveperm.sh /abspath > /perms_abspath.sh
[ save absolute path /abspath to a script /perms_abspath.sh ]

$ saveperm.sh . > ../perms_subdir.sh
[ save perms on current directory to script perms_subdir.sh in parent ]

The above script outputs (echos) a script to the screen.
You can redirect that into a file for execution later.

Note that the above script will NOT handle spaces in filenames!
I have found different bash versions to be exceedingly difficult
in handling spaces in filenames when returning from a find.
Setting IFS="\n" typically doesn't work well or consistently.
I would use an equivalent tcsh script instead of dorking with bash.**

-- Bryan

**NOTE:  Ed Schaffer keeps bothering me for a script for his column,
so I might just have to just write the tcsh version.  ;->


-- 
Bryan J. Smith                                 b.j.smith at ieee.org 
----------------------------------------------------------------- 
Beware of those who define their preference in terms of hate of
another option, and not on the positive merits of their selection





More information about the CentOS mailing list