Script to generate csv file

Hello;

I need to generate a csv file that contains a list of all the files in a particular server (from the root directory ie: \) that have a permission stamp of 777. I would like to create the csv so that it contains the following:

server name, file name, full path name where file exists, owner of file, group file belongs too

I am aware of the commands to get this information at the prompt or even spit it out via a script; what I do not know is how to read the information from each command and create a csv file.:confused:

Help would be graciously appreciated.

Thanks:)
G

try this... you may have to format the output... also "find" command has option to specify the permission during search... good luck!

find . | xargs stat

regards,
Ahamed

This should work:

hname=$(hostname)
find / -type f -perm 777 -ls | \
while read a b c d user group e f g h file
do
    echo "$hname;$(basename "$file");$(dirname "$file");$user;$group"
done

It can be more efficient without running basename and dirname, if you are running ksh/bash. That will avoid a lot of 'forking' if you are running through a lot of files.

echo "$hname;${file##*/};${file%/*};$user;$group"

Just some more info I should have added .... not using BASH or KSH, just CSH...OS is HP-UX. Hope this helps.

Pludi
When I ran the command "find / -type f -perm 777 -ls | \ " at the prompt to see what it would do; I get a return for a new line; see below:

[dslgvol@pike ./]$ find / -type f -perm 777 -ls | \
>

That new line is expected. The \ tells the shell that command continues on next line. Please run the entire set of code that pludi gave above.

Pludi:
When I try and run it with a script I get:

[dslgvol@pike ./dslgvol]$ sh testscript.sh
find: bad option -ls
[dslgvol@pike ./dslgvol]$

ahh yes, on HPUX that -ls option on find is missing. Stupid HPUX :).

Let me see if I can edit that code from pludi to run on hpux.

Funny though; when I do a "man find" it shows the "-ls" option.

Is the "man" just a "show all options but some may not work on your OS" listing?

Also; in the code; where am I actually creating the CSV file? I was thinking that a script to do what I am looking to do would be much larger, not that I would take a lrage script over a short one; just that is seems so streamlined...if it actually does what I need it to do.

G

Here you go.

find /tmp -type f -perm 777 -exec ls -al {} \;|while read a b c d e f g h 
do 
filepath=`echo $h|awk '{print $2}'` 
filename=`echo $filepath|awk -F "/" '{print $NF}'` 
echo "`hostname`;$c;$d;$filename;$filepath" 
done 

1 Like

Super; I will give it a try a little later.

Will get back to you via this forum either way.

Regards
G

To create the csv file, you would need to redirect the last echo statement to a filename. So instead of spitting out on the terminal, it will redirect output to a file. But you can redirect, once you are satisfied with that you see on screen.

---------- Post updated at 10:27 AM ---------- Previous update was at 10:25 AM ----------

and remember to change find /tmp to whatever directory you need to search. I hardcoded /tmp in my code, since I was testing against /tmp.

Thanks dude2cool;

I will be working on it now. Will let you know.

Thanks to all for your advise\input; greatly appreciated.

Regards
G

---------- Post updated at 03:19 PM ---------- Previous update was at 01:07 PM ----------

Hey dude2cool;

from a standpoint of content of a line (see below ex.); it's fine:
pike;root;sys;bat00125.log;/tmp/bat00125.log

However; I put the "redirect to a file" option in (see below):
echo "`hostname`;$c;$d;$filename;$filepath" >testfile

But the contents of the "testfile" when I cat it is one record. I tried putting in a /n like this:
echo "`hostname`;$c;$d;$filename;$filepath \n" >testfile

But no luck.

Any ideas? Also; can I replace the ";" in the record with ","?

Regards & thanks
Giuliano

@dude2cool: Thanks for fixing it up. Although I think that both dirname and basename are shell builtins, but I could be wrong.

@gvolpini: A single bracket (>) will overwrite the file on each successive iteration. Double them to append instead (>>)

Oh...did not think of the >>; will give it a try...thanks to both of you for your time and effort...I will let you know how I progress on this one.

Sincere regards
Giuliano

Running a shell builtin in backticks or $() still forks another shell I think.

I don't think so. Tried this snipped with both bash and ksh93, both reported the same PID. Unless I made a logical error here...

#!/bin/bash

echo $$
echo $( eval 'echo $$' )

You still get the same PID even when you force /bin/echo. $$ is the parent shell's PID.