Skip to content

Instantly share code, notes, and snippets.

@janderudder
Created June 28, 2019 20:53
Show Gist options
  • Save janderudder/23e512f937050c05960dc5b87dec9b1f to your computer and use it in GitHub Desktop.
Save janderudder/23e512f937050c05960dc5b87dec9b1f to your computer and use it in GitHub Desktop.
bash script: delete .vscode/ipch folders
#!/bin/bash
help() {
cat <<- EOF
Usage: $(basename $0) PATH...
Erase '.vscode/ipch' folders from VSCode projects in
the sub-directories of the supplied path.
PATH must be a valid folder.
EOF
}
if [[ $# -ne 1 ]]; then
echo "Error: $(basename $0) expects one argument."
echo "Type '$(basename $0) --help' for more information."
exit
fi
# Parse args
declare TargetFolder=""
while [[ $# -ne 0 ]]; do
case $1 in
'--help' | '-h' | '-?')
help && exit
;;
*)
TargetFolder="$1"
;;
esac
shift
done
# File access error
if [[ ! -d $TargetFolder ]] && [[ ! -w $TargetFolder ]]; then
echo "Provided base path is not a directory or is not writeable."
echo "Error for path: $TargetFolder"
exit
fi
# Main functionality: delete .vscode/ipch folders
echo "Removing '.vscode/ipch' folder in:"
let DeleteCount=0
let DeleteSize=0
declare Size=""
for Project in $(find "$TargetFolder" -name ipch); do
Suffix="${Project:${#Project}-12:12}"
if [[ $Suffix = '.vscode/ipch' ]]; then
FullPath="$(realpath "$Project")"
Size=$(du -sbh "${FullPath}" | cut -f1)
echo "${FullPath:0:-12} (${Size})"
DeleteSize=$((DeleteSize+$(du -sb "${FullPath}" | cut -f1)))
rm -rf "$FullPath"
((++DeleteCount))
fi
done
# Output result
if [[ $DeleteCount -eq 0 ]]; then
echo "...no folders found."
echo "Deleted ${DeleteCount} folders."
else
declare Unit="Bytes"
[[ ${DeleteSize} -gt 1024 ]] && ((DeleteSize/=1024)) && Unit="KB"
[[ ${DeleteSize} -gt 1024 ]] && ((DeleteSize/=1024)) && Unit="MB"
echo "Deleted ${DeleteCount} folders (${DeleteSize} ${Unit})"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment