Sort files in directory by extension with sortbyext

Moderator: Forum moderators

Post Reply
User avatar
stemsee
Posts: 835
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 195 times
Been thanked: 144 times
Contact:

Sort files in directory by extension with sortbyext

Post by stemsee »

sortbyext is a utility to help organise files into directories according to file extension. Directories are created. Empty files and directories will also be deleted. I cleaned up some drives and sub directories successfully. So may be someone can find a use for it too!

Usage: create executabe in path, /usr/sbin/sortbyext .... then open terminal in directory and type sortbyext
before.png
before.png (58.21 KiB) Viewed 312 times
after.png
after.png (36.48 KiB) Viewed 312 times
sortbyext.png
sortbyext.png (102.48 KiB) Viewed 312 times

Code: Select all

#!/bin/sh
# by stemsee gpl v3.0
for i in *
do 
	[[ ! -d $(echo "$i" | rev | cut -f1 -d'.' | rev) ]] && mkdir -p $(echo "$i" | rev | cut -f1 -d'.' | rev)
	mv "$i" $(echo "$i" | rev | cut -f1 -d'.' | rev)/
done
for i in *
do 
	[ -d "$i" ] && find "$i" -type d -empty -delete || find "$i" -type f -empty -delete
done
Post Reply

Return to “Utilities”