I have a python script that reads in input files from a text file. Ideally the text file should have one line per directory and that line should have all the files in that directory separated by a comma and with the full path, so something like this: Line 1: Directory1/fileA,Directory1/fileB Line 2: Directory2/fileC,Directory2/fileD,Directory2/fileE I have been using sublime text to do this but my collaborators don't use sublime text so they want a command to make this text files. I've been trying with ls -m but I get just one line with all the directories and all the files. Help please!
Asked
Active
Viewed 28 times
1 Answers
0
with bash: iterate over the directories, store the filenames in an array and then print out the array elements joined with a comma
shopt -s nullglob
for d in */; do
files=("$d"*)
((${#files[*]} > 0)) && (IFS=,; echo "${files[*]}")
done
I assume you don't want to see directories that have no files.

glenn jackman
- 238,783
- 38
- 220
- 352