Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-16-2008, 04:49 PM   #1
Registered: May 2007
Posts: 404

Rep: Reputation: 19
Can someone help me with a script please?

Hi There,

I've been running Apache 2.2.8 in cli mode for a few months on my vps. I've been running into some trouble with the way my webapps are accessing files as apache. Now I'm in the process of changing my installation to FastCGI w/ suexec. However, I just read something that said that I can't have > 755 on any files or directories.

I would like to run a script in my /home folder that will recursively go into each directory and reset any/all files/directories that have any permissions higher than 755. I would also like it to double check that the owner of the file is the same as the /home/username.

Can anyone tell me how to do this please?

Last edited by jim.thornton; 03-16-2008 at 04:53 PM.
Old 03-16-2008, 08:14 PM   #2
Registered: Sep 2004
Posts: 85

Rep: Reputation: 15
To set permissions use chmod which can be used recursively. For example, to set 755 for all directories and files inside /home/username you could run

chmod 755 -R /home/username
A similar command can be used to change the ownership rights to a specific user

chown username -R /home/username
If the owner of the directory is already username then it will remain unchanged.

If you are looking to preserve permissions if they are already lower than 755 then you will require a different solution. If you are looking to do this on many users a script may be better timewise, but for a single user this might be what you are looking for.
Old 03-16-2008, 08:30 PM   #3
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,896

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
To recursively test if the files are owned by the logged in user, try this:
for i in /home/<username>/*; do
   if ! test -O $i ; then
      echo "$i not owned by $USER"
You will get a list (in console) of any files not owned by the user. I leave it to use to modify the script to change ownership of found files, if you want ownership to be changed.
Old 03-17-2008, 12:48 AM   #4
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
about reseting, try this one:
find $path -perm /022
that will list the files
to reset:
chmod 755 $(find $path -perm /022)
and to exclude listing of files in the subdirectories, add the option '-maxdepth 1'
do 'man find' for more info about the command
you can also use the find command to find files with a different user

to check and change the user try this code:

# this will list only
find ${homedir} -not -user ${user}
# this will change
chown ${user}:${group} $(find ${homedir} -not -user ${user})

Last edited by konsolebox; 03-17-2008 at 12:57 AM.
Old 03-17-2008, 01:16 AM   #5
Registered: May 2007
Posts: 404

Original Poster
Rep: Reputation: 19
Thanks to all... Someone on another forum suggested not using just the find command.

It worked perfectly! Thank you for the suggestions though!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 03:41 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 05:19 AM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 10:10 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:03 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration