Solved

Linux openSUSE 12 - Folder listing into a report

Posted on 2014-03-05
13
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 

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
 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix process listing into CSV format 3 87
Zimbra on Amazon Linux help 7 161
can i read my emails on lamp ftp 4 71
Issue when using 'yum update' 4 25
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…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

752 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