wmdeit-cf-wmdelib/scripts/del-files-not-in-list.sh

37 lines
813 B
Bash
Raw Normal View History

2023-10-24 17:20:23 +00:00
#!/bin/bash
# Path to the folder containing files
folder_path="$1"
# Path to the file containing the list of filenames
list_file="$2"
# Check if the folder exists
if [ ! -d "$folder_path" ]; then
2023-10-24 21:17:40 +00:00
# echo "Folder does not exist: $folder_path"
exit 0
2023-10-24 17:20:23 +00:00
fi
# Check if the list file exists
if [ ! -f "$list_file" ]; then
2023-10-24 21:17:40 +00:00
# echo "List file does not exist: $list_file"
exit 0
2023-10-24 17:20:23 +00:00
fi
# Loop through the files in the folder
for file_in_folder in "$folder_path"/*; do
# Check if the current file is a regular file
if [ -f "$file_in_folder" ]; then
# Get the filename without the path
filename=$(basename "$file_in_folder")
# Check if the filename is not in the list
if ! grep -qFx "$filename" "$list_file"; then
echo "Deleting: $file_in_folder"
rm "$file_in_folder"
fi
fi
done