E-mail : support@tech2now.in

Execute a shell script to delete files in the current folder based on the .txt file

Linux Linux command ls pwd cp mv more grep tail

Create a .txt file that contains the list of files or criteria for deletion. Each line in the file can represent a file name or a condition.

Create the shells script delete_file.sh

#!/bin/bash

file_list="your_file.txt"
deleted_file_log="deleted_files.log"

# Check if the file list exists
if [ -f "$file_list" ]; then
    # Check if the log file exists; if not, create it
    touch "$deleted_file_log"

    # Read file names from the list
    while IFS= read -r file_name; do
        echo "Deleting file: $file_name"
        
        # Check if the file exists before attempting to delete
        if [ -f "$file_name" ]; then
            rm "$file_name"
            echo "File $file_name deleted."
            # Log the deleted file name
            echo "$file_name" >> "$deleted_file_log"
        else
            echo "File $file_name does not exist."
        fi
    done < "$file_list"

    echo "Deleted file names have been saved to $deleted_file_log."
else
    echo "File list $file_list does not exist."
fi

Save the script and run the script

./delete_file.sh