Output large volume of data to CSV file

I have a program that output the ownership and permission on each directory and file on the server to a csv file. I am getting error message
when I run the program. The program is not outputting to the csv file.

Error:
the file access permissions do not allow the specified action
cannot change directory
xrealloc: cannot allocate 9900416 bytes

Here is my code

#!/bin/bash
monitorf=/sc/sb/monitor.csv
ls -l $(find /sasem/* -type f) > "$monitorf"
mail -a /sc/sb/monitor.csv -s "monitor" jgk@yahoo.com; billygm@yahoo.com


replace:

ls -l $(find /sasem/* -type f) > "$monitorf"

by:

find /sasem/* -type f | while read -r file; do ls -l "$file" >> $monitorf;done
  1. The output from ls -l is a text file; not a CSV file.
  2. The "cannot access directory" errors can only be fixed by running the find command as a user who has permission to read and search /sasem and all of its subdirectories.
  3. It is not clear where the memory allocation error is coming from. If mail can't handle the 9.9+Mb attachment you are trying to attach to your mail; there is nothing we can do to help you.
  4. You said you are making a "a program that output sic the ownership and permission on each directory and file", but your find command only looks at regular files; not directories.

If you are running as a user with permission to read and search the file hierarchy rooted in /sasem , you want to list all files in your output (not just regular files), and mail can handle the size of the attachment you are creating, the following should work:

#!/bin/bash
monitorf=/sc/sb/monitor.txt
find /sasem -exec ls -ld "{}" + > "$monitorf"
mail -a "$monitorf" -s "monitor" jgk@yahoo.com; billygm@yahoo.com

If you don't have permission to search and read that file hierarchy, there is nothing we can do for you other than suggest that you get permission to run this script as a user who has access. If the attachment is too big for mail to handle, you might be able to use a different tool to enqueue your mail, but mail transport protocols also have message size limits imposed by servers on your machine, the receiving users' machines, and any server between them that will be used to forward your email messages.