Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 656
JavaRocky Avatar asked May 04 '10 09:05

JavaRocky


People also ask

How do I combine multiple lines in one?

The paste command can merge lines from multiple input files. By default, it merges lines in a way that entries in the first column belong to the first file, those in the second column are for the second file, and so on. The -s option can let it merge lines row-wise.

How do you join two lines in Unix?

The sed way. 'N' joins 2 lines. And we replace the newline with a comma.

How can I add multiple lines to a file?

Method # 1 – Using echo & Printf The simplest way to append multiple lines to a file is to use the echo and printf command. Let us start with echo. Echo is a command used to output a string or multiple strings as arguments.


2 Answers

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" (for lists that end with a newline)

like image 41
zaf Avatar answered Sep 18 '22 14:09

zaf


Similar to the very first option but omits the trailing delimiter

ls -1 | paste -sd "," - 
like image 196
Artem Avatar answered Sep 18 '22 14:09

Artem