LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-18-2010, 04:22 AM   #1
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS - FEDORA
Posts: 59

Rep: Reputation: 15
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.
 
Old 05-18-2010, 04:35 AM   #2
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
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
 
Old 05-18-2010, 04:43 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by limdel View Post
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?
 
Old 05-18-2010, 04:58 AM   #4
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
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
 
Old 05-18-2010, 06:45 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 05-18-2010, 11:00 AM   #6
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS - FEDORA
Posts: 59

Original Poster
Rep: Reputation: 15
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.
 
Old 05-18-2010, 11:30 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028

Rep: Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200
How about something like:
Code:
ls -l | awk '/^-/ && $1=$1' OFS=","
 
1 members found this post helpful.
Old 05-18-2010, 11:54 AM   #8
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS - FEDORA
Posts: 59

Original Poster
Rep: Reputation: 15
Hi Grail,

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

Thanks
 
Old 05-18-2010, 12:18 PM   #9
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS - FEDORA
Posts: 59

Original Poster
Rep: Reputation: 15
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--
 
Old 05-18-2010, 12:18 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028

Rep: Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200
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=","
 
Old 05-19-2010, 03:45 AM   #11
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS - FEDORA
Posts: 59

Original Poster
Rep: Reputation: 15
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
 
Old 09-19-2014, 01:14 PM   #12
equipelinux
LQ Newbie
 
Registered: Mar 2014
Posts: 2

Rep: Reputation: Disabled
Smile Tab?

Quote:
Originally Posted by grail View Post
How about something like:
Code:
ls -l | awk '/^-/ && $1=$1' OFS=","
How to put a tab instead of ,?
 
Old 09-19-2014, 02:00 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,797

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to import redhat users to ClearOS using CSV file format. alfaedh Linux - Server 1 01-31-2010 04:48 PM
Redirect output in .xls or csv format jeesun Programming 5 12-25-2009 06:56 PM
Best video format for YouTube upload and can Recordmydesktop output that format? linus72 Linux - Software 6 12-21-2009 04:53 PM
Script Output to CSV zokken Linux - General 3 04-11-2009 07:32 AM
OpenOffice help , read csv-like text format ufmale Linux - Desktop 3 09-10-2008 02:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration