I have a folder full of different files but without extension. How can I delete all files that are zip files?
-
Hint: [file Man Page - Linux - SS64.com](https://ss64.com/bash/file.html) – DavidPostill Jul 24 '21 at 06:30
1 Answers
In my Kubuntu the following command:
file -b --mime-type path/to/some/zip
returns:
application/zip
I can use it to detect all zip files in the current directory (with subdirectries). The command is:
find . -type f -exec sh -c '
for f do
file -b --mime-type "$f" | grep -q "^application/zip$" && {
printf "%s\n" "$f"
# rm "$f"
}
done
' find-sh {} +
If the result looks sane, uncomment rm "$f" (delete #) to actually remove the files.
Notes:
Neither
-bnor--mime-typeare portable options offile. If yourfiledoes not support them then check what barefile path/to/some/zipprints. It may be:path/to/some/zip: Zip archive data, …Where
…denotes additional information. If I needed to rely on this output then mygrepcommand would be:grep -q ": Zip archive data"but it would (possibly) falsely detect files with this very string in the name. Anyway, adjust your
grepto what yourfileprints.find-shis explained here: What is the second sh insh -c 'some shell code' sh?The command does not care about filenames (in Linux what you call "extension" is just a part of filename). It answers the question you asked in the body of the post ("How can I delete all files that are zip files?"), not the title ("How to bulk delete zip files without extension").
- 69,815
- 22
- 136
- 202