LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-04-2010, 07:28 PM   #1
linux_kv
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Rep: Reputation: 0
writing directory info


I have a directory structure in my system (linux) as shown below


/test/
---/test/stest1/
------/test/stest1/stest1_t2
---------/test/stest1/stest1_t2/stest1_t2_t1
---/test/stest2/
------/test/stest2/stest2_t2
---------/test/stest2/stest2_t2/stest2_t2_t1

out put should be like

directorynamewner:groupermissions

E.g.
I have two owners steve and scott
I have one group msngrp
I want to write output as below


/test/:steve:msngrp:rwx:rwx:r:
/test/stest1/:steve:msngrp:rwx:rs:r:
/test/stest1/stest1_t2:steve:msngrp:rwx:rs:r:
/test/stest1/stest1_t2/stest1_t2_t1:steve:msngrp:rwx:rs:r:
/test/stest2/:scott:msngrp:rwx:rs:r:
/test/stest2/stest2_t2:scott:msngrp:rwx:rs:r:
/test/stest2/stest2_t2/stest2_t2_t1:scott:msngrp:rwx:rs:r:




Can any one please help me for this

Thank you
linux_kv
 
Old 12-04-2010, 07:41 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by linux_kv View Post
I have a directory structure in my system (linux) as shown below

Can any one please help me for this

Thank you
linux_kv
Help you with WHAT? You haven't asked a question.

If you mean you want a SCRIPT to do this, yes, we can HELP you with it...but we're not going to write it for you. Post what you've written, and where you're stuck, and we can help you. Otherwise, check Google...there are thousands of scripting tutorials that you can easily find.
 
Old 12-04-2010, 09:24 PM   #3
JDebianV
Member
 
Registered: Nov 2010
Posts: 36

Rep: Reputation: 1
test/ ls -l -R > text.file

Will provide the output in long list format and lists subdirectories recursively- including permissions, group and ownership - not exactly in the same order you listed - output to a "text.file" -

Is this info. helpful?
 
Old 12-05-2010, 10:44 AM   #4
linux_kv
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Please find my script. recursive function is not working correctly(ending). If I don't use recursively it is working fine.
Please correct my mistake.



#!/bin/ksh

fdirinfo()
{
if [ "$1" != "" ] #if parameter exists, use as base folder
then cd "$1"
fi
cpath=$(pwd)
a=$(ls -R)
for files1 in $a; do

#echo "filename111= $filename1"

filename1=$(echo $files1|sed -e 's/[^-][^\/]*\///g')

filetype1=$(ls -l | grep "$filename1$" | awk '{print substr($1,1,1)}')

#echo "filename222= $filename1 filetype= $filetype1 ls -l | grep $filename1 | awk '{print substr($1,1,1)}'"

if [ "$filetype1" = "d" ]
then

owner1=$(ls -l | grep "$filename1$" | awk '{print $3}')
ownerpermission1=$(ls -l | grep "$filename1$" | awk '{print substr($1,2,3)}')
ownerpermission1=$(echo $ownerpermission1|sed -e 's/-//g')

group1=$(ls -l | grep "$filename1$" | awk '{print $4}')
grouppermission1=$(ls -l | grep "$filename1$" | awk '{print substr($1,5,3)}')
grouppermission1=$(echo $grouppermission1|sed -e 's/-//g')

otherpermission1=$(ls -l | grep "$filename1$" | awk '{print substr($1,8,1)}')

echo "$cpath/$filename1:$owner1:$group1:$ownerpermission1:$grouppermission1:$otherpermission1:"
#fdirinfo filename1
cd $cpath
fi
done
}
fdirinfo $1
 
Old 12-06-2010, 10:24 AM   #5
overlook
LQ Newbie
 
Registered: May 2006
Location: Sweden
Distribution: Custom
Posts: 12

Rep: Reputation: 1
Quote:
Originally Posted by linux_kv View Post
Please find my script. recursive function is not working correctly(ending). If I don't use recursively it is working fine.
Please correct my mistake. [..]
Ok, that seems a little complicated. Try this one instead:
Code:
#!/bin/bash

if [ "${#}" -eq 1 -a -d "${1}" ]; then
  for f in $(find ${1} -type f) ; do
    stat --format='%n:%U:%G:%a:' ${f}
  done
else
  printf "Usage: ${0##*/} [directory]\n"
