How to get just filename from minio (mc) ls

Viewed 365

I need to retrieve the filename for a bash script. I thought mc ls could do all that ls can do, but I seem to be mistaken. So now I'm struggling with regex.

When I do mc ls minio/bucket1/, I'll get:

[2021-05-14 11:15:18 CEST]     0B files1/
[2021-05-14 11:15:18 CEST]     0B files2/
[2021-05-14 11:15:19 CEST]     0B file1.ext
[2021-05-14 11:15:18 CEST]     0B file2.ext

How can I extract just the filename in bash?

1 Answers

You can pipe the following sed command after your mc ls command:

sed -n 's/^\[[^][]*][[:blank:]]*[^[:blank:]]*[[:blank:]]\(.*\.gpkg\)$/\1/p'

See the online demo.

Details:

  • -n - suppresses the default line output
  • s - substitution command
  • ^\[[^][]*][[:blank:]]*[^[:blank:]]*[[:blank:]]*\(.*\.gpkg\)$ - a regex that matches
    • ^ - start of string
    • \[[^][]*] - a substring between [ and ] with no square brackets inside
    • [[:blank:]]* - zero or more horizontal whitespace
    • [^[:blank:]]* - zero or more non-horizontal whitespace chars
    • [[:blank:]]* - zero or more horizontal whitespace
    • \(.*\.gpkg\) - Group 1: any text and then .gpkg
    • $ - end of string
  • \1 - replaces the match with Group 1 value
  • p - prints the result of the substitution.
Related