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?
Although you can alter the formatting of
ls
output to some extent, for examplefor 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'sprintf
function:Additionally to the accepted answer, we can group three printf commands to generate complete JavaScript array file without the need to manual editing:
I placed the above command in an executable
listing.sh
file in the same directory for easier call later:?Notice: