Skip to content

Instantly share code, notes, and snippets.

@f-steff
Created January 13, 2020 12:20
Show Gist options
  • Select an option

  • Save f-steff/05079a2efdc67cbfacfbda0ad0aba9aa to your computer and use it in GitHub Desktop.

Select an option

Save f-steff/05079a2efdc67cbfacfbda0ad0aba9aa to your computer and use it in GitHub Desktop.
Bash shell: Sort sets of numbered files into numbered Folders
# I had 70000+ files all named after the pattern nnn_something.jpg. nnn was a number from 000 to 999. Not all numbers existed.
# Each set of nnn belonged together, and I wanted them placed in sub-folders like this 074/074_something.jpg
# These three bash shell commands cleaned up the mess in a few seconds:
# CREATE FOLDERS.
#This works on bash prior to version 4 - as on Mac. (Newer Bash can just write $ mkdir {000..999} )
$ for i in 00{0..9} 0{10..99} {100..999}; do mkdir ${i} ; done
# MOVE FILES
#This works on bash prior to version 4 - as on Mac. (Newer Bash can use {000..999} )
$ for i in 00{0..9} 0{10..99} {100..999}; do mv ${i}*.jpg ${i}/ ; done
# DELETE EMPTY FOLDERS
$ find . -type d -empty -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment