?
Solved

Bash - ls - and creating CSVs

Posted on 2006-11-09
10
Medium Priority
?
1,456 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month15 days, left to enroll

771 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