Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Linux openSUSE 12 - Folder listing into a report

Posted on 2014-03-05
13
Medium Priority
?
266 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
Turn your laptop into a mobile console!

The CV211 Laptop USB Console Adapter provides a direct Laptop-to-Computer connection for fast and easy remote desktop access with no software to install.

 

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Suggested Courses

704 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