LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   howto: ls output to csv format (https://www.linuxquestions.org/questions/linux-newbie-8/howto-ls-output-to-csv-format-808545/)

limdel 05-18-2010 03:22 AM

howto: ls output to csv format
 
Hi,

i wonder if it would be possible to redirect ls output to a .csv file format ?i want to have something like:

FilePath,FileName,ctime,Size,RightAccess

I check on the man page but didn't found something that look like what i wanted.

Thanks in advance.

alli_yas 05-18-2010 03:35 AM

Can you give an example of exactly how you want the output to look like?

The following is taken off another Linux site; but basically solves your question about writing to a CSV; all that's required is to modify the ls in order to show the output in your desired format:

Code:

# ls -go * | sed 's/[ \t]/,/g' > list.csv

# more list.csv
-rw-r--r--,1,364,2009-12-28,15:08,1
-rw-------,1,1682,2009-12-28,16:35,anaconda-ks.cfg

Cheers
Yas

catkin 05-18-2010 03:43 AM

Quote:

Originally Posted by limdel (Post 3972482)
i want to have something like:

FilePath,FileName,ctime,Size,RightAccess

Can you clarify RightAccess -- do you want owner, group and global? Do you want to report ACLs if any?

ashok.g 05-18-2010 03:58 AM

This will do the trick for you
Code:

ls -l | awk '{if(NR==1){print "FileName, ctime, Size, Access";}else{print  $9 "," $6 " " $7 " " $8 "," $5 "," $1;}}' > a.txt
Now, a.txt contains the data you want

colucix 05-18-2010 05:45 AM

Anoher option is the find command with the -printf action, that provides a high level of customization. For example:
Code:

$ find /home/user  -maxdepth 1 -type f -name R\* -printf "%h,%f,%CY-%Cm-%Cd %CT,%s,%M\n"
/home/user,README,2010-05-06 12:13:26,303,-rw-r--r--

You can use all the search criteria of find to match the desired files or directories and choose the format of the results at the same time.

limdel 05-18-2010 10:00 AM

Hi Guys,

Sorry to answer lat, i forget to enable email notification.

First of all thanks to all of you for the quick feedback, this website is just awesome !

Well, for more detail about my request:
-Columns order had no particular importance on the csv file that i'm expecting.
-Per "RightAccess", yes i do mean owner, group and global name and file permission for each of them.
-The output must output only files, no blank line or extra header nor footer.

For example:

While for an output i would get:
Code:

[lolo@triton ~]$ ls -l ~/Music
total 56196
-rw-rw-r--. 1 lolo lolo 5376128 2009-10-26 19:05 Dam-Cuoi-Dau-Xuan.mp3
-rw-rw-r--. 1 lolo lolo 4966641 2009-10-26 19:05 Dam-Cuoi-Tren-Duong-Que.mp3
-rw-rw-r--. 1 lolo lolo 4423680 2009-10-26 19:04 Duyen-Tinh-Ly-Ngua-O.mp3
-rw-rw-r--. 1 lolo lolo 5161409 2009-10-26 19:05 Hanh-Phuc-Tram-Nam.mp3

I'm expecting someting like that on the .csv file (one more time, column order have no importance)
Code:

-rw-rw-r--.,1,lolo,lolo,5376128,2009-10-26 19:05,/home/lolo/Music,Dam-Cuoi-Dau-Xuan.mp3
-rw-rw-r--.,1,lolo,lolo,4966641,2009-10-26 19:05,/home/lolo/Music,Dam-Cuoi-Tren-Duong-Que.mp3
-rw-rw-r--.,1,lolo,lolo,4423680,2009-10-26 19:04,/home/lolo/Music,Duyen-Tinh-Ly-Ngua-O.mp3
-rw-rw-r--.,1,lolo,lolo,5161409,2009-10-26 19:05,/home/lolo/Music,Hanh-Phuc-Tram-Nam.mp3

@Yas
Your command return me ls command header that i could not manipulate on the .csv file

@ashok.g
Just missing file path, i guess with a few tuning it should be ok

@colucix
Just missing owner and group name, but i think it would be just fine.

I will work around and those command lines sample, i'm sure i will found the good one.

Thanks again to all of you.

Have a nice day.

grail 05-18-2010 10:30 AM

How about something like:
Code:

ls -l | awk '/^-/ && $1=$1' OFS=","

limdel 05-18-2010 10:54 AM

Hi Grail,

Good one, just missing the file path, i guess ls command would not return this info with 1 line script

Thanks :hattip:

limdel 05-18-2010 11:18 AM

The Command line i will use
 
From Colucix proposal

This command:
Code:

[lolo@triton ~]$ find ~/Music -maxdepth 3 -type f -iname "*.mp3" -printf "%h,%f,%CY-%Cm-%Cd %CT,%s,%u,%M\n" > mymusic.csv

Will get me:
Code:

/home/lolo/Music,Ngay-Xuan-Vui-Cuoi.mp3,2010-05-16 09:25:58.4214092500,5708100,lolo,-rw-rw-r--
/home/lolo/Music,Lien-khuc-to-hong.mp3,2010-05-16 09:25:57.6416482430,2674970,lolo,-rw-rw-r--
/home/lolo/Music,Duyen-Tinh-Ly-Ngua-O.mp3,2010-05-16 09:25:57.3173987590,4423680,lolo,-rw-rw-r--
/home/lolo/Music,Thuong-Nhau-Ly-To-Hong.mp3,2010-05-16 09:25:58.6827738740,3826414,lolo,-rw-rw-r--


grail 05-18-2010 11:18 AM

Not sure why the need for full path, but this works:
Code:

find /home/lolo/Music -maxdepth 1 -type f -name "[a-zA-Z]*" -exec ls -l {} \; | awk '/^-/ && $1=$1' OFS=","

limdel 05-19-2010 02:45 AM

Hi Grail,

Quote:

Not sure why the need for full path, but this works:
Because i search for those files on more than one sub-folder level and i want to be able to know where to pickup a particular file.

Thanks for the feedback,
Have a nice day.

Laurent

equipelinux 09-19-2014 12:14 PM

Tab?
 
Quote:

Originally Posted by grail (Post 3972897)
How about something like:
Code:

ls -l | awk '/^-/ && $1=$1' OFS=","

How to put a tab instead of ,?

rknichols 09-19-2014 01:00 PM

Note that for a general solution the file name could contain commas and/or quote marks, and you need to handle them properly in the CSV file. The overall file name needs to be quoted, and any embedded quote marks need to be doubled. For file name
Code:

She said, "No!".mp3
a correct CSV line would be
Code:

"She said, ""No!"".mp3",2010-05-16 09:25:58.4214092500,5708100,lolo,-rw-rw-r--
I didn't see anything in the above solutions that would take care of that. Also, CSV has no recognized way to handle embedded newline characters in a field. Fortunately those are rare in file names, but your script should check for and warn about that.


All times are GMT -5. The time now is 11:53 PM.