Skip to content

Instantly share code, notes, and snippets.

@futurulus
Last active July 28, 2016 06:11
Show Gist options
  • Save futurulus/c0d5466d1f0411ad4f8bb5f7517ac82b to your computer and use it in GitHub Desktop.
Save futurulus/c0d5466d1f0411ad4f8bb5f7517ac82b to your computer and use it in GitHub Desktop.
Ever get impossible-to-delete files on Windows because they have * or ? in their names?
#!/usr/bin/env bash
# Rename files in a Windows folder that somehow ended up with illegal Windows filenames
# (because NTFS doesn't enforce this, but it makes the files unusable and indestructible).
# Must be run in Linux. There seems to be no other way.
ls -1 | awk '/[/\:*?"<>|]/ {
orig = $0;
gsub(/[/\:*?"<>|]/, "_", $0);
gsub(/'"'"'/, "'"'"'\"'"'"'\"'"'"'", orig);
gsub(/'"'"'/, "'"'"'\"'"'"'\"'"'"'", $0);
print "mv '"'"'" orig "'"'"' '"'"'" $0 "'"'"'"
}' | bash -s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment