unixcontainerslsbusyboxbasename

How to get the basename of multiple file with the filename pattern in linux docker container


I have multiple files with the same name pattern in a directory in my container folder:

/report/tsl_report_20210604.csv
/report/tsl_report_20210604.zip

I am trying to list down and get the basename. So ouput should be like:

tsl_report_20210604.csv
tsl_report_20210604.zip

I am using command ls report/tsl_report_* | xargs basename -a 2>&1 and it's working fine on my local Mac machine but when running the same in container, I am getting the below o/p:

BusyBox v1.25.1 (2018-05-30 19:58:18 GMT) multi-call binary.

Usage: basename FILE [SUFFIX]

Strip directory path and .SUFFIX from FILE

I don't have any clue why it's not working in container.Can anyone please pointout.

Thanks,


Solution

  • As the stripped down version of basename as part of busybox, you can implement the same result by:

    Option 1:

    ls report/tsl_report_* | xargs -n1 basename
    

    Option 2:

    $ for x in report/tsl_report_*; do basename $x ;done
    tsl_report_20210604.csv
    tsl_report_20210604.zip
    

    Option 3:

    find ./report/ -name 'tsl_report_*' | xargs -n1 basename