I would like to join the result of ls -1
into one line and delimit it with whatever i want.
Are there any standard linux commands i can use to achieve this?
I would like to join the result of ls -1
into one line and delimit it with whatever i want.
Are there any standard linux commands i can use to achieve this?
If you version of xargs supports the -d flag then this should work
ls | xargs -d, -L 1 echo
-d is the delimiter flag
If you do not have -d, then you can try the following
ls | xargs -I {} echo {}, | xargs echo
The first xargs allows you to specify your delimiter which is a comma in this example.
EDIT: If your delimiter is a comma then the following also works
ls -m
Ah, the power and simplicity !
ls -1 | tr "\\n" ","
Change the "," to whatever you want.
To avoid potential newline confusion for tr we could add the -b flag to ls:
ls -1b | tr '\n' ';'
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):
files=($(ls -1))
list=${files[@]/%/,}
list=${list%*,}