Solved

Bash - ls - and creating CSVs

Posted on 2006-11-09
10
1,428 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
docker invalid registry name 2 107
Codiing Non-Existent Links 4 67
linux redhat 7.2 10 46
how to install java on RHEL image on EC2 4 7
This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

896 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

12 Experts available now in Live!

Get 1:1 Help Now