Shell script that lists files with different owner than the folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script that lists files with different owner than the folder
# 1  
Old 11-26-2011
Shell script that lists files with different owner than the folder

Hello, I'm trying to write a script which is listing files based on different preferences, like filetype or permissions. All is fine, except for one: I want to list files in /home which has a different owner than the home directory it is in.

Here is an example:
/home/UserA is the directory, and /home/UserA/foobar.txt is the file, that is owned by UserB.
If I would have to use a fixed user, this wouldn't be a problem, as I could just do

Code:
find /home/UserA \! -user UserA -exec echo "{}" is not owned by UserA \;

But I don't know, how can I use variables, that modifies the output and the checking based on who's home directory (for a /home/UserB/foobar.txt owned by UserA scenario) it is in. I was thinking about using awk to get the owner of the home directory and the file and compare it, but it doesn't feel right.
Basically I just have to find a way, to cycle through the home directories and replace the 3 UserA in my script, like this:

Code:
/home/UserA
/home/UserB
/home/UserC

find /home/UserA \! -user UserA -exec echo "{}" is not owned by UserA \;
find /home/UserB \! -user UserB -exec echo "{}" is not owned by UserB \;
find /home/UserC \! -user UserC -exec echo "{}" is not owned by UserC \;

I would appreciate any idea.
# 2  
Old 11-26-2011
Something like this?
Code:
#!/bin/bash

DIR="/home/UserA /home/UserB /home/UserC"
for dir in $DIR
do
  user=$( basename $dir )
  find $dir ! -user $user -exec echo "{}" is not owned by $user \;
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 11-26-2011
Yes, like this, but my main problem is processing the folderlist in /home, because the script should work in any enviroment, not just with predefined names.

This:
Code:
cd /home
ls -l | awk '{print $3}'

would get a owners list for me, or with
Code:
cd /home
ls -l | awk '{print $8}'

I could get a list of folders (and files, but this is irrelevant in this case), but as I mentioned my problem is how to precess and use that list.

Sorry, that I left this out of my original post. I also forgot to mention, that the search has to go through every file, no matter, how deep it is in the folder tree. This is why I used 'find'.

And before I forget, thank you for the help! :-)
# 4  
Old 11-26-2011
More generic would be to get all the users in the system and then search the home directories right?

--ahamed

---------- Post updated at 02:11 PM ---------- Previous update was at 01:51 PM ----------

Following script takes the users from /etc/passwd and searches their home directory for any files which they don't own. You definitely don't want to search any system accounts. So you can update the excp_list with the users you want to skip separated by pipe.
Code:
#!/bin/bash

excp_list="(root|daemon)"
awk -F: '{print $1,$6}' /etc/passwd | while read user home_dir
do
  test $(echo $user | egrep -c "$excp_list") -ge 1 && continue
  test -z "$home_dir" && continue
  find $home_dir ! -user $user -exec echo "{}" is not owned by $user \;
done

HTH
--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 11-26-2011
Well, I was thinking about getting a userlist with
Code:
cat /etc/passwd | grep /home | cut -d: -f1

but this is messy too (extra results), and does not solve my problem. By generic I meant, that the folders in /home are not called UserA, UserB, etc. which I used in the code here.
Code:
cd /home ls -l | awk '{print $8}'

works pretty well, I just have to find a way to insert the result into
Code:
DIR="/home/UserA /home/UserB /home/UserC"

this line, instead of UserA,...,etc.

-----------------------------EDIT-------------------------------------------

I liked your first script better as it was less complicated for me. :-)
Using the folderlist and owners from /home is perfectly fine (I don't have to think about shares and users without home, as the focus is on those who have a home directory) and I just have to list the files that are not owned by the owner of the home directory.
I just have to find a way, to make that list usable.

Last edited by Zwiebi; 11-26-2011 at 06:24 PM..
# 6  
Old 11-26-2011
Try this...
Code:
DIR=$( ls -d /home/* )

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 11-26-2011
WOW, thank you, that worked just perfectly!

I knew, it can be done very easily, I was just too amateur to find :-) it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute 10 sql files from a folder through sqlplus in shell script?

I am new to shell scripting and i want to know how to execute the *.sql files from a folder through sqlplus in shell script and files should be execute in sequentially one by one while execution if any ORA error it has to exit from sqlplus session 1) scripts from external folder 2) logs has... (1 Reply)
Discussion started by: sreekanth Reddy
1 Replies

2. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

3. UNIX for Beginners Questions & Answers

How to get script to create a new file that lists folder content sorted by size?

I have a script that sorts and processes unsorted files to newly created directories. Its working great, but I am trying to understand the leanest method to get the script to create an additional file within each newly created directory that: Contains a list of all files in the directory... (4 Replies)
Discussion started by: Braveheart
4 Replies

4. Shell Programming and Scripting

Shell script to copy files from on folder to another

I am trying to copy files with specific date and name to another folder. I am very new to shell scripting so i am finding it hard to do that. see the sample code i have written below. srcdir="/media/ubuntu/CA52057F5205720D/Users/st4r8_000/Desktop/office work/26 nov"... (13 Replies)
Discussion started by: Aqeel Abbas
13 Replies

5. Shell Programming and Scripting

How to unzip files from folder in shell script (ksh)?

I have a folder (C:\shellprg\input\) containing .CSV, .zip, .gz files. 1] I want to find all .zip/.gz files from folder (C:\shellprg\input\). 2] unzip/uncompress files into the same folder (C:\shellprg\input\) through shell script. I am using below commands for unzip files, unzip <filename>... (2 Replies)
Discussion started by: Poonamol
2 Replies

6. Shell Programming and Scripting

How to Process input files from folder in shell script?

Hi, I want to process all input files available into folder (C:\ShellPrg\InputFile\) Input files are abc.CSV , XYZ.zip (zip of CSV file), PQR.gz (zip of CSV file). I want to check the extension of file, If its .zip/.gz then need to unzip the file as .CSV I want to parse line by line of... (2 Replies)
Discussion started by: Poonamol
2 Replies

7. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

8. UNIX for Advanced & Expert Users

How UNIX admin set up this? how files of 744 of other owner can be removed by another owner?

Hi all, We have some files are under 744 permissions and the the owner is say owner1 and group1. Now we have another user owner2 of group2, owner2 can remove files of the owner1 and the permission of those files are 744, unix admin told us he did some config at his side so we can do that. ... (14 Replies)
Discussion started by: TheGunMan
14 Replies

9. Shell Programming and Scripting

HOW TO CHECK ONLY .C FILES EXISTS OR NOT IN A FOLDER using IF in C shell script?

Hi friends.. I hav a problem.... I dont know how to check .c files exists r not in a folder using IF in C shell script actually i tried like this if(=~ *.c) even though some .c files or there in the current folder..it is not entering int o the if control statement...... (17 Replies)
Discussion started by: p.hemadrireddy
17 Replies

10. Shell Programming and Scripting

Shell script to find out 2 last modified files in a folder..PLZ HELP!!!!!!!!!

hi all, I need to find out the last 2 modified files in a folder.There is some way by which,we can check the timestamp and find out..??please help this is urgent. Thanks in Advance Anju (3 Replies)
Discussion started by: anju
3 Replies
Login or Register to Ask a Question