Link to home
Start Free TrialLog in
Avatar of Steven Vona
Steven VonaFlag for United States of America

asked on

Listing a directory with spaces in perl

here is my situation...

I have a music folder with thousands of artists, the directory structure is as follows:
artist name > Album name > Songs

example: [savona@bighat scripts]$ ls /home/savona/drobo/Music/The\ Beatles/
The Beatles - A Hard Day's Night    The Beatles - Revolver
The Beatles - Help                  The Beatles - Rubber Soul
The Beatles - Let It Be             The Beatles - Sgt. Peppers Lonely Hearts Club Band
The Beatles - Magical Mystery Tour


As a learning experience (just starting with perl) I wanted to make a perl script that will make me a nice clean music list text file.  Something that would end up looking like this:

The Beatles
      The Beatles - A Hard Day's Night
      The Beatles - Help
      The Beatles - Let It Be
      The Beatles - Magical Mystery Tour
      The Beatles - Revolver
      The Beatles - Rubber Soul
      The Beatles - Sgt. Peppers Lonely Hearts Club Band

So I have been sitting here hacking away at it but I am at a lost here.  How am I going to list the directories out if they have spaces in them?  I tried multiple ways, globbing, splitting, escaping (i.e. \\\)... I know it is something so simple I am missing.

Here is the code I have so far... some things may be commented out since I was trying a bunch of different things.  Any help would be appreciated.
#!/usr/bin/perl
# Set some variables
$musicdir="/home/savona/drobo/Music/";
# Get long listing of Music directory and awk out artist names and make temp files
system("ls -l $musicdir > /tmp/musiclist");
system("`awk '{print \$8,\$9,\$10,\$11,\$12,\$13,\$14,\$15,\$16}' /tmp/musiclist > /tmp/musiclist2`");
system("rm -f /tmp/musiclist");
system("`awk '{print \$1,\$2,\$3,\$4,\$5,\$6,\$7}' /tmp/musiclist2 > /tmp/musiclist3`");
# open temp file for reading
open (MUSICLIST, '/tmp/musiclist2');
while (<MUSICLIST>) {
        chomp($name);
#       @n = split / /;
#       $fullname = `echo "@n[0]" "@n[1]" "@n[2]" "@n[3]" "@n[4]" "@n[5]" "@n[6]" "@n[7]"`;
#       system("ls -l $musicdir$fullname >> /home/savona/musiclist.txt");
#
print "$name";
}

Open in new window

Avatar of ghostdog74
ghostdog74

if you want to use Perl, then use Perl. There's no need to use awk or any shell tools (for what you want).

use File::Basename;
my $music = "/home/savona/drobo/Music/The Beatles";
chdir($music);
my $artistname = basename($music);
print $artistname."\n";
while ( <*>){
 print "\t".$_."\n"; 
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oleber
oleber
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also use this code snippet to list all the files in a drectory.
Do let me know i you need any more help on this.
$direct=<Directory Path>;
opendir(DIR,$direct);
print readdir(DIR);
closedir(DIR);

Open in new window

Avatar of Steven Vona

ASKER

Thanks, this code does exactly what I wanted it to.  Is it possible you can take a second and explain what the code is doing?  How did you get around the spaces in the directory names?

Thanks!
Oleber, how would I print the output to a file?
as usual, redirect the output

perl script.pl > my_file.txt