Solved

Bash - ls - and creating CSVs

Posted on 2006-11-09
10
1,431 Views
Last Modified: 2008-02-01
Hi,
I use cygwin to get bash-functionality on my windows machines.
I have a dirtectory tree, with various files at various levels, I'm looking for a script to create a csv in every folder downstream of the one I initiate the script in, with a list of the files in that folder, the csv is easy - just [ls > name.csv]
Steven
0
Comment
Question by:steviedeehook
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:ygoutham
ID: 17912493
ls -al | tr -s '  ' ' ' | tr -s ' ' ','

mind you this will not work if any of the file names has a space in them.

you can do a

ls -Ral | tr -s '  ' ' ' | tr -s ' ' ','

to recursively go through directories

goutham
0
 
LVL 84

Expert Comment

by:ozo
ID: 17912494
find . -type d -exec sh -c 'cd {}; pwd; ls > name.csv' \;
0
 
LVL 14

Expert Comment

by:ygoutham
ID: 17912510
that only messes the output with a comma inbetween the dates. forget it .  if you want only the file names then why not

ls | tr -s "\n" ","

that gives only the file names as a csv value.

ls -R | tr -s "\n" ',' | tr -s "\/" "\n"

to have the directory names displayed on top of every line

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Author Comment

by:steviedeehook
ID: 17912705
most of the filenames and folder names have spaces.

Don't want comma between the names, just what "ls > name.csv" would do.

find . -type d -exec sh -c 'cd {}; pwd; ls > name.csv' \; worked great, but get confused with spaces in folder names.

Steven
0
 
LVL 7

Expert Comment

by:Jonybrv
ID: 17912757

Hmm.. will something like this do your job ?

#!/usr/bin/perl
open(I_FILE,"ls -R1 |") or die("Cannot get the directory list.");
open(O_FILE,">treelist.csv") or die ("Cannot create output file");
foreach $line (<I_FILE>) {
chomp($line);
@countdir = split ( "\/", $line);
$output = join(",",@countdir);
$cnt = @countdir;
$cnt2=1;
print O_FILE "$addcomma.$output";
$addcomma="";
while ($cnt2 < $cnt) {
       $addcomma=$addcomma.",";
       $cnt2=$cnt2+1;
}
}
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 17912777
find . -type d -exec sh -c 'cd "{}"; pwd; ls > name.csv' \;
0
 
LVL 1

Author Comment

by:steviedeehook
ID: 17912784
find . -type d -exec sh -c 'cd "{}"; pwd; ls > name.csv' \;
Is PERFECT!
Thanks
Steven
0
 
LVL 14

Expert Comment

by:ygoutham
ID: 17912822


find . -type d -exec sh -c 'cd "{}"; pwd; ls > name.csv' \;

adding the double quote before and after the {} should do the trick
0
 
LVL 14

Expert Comment

by:ygoutham
ID: 17912825
sorry i did not see the post.  good luck!
0
 
LVL 7

Expert Comment

by:Jonybrv
ID: 17912874
Tested this :

#! /usr/bin/perl

open(I_FILE,"ls -R1 |") or die("Cannot get the directory list.");
open(O_FILE,">treelist.csv") or die ("Cannot create output file");

foreach $line (<I_FILE>) {
@countdir = split ( "\/",$line);
$output = join(",",@countdir);
$cnt = @countdir;
$cnt2=1;
if ($cnt > 1) {
$addcomma="";
print O_FILE "$output\n";
}
else
{
print O_FILE "$addcomma.$line\n";
}
while ($cnt2 < $cnt) {

$addcomma=$addcomma.",";
$cnt2=$cnt2+1;
}
}
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question