fi
The above assumes you only want to have files listed in your output and not directories (remove "-type f" from find if you want to include directories). Also, it's probably better to use octal modes (permissions) when scripting. Keep in mind that you really don't need use separate fields for $ownerpermission1:$grouppermission1 because an octal value like the one in the above script already contains both (a file with mode 644 equals user=r+w,group=r,world=r).

Last edited by overlook; 12-06-2010 at 11:08 AM. Reason: grammar, and some additional notes
 
Old 12-06-2010, 11:00 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Take a look at the -printf predicate of find, e.g.
Code:
find /path/to/base/dir -type d -printf "%p:%u:%g:%M\n"
 
Old 12-06-2010, 11:37 AM   #7
overlook
LQ Newbie
 
Registered: May 2006
Location: Sweden
Distribution: Custom
Posts: 12

Rep: Reputation: 1
Quote:
Originally Posted by colucix View Post
Take a look at the -printf predicate of find, e.g. [..]
Very useful, thanks! Didn't realize find had this feature. So the solution to his problem is reduced to a single command, awesome .. and the final script would be (with line-feed and octal modes):
Code:
#!/bin/bash

if [ "${#}" -eq 1 -a -d "${1}" ]; then
  find ${1} -type f -printf "%p:%u:%g:%m\n"
else
  printf "Usage: ${0##*/} [directory]\n"
fi
 
Old 12-06-2010, 06:23 PM   #8
linux_kv
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for the replies I will try your suggestions however I fixed my script
I made a mistake fdirinfo "filename1" instead of fdirinfo "$filename1"
here is my script


dirName="."

function fdirinfo()
{
if !(test -d "$1") #check the parameter is directory
then echo $1; return;
fi

cd "$1" #change to directory

for filename1 in * #loop trhough all the files
do
if test -d "$filename1" #if directory
then

owner1=$(ls -l | grep "$filename1$" | awk '{print $3}') #get the owner
ownerpermission1=$(ls -l | grep "$filename1$" | awk '{print substr($1,2,3)}') #get the permissions
ownerpermission1=$(echo $ownerpermission1|sed -e 's/-//g') #replace any -

group1=$(ls -l | grep "$filename1$" | awk '{print $4}') #get the group
grouppermission1=$(ls -l | grep "$filename1$" | awk '{print substr($1,5,3)}') #get the permissions
grouppermission1=$(echo $grouppermission1|sed -e 's/-//g') #replace any -

otherpermission1=$(ls -l | grep "$filename1$" | awk '{print substr($1,8,1)}')

echo "$(pwd)/$filename1:$owner1:$group1:$ownerpermission1:$grouppermission1:$otherpermission1:" #make string


fdirinfo "$filename1" #call recursively
cd .. #change directory to previous one
fi
done
}




if [ "$1" != "" ] #if parameter exists, use as base folder
then dirName=$1
fi


fdirinfo "$dirName"
 
Old 12-13-2010, 09:30 PM   #9
linux_kv
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Original Poster
Rep: Reputation: 0
I used your suggestions and wrote



find $dirName \( ! -regex '.*/\..*' \) -type d -printf "%p:%u:%g:%M:\n" | grep -v "lost+found" | sed -e 's/:/|/3'| awk '{print substr($0,0,index($0,"|")-1) ":" substr($0,index($0,"|")+2,3) ":" substr($0,index($0,"|")+5,3) ":" substr($0,index($0,"|")+8,3) }' | sed -e 's/-//g' -e 's/:$//' | awk '{print $0 ":" } ' >$2



#this command give output in a specific format (dir with fullpath : owner:groupwnerpermissions:grouppermissions and redirects to a file
#find command gets all the directories (non hidden) and printf prints path user group and permssions
#grep command ignores lost+found directory
#sed replaces third : as |
#awk prints anything before | and substring of remaining portion
#sed replace any -
#and replaces if last char is : this is required because sometimes replace all the - will leave :
#awk prints all the output and also : at the end
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CD formats, writing speed and software:general info cad Linux - General 1 02-15-2007 01:00 AM
Gnomebaker not writing track info saravkrish Linux - Software 1 02-11-2007 10:18 PM
Writing PDF meta info senyahnoj Programming 1 10-05-2006 05:56 AM
info problem "info: dir: No such file or directory" EAD Linux - Software 0 03-22-2006 02:16 PM
Info on technical writing tools madhup General 0 07-15-2003 12:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration