JVGen
JVGen

Reputation: 601

Print File Paths and Filenames within a Directory into a CSV

I have a directory that contains several files. For instance:

File1.bam
File2.bam
File3.bam

I want to create a .csv file that contains 2 columns and includes a header:

Name,Path
File1, /Path/To/File1.bam
File2, /Path/To/File2.bam
File3, /Path/To/File3.bam

I've managed to piece together a way of doing this in separate steps, but it involves creating a csv with the path, appending with the filename, and then appending again with the header. I would like to add both the filename and path in 1 step, so that there is no possibility of linking an incorrect filename and path.

In case it matters, I'm trying to do this within a script that is running in a batch job (SLURM), and the output CSV will be used in subsequent workflow steps.

find ~/Desktop/test -iname '*.csv' -type f >bamlist1.csv
awk '{print FILENAME (NF?",":"") $0}' *.csv > test.csv
{ echo 'Name, Path'; cat bamlist.csv; } > bamdata.csv

Upvotes: 1

Views: 154

Answers (1)

Ed Morton
Ed Morton

Reputation: 203607

Untested but should be close:

find ~/Desktop/test -name '*.bam' -type f |
awk '
    BEGIN { OFS=","; print "Name", "Path" }
    { fname=$0; sub(".*/","",fname); print fname, $0 }
' > bamdata.csv

Like your original script the above assumes that none of your file/directory names contain newlines or commas.

If you have GNU find you can just do:

{ echo "Name,Path"; find ~/Desktop/test -name '*.bam' -type f -printf '%f,%p\n' } > bamdata.csv

Upvotes: 3

Related Questions