Skip to content
Advertisement

How to sort files into folders by filetype on bash (with ‘file’ command)?

I have thousands of files without extensions after recovery (mostly pictures). I need to sort them into separate folders by filetype (folders must be created during sort process). I can determine filetype in linux using “file” command. Does somebody have bash script for it?

For example: Initial dir contains files: 001, 002, 003, 004. After sorting should be 3 dirs: ‘jpeg’ contain 001.jpg, 003.jpg; ‘tiff’ contain 002.tiff and ‘others’ contain 004.

Advertisement

Answer

This answer does not execute file command multiple times for each file, which is unnecessary

JavaScript

similarly, can be done with bash4+ script using associative arrays.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement