Solved

Linux openSUSE 12 - Folder listing into a report

Posted on 2014-03-05
13
257 Views
Last Modified: 2014-03-06
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?
0
Comment
Question by:bax2000
  • 7
  • 6
13 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39905937
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".
0
 

Author Comment

by:bax2000
ID: 39905944
@woolmilkporc

Thanks that does seem to make it neater.
How do I add the folder owner and group?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39905953
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.
0
 

Author Comment

by:bax2000
ID: 39905980
@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?
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
ID: 39906007
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").
0
 

Author Comment

by:bax2000
ID: 39906019
Perfect thanks .. just what I needed.

Now how can I pull a list of groups and users in each group?
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39906024
Sorry, I don't understand what you're after. Please explain!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39906054
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]}'
0
 

Author Comment

by:bax2000
ID: 39906080
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.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39906303
Newer Linux distros have "groupmems":

groupmems -g groupname -l
0
 

Author Comment

by:bax2000
ID: 39906332
Our server does not recognize the groupmems command.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 39906513
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

0
 

Author Closing Comment

by:bax2000
ID: 39909242
Thanks for the help. Most appreciated.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
My bash alias isn't executing 5 47
Removing DES and 3DES ciphers in linux RedHat 6.8 38 94
AWS vpc peering connection 1 42
Linux hostname change 2 52
Meetings to discuss business process can waste time, and often do .  The meeting's dialog can get confusing when participants have different professional perspectives and backgrounds.  A jointly-developed process picture helps wade through the confu…
A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now