Bash Aliases

In bash_aliases file can set alias to command or scripts for automate proccess in the linux box administration is under home directory as a hidden resource and runs at start of the system to reload need source it with

source ~/.bash_aliases

or reloading the bash tty

exec bash

find-tools

Search and Delete with fzf

Add next code to ~/.bash_aliases

~/bash_aliases
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
alias find-tools='find-tools'
function find-tools(){
# URL_SOURCE https://stackoverflow.com/a/29754866/5090696
getopt --test > /dev/null
if [[ $? -ne 4 ]]; then
    echo "I’m sorry, `getopt --test` failed in this environment."
    exit 1
fi
OPTIONS=dm:fo:v
LONGOPTIONS=delete,mimetype:,force,output:,verbose
# -temporarily store output to be able to check for errors
# -activate advanced mode getopt quoting e.g. via “--options”
# -pass arguments only via   -- "$@"   to separate them correctly
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTIONS --name "$0" -- "$@")
if [[ $? -ne 0 ]]; then
    # e.g. $? == 1
    #  then getopt has complained about wrong arguments to stdout
    exit 2
fi
# use eval with "$PARSED" to properly handle the quoting
eval set -- "$PARSED"
# now enjoy the options in order and nicely split until we see --
while true; do
    case "$1" in
        -d|--delete)
            d=y
            shift
            ;;
        -f|--force)
            f=y
            shift
            ;;
        -v|--verbose)
            v=y
            shift
            ;;
        -o|--output)
            outFile="$3"
            shift 2
            ;;
        -m|--mimetype)
            mime="$2"
            shift 2
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Programming error"
            exit 3
            ;;
    esac
done

mimetypes=$(sed -E 's/\/.*//g; /^$/d; /^#/d' /etc/mime.types | uniq)

echo "arguments are $1 , $mime"

ext=$(sed -E "/^${mime}/!d; s/^[^ \t]+[ \t]*//g; /^$/d; s/ /\n/g" /etc/mime.types | sed -Ez 's/\n$//; s/\n/\\|/g; s/(.*)/\.*\\.\\(\1\\)\n/')

# handle non-option arguments
if [[ $# -ne 1 ]]; then
    echo "Usage: ${0##*/} -m <mimetype> --delete <path/to/files>."
#    echo "Usage: ${0##*/} -m=<mimetype>"
    echo "Available mimetypes:"
    echo "$mimetypes"
#    exit 4
fi
# init the program

if [[ $1 ]]; then
     if [[ $d ]]; then
         find "$1" -type f -regex "$ext" | fzf -m | xargs -d '\n' -I {} rm -v {}
     fi
else
    echo "verbose: $v, force: $f, mimetype: $mime, delete: $d, in: $1, out: $outFile"
fi
}

how this works :

fzf-usage
Visualize Mimetypes

for search and delete files with mimetype=audio do

find-tools --delete -m audio ./music/

fzf-filter
Filter results

Select multiple items with tab and Deselection with Shift + tab

fzf-delete
delete results

If you want visualize music with another user like www-data for delete them

sudo -u www-data bash
# then logged as www-data user
source /home/<username>/.bash_aliases
find-tools -m audio --delete ./data/

Last update: Nov 20, 2024