gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Write short command for bash which do "file" command for every file in directory and…
- User
Write short command for bash which do "file" command for every file in directory and gets MIME type. If there is NO "jpeg" substring in "file" command output, remove file
- Assistant
You can use the following
bashcommand to iterate through each file in a directory, check for the "jpeg" substring in the MIME type, and remove files that do not contain it:for file in *; do if [[ -f $file && $(file --mime-type -b "$file") != *jpeg* ]]; then rm "$file"; fi; doneExplanation:
for file in *: Iterates over all files in the current directory.[[ -f $file ]]: Checks if the current item is a file.file --mime-type -b "$file": Gets the MIME type of the file in a brief format.!= *jpeg*: Checks if the MIME type does not contain "jpeg".rm "$file": Removes the file if it doesn't match the condition.
Make sure to be cautious when running this command, as it will permanently delete files that do not have a JPEG MIME type. You might want to test it first by, for instance, echoing the file names instead of removing them.