LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with managing/calculating user files (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-managing-calculating-user-files-933743/)

alankru 03-10-2012 08:07 AM

Help with managing/calculating user files
 
Hi,

I am using CentOS to run a web server, but I believe that this is a simple newbie question so I hope that I am posting this in the correct forum.

I have a number of users with files owned by the user "nobody" in their home directories. Would someone be able to help me with the following please?

1. How would I go about finding the user's that have files in their home directories that are owned by "nobody"? I know that I can do

Code:

#find /home -user "nobody"
however, I would like to have a summary of the locations where these files are, rather than a verbose listing of every file. Is this possible?

2. A long time ago, I was given a command to scan a user's home directory and calculate the disk usage of their "nobody" files:

Code:

#find /home/directory -user nobody | xargs -i du -b "{}" | awk '{sum+=$1} END {print sum/1024/1024" MB"}'
However, when I run this command on an example user, it doesn't show the true disk usage of the files owned by the "nobody" user. Furthermore, if I use the same command to find files owned by the example user, it comes back with 6GB, which is not correct either as they have a quota of 800MB. So clearly something isn't right with that command.

Any help would be appreciated. Thank you.

allend 03-10-2012 08:41 AM

My suggestion for an amended command.
Code:

find /home/directory -type f -user  nobody -print0  | xargs -0 du -b | awk '{sum+=$1} END {print sum/1024/1024" MB"}'
The '-type f' option to find will only find regular files.
The '-print0' option to find together with the '-0' option to xargs will handle filenames with spaces.
The now deprecated '-i' option to xargs is superfluous (and wrongly had no replacement string specified in your original).

alankru 03-10-2012 09:00 AM

That works perfectly, thank you so much for your help! :)

Do you know what I can do about finding a listing of the directories where the "nobody" owned files are (across the entire /home drive)? Is this possible please?

EG. I run a command that gives me a listing like:

/home/user/folder/
/home/user/folder2/
/home/user/folder2/folder3

etc.

allend 03-10-2012 09:23 AM

Try this, although it may take a long time to execute.
Code:

find /home -type f -user nobody -printf "%h\n" | uniq

alankru 03-10-2012 11:00 AM

Thanks very much again allend, you have helped me out tremendously!

What I have now done is also pipe it to sort and redirect it to a file so that I can go through it:

Code:

find /home -type f -user nobody -printf "%h\n" | sort | uniq > nobodyfiles.txt
Thanks again! :D

David the H. 03-11-2012 12:36 AM

One final suggestion. "sort -u" (assuming gnu sort) can be used, making uniq unecessary.

alankru 03-12-2012 02:58 PM

Great, thanks for that improvement on mine.

What I needed for my purposes was to also get a list of the user's who had "nobody" files in their home directories. I was able to take the data from the earlier
Code:

find /home -type f -user nobody -printf "%h\n" | sort | uniq > nobodyfiles.txt
command and I then put it into a spreadsheet application to get a list of users. However, for anyone else looking at this thread, someone else suggested to me that the following command could be used (I didn't try it myself due to the amount of time it takes to execute):

Code:

find /home -user "nobody" | sed 's/^\/home\///' | awk -F"/" '{ print $1 }' | sort | uniq
This will list all users that have at least one file owned by nobody. Of course, the uniq command can be removed as per your post David the H.

One thing that I came across with the command
Code:

find /home/directory -type f -user  nobody -print0  | xargs -0 du -b | awk '{sum+=$1} END {print sum/1024/1024" MB"}'
was that if a user does not have any files in their home directory owned by the nobody user, then a value of "100.58 MB" gets returned. This is something to watch out for, but it didn't cause me a problem as I worked through each user manually from the user listing I got earlier. Therefore, I knew that the figures could be relied upon as there genuinely were nobody files in the user's home directory.

Thank you again for everyone's help.


All times are GMT -5. The time now is 06:43 AM.