Created
August 22, 2024 16:18
-
-
Save cemolcay/47870c105336e2e578e93dce9d02e228 to your computer and use it in GitHub Desktop.
Remove files from a folder with a name reference file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: ./remove_files.sh /path/to/folder /path/to/input_file.txt | |
# Get the directory and input file from the arguments | |
TARGET_DIR="$1" | |
INPUT_FILE="$2" | |
# Check if the directory exists | |
if [ ! -d "$TARGET_DIR" ]; then | |
echo "Error: Directory $TARGET_DIR does not exist." | |
exit 1 | |
fi | |
# Check if the input file exists | |
if [ ! -f "$INPUT_FILE" ]; then | |
echo "Error: Input file $INPUT_FILE does not exist." | |
exit 1 | |
fi | |
# Read the input file line by line | |
while IFS= read -r filename; do | |
# Construct the full path to the file | |
file_path="$TARGET_DIR/$filename" | |
# Check if the file exists | |
if [ -f "$file_path" ]; then | |
# Delete the file | |
rm "$file_path" | |
echo "Deleted: $file_path" | |
else | |
echo "File not found: $file_path" | |
fi | |
done < "$INPUT_FILE" | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment