Skip to content

Instantly share code, notes, and snippets.

@fetwar
Created April 17, 2023 05:01
Show Gist options
  • Save fetwar/1e8e10da11c4e8e47e92a0a854fe161c to your computer and use it in GitHub Desktop.
Save fetwar/1e8e10da11c4e8e47e92a0a854fe161c to your computer and use it in GitHub Desktop.
Remove files from current folder if they are present in a tarball within the same dir
#!/bin/bash
# Get the current directory path
current_directory=$(pwd)
# Iterate over all tar files in the current directory
for tar_file in "$current_directory"/*.tar; do
# If the tar file exists, process it
if [ -f "${tar_file}" ]; then
# Iterate over all files in the current directory (excluding directories and tar files)
find . -maxdepth 1 -type f ! -name '*.tar' -exec basename {} \; | while read -r file; do
# Check if the file exists in the tar archive
if tar -tf "${tar_file}" | grep -q "${file}"; then
# If it exists, delete the file
rm -f "${file}"
echo "Deleted ${file}"
fi
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment