Skip to content

Instantly share code, notes, and snippets.

@peterkos
Created September 25, 2023 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterkos/ed9b102ba7a4755676f6c8803b21468c to your computer and use it in GitHub Desktop.
Save peterkos/ed9b102ba7a4755676f6c8803b21468c to your computer and use it in GitHub Desktop.
music file organization scripts
#!/bin/fish
# Sets the created date of each directory
# to the date of its *oldest* contained file.
# rewritten from: https://stackoverflow.com/a/40173154/1431900
#
# ```
# dir_before/
# wow_song/ @ 1/3/20 <-----------| :^(
# wow_song v1.wav @ 1/1/20 <-|
# wow_song v2.wav @ 1/2/20
#
# dir_after/
# wow_song/ @ 1/1/20 <-----------| :^)
# wow_song v1.wav @ 1/1/20 <-|
# wow_song v2.wav @ 1/2/20
# ```
function "datefix"
# for nice table formatting later, lol
set maxDirNameLen (math (ls -l --no-permissions --no-filesize --no-time --no-user | awk '{ print length }' | sort -n | tail -1) + 1)
for dir in */
cd $dir
set dateCur (date -r . +%D\ %I:%M\%p)
# Get name of newest file
set newestChild (stat -f "%m:%N" * | sort -n | head -1 | cut -f2- -d:)
# Set modification times of parent folder to match
touch -r $newestChild .
set dateNew (date -r . +%D\ %I:%M\%p)
if test "$dateCur" = "$dateNew"
printf "%-"$maxDirNameLen"s | (same)\n" $dir
else
printf "%-"$maxDirNameLen"s | %s -> %s\n" $dir $dateCur $dateNew
end
cd ../
end
end
#!/bin/fish
# Create folders from the first unique word of every file
# ```
# dir_before/
# wow_song v1.wav
# wow_song v2.wav
# cool_song v1.wav
# cool_song v2.wav
#
# dir_after/
# wow_song/
# cool_song/
# wow_song v1.wav
# wow_song v2.wav
# cool_song v1.wav
# cool_song v2.wav
# ```
fd | grep -Eo './(\w)*' | uniq | xargs mkdir
# then, the child folders are moved in:
# ```
# dir_before/
# wow_song/
# cool_song/
# wow_song v1.wav
# wow_song v2.wav
# cool_song v1.wav
# cool_song v2.wav
#
# dir_after/
# wow_song/
# wow_song v1.wav
# wow_song v2.wav
# cool_song/
# cool_song v1.wav
# cool_song v2.wav
# ```
fd -t f | grep -Eo './(\w)*' | xargs -I {} zsh -c "mv {}* {}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment