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?
Adding on top of majkinetor's answer, here is the way of removing trailing delimiter(since I cannot just comment under his answer yet):
Just remove as many trailing bytes as your delimiter counts for.
I like this approach because I can use multi character delimiters + other benefits of
awk
:This command is for the PERL fans :
Here 40 is the octal ascii code for space.
-p will process line by line and print
-l will take care of replacing the trailing \n with the ascii character we provide.
-e is to inform PERL we are doing command line execution.
0 means that there is actually no command to execute.
perl -e0 is same as perl -e ' '
You can use:
This replaces the last comma with a newline:
ls -m
includes newlines at the screen-width character (80th for example).Mostly Bash (only
ls
is external):Using
readarray
(akamapfile
) in Bash 4:Thanks to gniourf_gniourf for the suggestions.
Ah, the power and simplicity !
Change the comma "," to whatever you want. Note that this includes a "trailing comma"
Parsing
ls
in general is not advised, so alternative better way is to usefind
, for example:Or by using
find
andpaste
:For general joining multiple lines (not related to file system), check: Concise and portable “join” on the Unix command-line.