Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

1 / код / deleteFiles

.sh
Скачиваний:
0
Добавлен:
27.09.2026
Размер:
2 Кб
Скачать
☆
#!/bin/bash

# check params
if [ $# -eq 0 ]; then
    echo "Error! There is no params"
    echo "Using: $0 [-p] <file_name_or_pattern>"
    exit 1
fi

#check mode (if -p - it's pattern mode)

if [ "$1" =  "-p" ]; then
    echo "Pattern mode"

    #check second param
    if [ $# -lt 2 ]; then
        echo "Error: missing pattern after -p"
        exit 1
    fi

    echo "All patterns:"
    echo "${@:2}"
    echo ""
    
    for pattern in "${@:2}"; do

        echo "Processin pattern: $pattern"

        FILES=$(ls | grep "$pattern")

        # if no files
        if [ -z "$FILES" ]; then
            echo "No files found with pattern '$pattern'"
            echo ""
            continue
        fi

        echo "Found files:"
        echo "$FILES"

        echo ""
        echo "Do you want to delete this file (y/n)"
        read answer

        if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
            echo "Deleting..."
            rm $FILES
        else
            echo "Deletion cancelled"
            echo ""
            continue
        fi
    done



else
    echo "Current file mode"
    
    for file in "$@"; do

        echo "Processin file: $file"

        #file check
        if [ ! -f "$file" ]; then
            echo "Error: File '$file' does not exists"
            continue
        fi

        echo "File '$file' exists"

        #submit deletion
        echo "Do you want to delete this file (y/n)"
        read answer

        if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
            echo "Deleting..."
            rm "$file"
        else
            echo "Deletion cancelled"
            continue
        fi
    done
fi