Linux openSUSE 12 - Folder listing into a report

Barry Kay
Barry Kay used Ask the Experts™
on
I need to list all directories with permissions and pipe them to a file so that I can edit it into a readable report.
I don't have a GUI interface on the Linux server so need to do this via command line.
I have been able to run this command  "ls -l -R |grep ^d > list.txt"  but the output in the file is not neat and all.

How can I get this data neatly into a report?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2013
Top Expert 2013

Commented:
This obviously depends on your understanding of "neatly"  :-)

I'd suggest using "find" and its "printf" feature.
Here is an example showing just the permissions followed by the directory names:

find . -type d -printf "%M\t%p\n"

If your find implementation does not support "%M" try "%#m" which will show the permissions in octal notation.
To get a comma-separated list replace "\t" (= TAB) with a comma:

find . -type d -printf "%M,%p\n"

There are many more formatting directives for "printf". Please have a look at "man find" ("Actions - printf") or let me know if I didn't exactly meet your definition of "neatly".
Barry KaySystems Engineer

Author

Commented:
@woolmilkporc

Thanks that does seem to make it neater.
How do I add the folder owner and group?
Most Valuable Expert 2013
Top Expert 2013

Commented:
The owner is represented by "%u", the group by "%g":

find . -type d -printf "%M\t%10u %10g\t%p\n"

The (optional!) leading numbers ("10") are used to define a minimum field length for better column alignment.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Barry KaySystems Engineer

Author

Commented:
@woolmilkporc

Thanks getting there. Is there a way to put a comma "," between the columns so when I pipe it to a CSV file it will open in excel in columns?
Most Valuable Expert 2013
Top Expert 2013
Commented:
Yes, there is. I used TAB characters or spaces in my examples, but you can of course use commas:

find . -type d -printf "%M,%u,%g,%p\n"

Please note that I removed the minimum field length specifications. I think they don't make sense in CSV output.

By the way, the trailing "\n" indicates a "newline" character and is required, otherwise all the output would go into one single line.

You might want to enclose the directory name in double quotes, for the (unlikely but possible) case that a directory name would contain a comma:

find . -type d -printf "%M,%u,%g,\"%p\"\n"

Note that the double quotes must be escaped (\") to protect them from being stripped off by the shell.

Finally, if you don't like the "./" in front of each directory name use "%P" (uppercase "P") instead of "%p" (lowercase "p").
Barry KaySystems Engineer

Author

Commented:
Perfect thanks .. just what I needed.

Now how can I pull a list of groups and users in each group?
Most Valuable Expert 2013
Top Expert 2013

Commented:
Sorry, I don't understand what you're after. Please explain!
Most Valuable Expert 2013
Top Expert 2013

Commented:
Do you want to know how many directories are owned by a specific userid and how many directories are owned by a specific group id?

find . -type d -printf "%u %g\n" |awk '{U[$1]+=1; G[$2]+=1}
                                                           END {print "Owner\tCount";
                                                                     for(n in U) print n"\t"U[n];
                                                                    print "\nGroup\tCount";
                                                                     for(n in G) print n"\t"G[n]}'
Barry KaySystems Engineer

Author

Commented:
I'm looking on how to just list each security group and used that are members of that group. As I would see the groups when I used the YAST program.
hope that helps.
Most Valuable Expert 2013
Top Expert 2013

Commented:
Newer Linux distros have "groupmems":

groupmems -g groupname -l
Barry KaySystems Engineer

Author

Commented:
Our server does not recognize the groupmems command.
Most Valuable Expert 2013
Top Expert 2013
Commented:
awk -F: '{print $1,$3,$4}' /etc/group | while read gname gid memlist; do
 set $(awk -F: -v G="$gid" '{if($4~G) {print $1,$4;exit}}' /etc/passwd) >/dev/null
       uname=$1; pgid=$2
   if [[ $pgid = $gid ]] ; then memlist="$uname $memlist"; fi
   if [[ ! -z $memlist ]]; then printf "%-12s %-64s\n" "$gname" "$memlist"; fi
done

Open in new window

Barry KaySystems Engineer

Author

Commented:
Thanks for the help. Most appreciated.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial