Solved

Linux openSUSE 12 - Folder listing into a report

Posted on 2014-03-05
13
265 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

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 …
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…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
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 …
Suggested Courses

623 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