Listing a directory with spaces in perl

Posted on 2009-04-20
Last Modified: 2012-05-06
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.

# Set some variables


# 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>) {


#       @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

Question by:savone
    LVL 9

    Expert Comment

    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";
    my $artistname = basename($music);
    print $artistname."\n";
    while ( <*>){
     print "\t".$_."\n"; 

    Open in new window

    LVL 10

    Accepted Solution

    simplify at most
    my $music = "/home/savona/drobo/Music/";
    chdir $music;
    foreach my $artist ( <*> ){
        if ( -d $artist ) {
            print "$artist\n"; 
            chdir $artist;
            foreach my $album ( <*> ){
                print "\t$album\n" if -d $album; 
            chdir "..";

    Open in new window

    LVL 5

    Expert Comment

    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>;
    print readdir(DIR);

    Open in new window

    LVL 23

    Author Closing Comment

    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?

    LVL 23

    Author Comment

    Oleber, how would I print the output to a file?
    LVL 10

    Expert Comment

    as usual, redirect the output

    perl > my_file.txt

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    755 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

    21 Experts available now in Live!

    Get 1:1 Help Now