Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Created August 22, 2024 16:18
Show Gist options
  • Save cemolcay/47870c105336e2e578e93dce9d02e228 to your computer and use it in GitHub Desktop.
Save cemolcay/47870c105336e2e578e93dce9d02e228 to your computer and use it in GitHub Desktop.
Remove files from a folder with a name reference file
#!/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