Solved

Bash - ls - and creating CSVs

Posted on 2006-11-09
10
1,419 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now