Score:4

Is there any way to control ls output format

ng flag

I have a directory contains some svg images among its contents. I list them with following ls command to save the output in a file:

ls *200px.svg > animList.js

The output file looks like:

file1-200px.svg
file2-200px.svg
file3-200px.svg
...
fileX-200px.svg

I need the file name to be written like 'fileX-200px.svg', to easily make this files list working like JavaScript array. i.e. I need the output file to be:

'file1-200px.svg',
'file2-200px.svg',
'file3-200px.svg',
...
'fileX-200px.svg',

So at the end I just need to add something like var arr = [ at the beginning of the file and then add ]; at the end of the file to work as JavaScript array. Is there any way in Linux to control the output display of the ls command?

Score:6
hr flag

Although you can alter the formatting of ls output to some extent, for example

LC_CTYPE=C ls --quoting-style=locale *200px.svg

for single-quotes (see info coreutils ls 'formatting the file names' for details) it would be simpler just pass the results of the glob to the shell's printf function:

printf "'%s',\n" *200px.svg
ng flag
Wow! It means that `printf` is an abbreviation for `print files`!
hr flag
@SaidbakR it's more likely *print formatted* I think
Raffa avatar
jp flag
+1 Or `find`’s `-printf` action to avoid possible maximum arguments limitt.
hr flag
@Raffa I think that shouldn't be necessary so long as you're using the shell's built-in `printf` - see for example [Shell builtin `printf` line limit?](https://unix.stackexchange.com/questions/519789/shell-builtin-printf-line-limit)
Raffa avatar
jp flag
Got it :-) thanks … One wold have to exhaust all system memory resources accessible by this shell instance in order to reach a limit … Still a distant possibility but could happen … Thanks again … I didn’t know this before.
ng flag
@steeldriver but here it does not **only** format an input to it, it searches for files and listing them as well. However, what you have said about the abbreviation of *f* is true. It is about **f**ormat.
Raffa avatar
jp flag
@SaidbakR It doesn’t search … the shell glob character `*` is expanded by the shell to matching filenames that are then passed to `printf` which only formats and prints what it sees.
Score:1
ng flag

Additionally to the accepted answer, we can group three printf commands to generate complete JavaScript array file without the need to manual editing:

(printf "var arr =[\n" && printf "'%s',\n" *.svg && printf "];") >animList.js

I placed the above command in an executable listing.sh file in the same directory for easier call later:

#!/bin/bash
(printf "var arr =[\n" && printf "'%s',\n" *.svg && printf "];") >animList.js
echo "Done!"

Notice:

This is not an answer. It is only an addition to optimize the value of the question's solution in a practical aspect.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.