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.
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.
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.
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
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 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 ","?