Skip to content
Advertisement

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:%.2TSn"

But the problem is that I have filenames %f and directory names %h 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.

Example:

“/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 sed as part of -printfso 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?

Advertisement

Answer

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

and

find . -print0 | parallel -0 ...
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement