Friday, July 29, 2022

[SOLVED] creating a list of files with file absolute path in linux

Issue

I have a large sum of files (~50000 files).

ls /home/abc/def/

file1.txt
file2.txt
file3.txt
.........
.........
file50000.txt

I want to create a CSV file with two columns: first column provides the filename and the second provides the absolute file path as:

output.csv

file1.txt,/home/abc/def/file1.txt
file2.txt,/home/abc/def/file2.txt
file3.txt,/home/abc/def/file3.txt
.........................
.........................
file50000.txt,/home/abc/def/file50000.txt

How to do this with bash commands. I tried with ls and find as

find /home/abc/def/ -type f -exec ls -ld {} \; | awk '{ print $5, $9 }' > output.csv

but this gives me absolute paths. How to get the output as shown in output.csv above


Solution

How about:

$ find /path/ | awk -F/ -v OFS=, '{print $NF,$0}'

Add proper switches to find where needed.



Answered By - James Brown
Answer Checked By - Mary Flores (WPSolving Volunteer)