1

I got a loop so far that prints all the paths from find command:

#!/bin/bash

while read -r line ; do
    echo "Processing... $line"
done < <(find /home/$USER -type d \( -name ".*" \) -prune -o -type f -size +10M -user $USER -writable -printf '%s %p\0'  |
        sort -z -n -k1 |
        awk -v RS="\0" '
        {
            file = $0; gsub(/^[^ ]* /, "", file)
            print file
        }')

I need to find a way this script displays these files to user and let him select and unselect them and after a finished selection returns these paths to script that will remove those files or pack them.

2
  • If there's user interaction, you might as well split up your program into collection, selection, action.
    – user2849202
    Commented Mar 11, 2021 at 22:09
  • @Roadowl Thanks for the advice! But I don't really have a idea how to do that in one script, like to not output data from that loop to txt file or something. Commented Mar 12, 2021 at 9:50

1 Answer 1

1

Use fzf tool. It accepts stdin and shows the list of lines to the user and lets them select what they want. When the user submits, fzf will use stdout to print the selection. Example:

selection=$(ls | fzf)
echo "user selected: $selection"

Use fzf -m to let user select multiple lines.

Alternatively, use vipe tool, or gum, or fzy.

2
  • tested it! It's really cool feature but I'm wondering is there a built in GNU utility like that? Commented Mar 16, 2021 at 11:59
  • Not that I know of. If you can't afford adding a dependency, either reimplement some prompt-based selection or put the whole list in a temp file and spawn $EDITOR on the temp file to let user remove lines they don't desire.
    – λuser
    Commented Mar 16, 2021 at 21:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.