Search code examples
perls3cmd

s3cmd list of contents - only filenames - perl one liner?


Currently I'm using s3cmd ls s3://location/ > file.txt to get a list of contents of my s3 bucket and save on a txt. However the above returns dates, filesizes paths and filenames.

for example:

2011-10-18 08:52      6148   s3://location//picture_1.jpg

I only need the filenames of the s3 bucket - so on the above example I only need picture_1.jpg.
Any suggestions?

Could this be done with a Perl one liner maybe after the initial export?


Solution

  • File::Listing does not support this format because the designers of this listing format were stupid enough to not simply reuse an existing one. Let's parse it manually instead.

    use URI;
    my @ls = (
        "2011-10-18 08:52 6148 s3://location//picture_1.jpg\n",
        "2011-10-18 08:52 6148 s3://location//picture_2.jpg\n",
        "2011-10-18 08:52 6148 s3://location//picture_3.jpg\n",
    );
    
    for my $line (@ls) {
        chomp $line;
        my $basename = (URI->new((split q( ), $line)[-1])->path_segments)[-1];
    }
    
    __END__
    picture_1.jpg
    picture_2.jpg
    picture_3.jpg
    

    As oneliner:

    perl -mURI -lne 'print ((URI->new((split q( ), $line)[-1])->path_segments)[-1])' < input