Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bash - ls - and creating CSVs

Posted on 2006-11-09
10
Medium Priority
?
1,469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

618 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