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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-18-2010, 04:22 AM
|
#1
|
Member
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS - FEDORA
Posts: 59
Rep:
|
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.
|
|
|
05-18-2010, 04:35 AM
|
#2
|
Member
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559
Rep:
|
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
|
|
|
05-18-2010, 04:43 AM
|
#3
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by limdel
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?
|
|
|
05-18-2010, 04:58 AM
|
#4
|
Member
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215
Rep:
|
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
|
|
|
05-18-2010, 06:45 AM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
|
|
|
05-18-2010, 11:00 AM
|
#6
|
Member
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS - FEDORA
Posts: 59
Original Poster
Rep:
|
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.
|
|
|
05-18-2010, 11:30 AM
|
#7
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
How about something like:
Code:
ls -l | awk '/^-/ && $1=$1' OFS=","
|
|
1 members found this post helpful.
|
05-18-2010, 11:54 AM
|
#8
|
Member
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS - FEDORA
Posts: 59
Original Poster
Rep:
|
Hi Grail,
Good one, just missing the file path, i guess ls command would not return this info with 1 line script
Thanks
|
|
|
05-18-2010, 12:18 PM
|
#9
|
Member
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS - FEDORA
Posts: 59
Original Poster
Rep:
|
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--
|
|
|
05-18-2010, 12:18 PM
|
#10
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
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=","
|
|
|
05-19-2010, 03:45 AM
|
#11
|
Member
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS - FEDORA
Posts: 59
Original Poster
Rep:
|
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
|
|
|
09-19-2014, 01:14 PM
|
#12
|
LQ Newbie
Registered: Mar 2014
Posts: 2
Rep:
|
Tab?
Quote:
Originally Posted by grail
How about something like:
Code:
ls -l | awk '/^-/ && $1=$1' OFS=","
|
How to put a tab instead of ,?
|
|
|
09-19-2014, 02:00 PM
|
#13
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,797
|
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 07:41 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|