anoopb anoopb - 1 year ago 56
Linux Question

getting the output of "find" in a CSV

I've got a project where I'm trying to collect all the files on a linux system into a CSV to be dumped into a DB.

my script has

FIND_CMD $TARGET $OPTS -xdev $FS -printf "%h,%f,%y,%s,%TY-%Tm-%Td %TH:%TM:%.2TS\n"

But the problem is that I have filenames
and directory names
with commas in them.

So then I thought I could just put each item in quotes and that won't be a problem.

$FIND_CMD $TARGET $OPTS -xdev $FS -printf "\"%h\",\"%f\",\"%y\",\"%s\",\"%TY-%Tm-%Td %TH:%TM:%.2TS\"\n"

But then I discovered that I have filenames with quotes in them :(

So i'm trying to figure out how I can sub " with "" with quotes around the whole thing.


"/home""dir","file,name","f","1024","2016-11-04 11:11:00"

So i'm wondering if there's a way for me to call
as part of
so i can tell the find command to replace "s with ""s

My other thought was to replace the delimiter in my find command with something else like | but i'm likely to run into the same problem if a filename or directory name has a | in (which is possible)

Are there any other creative solutions to this that i may not be thinking of?

Answer Source

Consider using NUL as a delimiter in your CSV since that is not a valid character in a Linux filename/pathname - Wikipedia refernce.

That is also the reason that the find command provides -print0 and that tools such as Perl and GNU Parallel have corresponding switches giving rise to:

find . -name '*.orig' -print0 | perl -n0e unlink


find . -print0 | parallel -0 ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download