Saturday, April 20, 2024
Homepage · ls
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 447  / 2 Years ago, wed, april 6, 2022, 5:39:01

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?


More From » ls

 Answers
1

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',
" *200px.svg

[#40] Friday, April 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rillrage

Total Points: 122
Total Questions: 120
Total Answers: 103

Location: Tokelau
Member since Thu, Aug 26, 2021
3 Years ago
rillrage questions
Sat, Apr 30, 22, 13:11, 2 Years ago
Wed, Mar 9, 22, 05:33, 2 Years ago
;