Skip to content

Instantly share code, notes, and snippets.

@dantonnoriega
Created September 24, 2017 02:49
Show Gist options
  • Save dantonnoriega/c4c24506e7b88c7aac1793fa924aade7 to your computer and use it in GitHub Desktop.
Save dantonnoriega/c4c24506e7b88c7aac1793fa924aade7 to your computer and use it in GitHub Desktop.
quick example that downloads some videos from youtube (CS106B Programming Abstraction) then renames the files using regex in zsh for file names ($match). Really cool and useful!
# $match example for zsh
# inspiration:
# https://stackoverflow.com/questions/13043344/search-and-replace-in-bash-using-regular-expressions
# reprex
mkdir CS106B
cd CS106B
# download mp4s of programmming abstraction
youtube-dl -f mp4 "https://www.youtube.com/watch?v=kMzH3tfP6f8&list=PLFE6E58F856038C69"
# use reprex to chop off all the random garbage after download e.g.
# "Lecture 1 _ Programming Abstractions (Stanford)-kMzH3tfP6f8.mp4"
# vs
# "Lecture 1.mp4"
# $match matches the full regex then use ${match[1]} akin to using \\1 in perl or R (first set of parens)
for f in *
do
re='(Lecture [0-9]+) (.*)'
if [[ $f =~ $re ]];
then
mv "./$match" "./${match[1]}.mp4"
fi
done
# rename again, but now we add a "0" infront of any single digit e.g.
# Lecture 1.mp4 -> Lecture 01.mp4
for f in *
do
re='(Lecture ([0-9]).mp4$)'
if [[ $f =~ $re ]];
then
mv "./$match[1]" "./Lecture 0${match[2]}.mp4"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment