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. |
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 Yas |
Quote:
|
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 |
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" |
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 Code:
-rw-rw-r--.,1,lolo,lolo,5376128,2009-10-26 19:05,/home/lolo/Music,Dam-Cuoi-Dau-Xuan.mp3 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. |
How about something like:
Code:
ls -l | awk '/^-/ && $1=$1' OFS="," |
Hi Grail,
Good one, just missing the file path, i guess ls command would not return this info with 1 line script Thanks :hattip: |
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-- |
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="," |
Hi Grail,
Quote:
Thanks for the feedback, Have a nice day. Laurent |
Tab?
Quote:
|
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 Code:
"She said, ""No!"".mp3",2010-05-16 09:25:58.4214092500,5708100,lolo,-rw-rw-r-- |
All times are GMT -5. The time now is 11:53 PM. |