Search code examples
linuxbashsh

How to create a loop that goes thought find and let's user select files?


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.


Solution

  • 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.