Search code examples
pathpipeconcatenationls

getting all mounting directories with a specific name


I am trying to receive all mounting directories with a specific name in order to pipe them into another command.

within /mnt/ there are 2 folders, plotfield1 and plotfield2
ls|grep plotfield properly returns those two foldernames:
enter image description here

but what I am looking for is the full path:

/mnt/plotfield1
/mnt/plotfield2

Currently, for testing, I use this:

cd /mnt/
test=$(ls|grep plotfield|cat <(echo -n '/mnt/') -)
echo $test

the result is unexpected, only the first string is concatenated: enter image description here

is there a better way to get the full path of the files? the goal would be something like this pseudocode:
ls|grep plotfield|concat|xargs chia plots add -d


Solution

  • This should be straightforward using a glob expression like plotfield* and using the builtin printf to put the output to standard output, i.e. from any path

    printf '%s\n' /mnt/plotfield*
    # or 
    printf '%s\n' /mnt/plotfield?
    

    Also for storing multiple lines of output, use an array type to hold your values. So to store the above lines to an array, use mapfile or readarray

    mapfile -t paths < <(printf '%s\n' /mnt/plotfield?)
    

    and pass the array expansion "${paths[@]}" to any other commands as needed.