How to join multiple lines of file names into one with custom delimiter? How to join multiple lines of file names into one with custom delimiter? linux linux

How to join multiple lines of file names into one with custom delimiter?


Similar to the very first option but omits the trailing delimiter

ls -1 | paste -sd "," -


EDIT: Simply "ls -m" If you want your delimiter to be a comma

Ah, the power and simplicity !

ls -1 | tr '\n' ','

Change the comma "," to whatever you want. Note that this includes a "trailing comma"


This replaces the last comma with a newline:

ls -1 | tr '\n' ',' | sed 's/,$/\n/'

ls -m includes newlines at the screen-width character (80th for example).

Mostly Bash (only ls is external):

saveIFS=$IFS; IFS=$'\n'files=($(ls -1))IFS=,list=${files[*]}IFS=$saveIFS

Using readarray (aka mapfile) in Bash 4:

readarray -t files < <(ls -1)saveIFS=$IFSIFS=,list=${files[*]}IFS=$saveIFS

Thanks to gniourf_gniourf for the suggestions.