Skip to content

Instantly share code, notes, and snippets.

@othyn
Last active December 28, 2021 02:08
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 othyn/3b2ac05f6bc145d3b9f394562df16308 to your computer and use it in GitHub Desktop.
Save othyn/3b2ac05f6bc145d3b9f394562df16308 to your computer and use it in GitHub Desktop.
Move files into directory/directories of their own (self) filename
# This script is designed to be a 'one-liner', just remove the comments to paste and go!
# However feel free to add a shebang and use it as a script, swapping the glob out for a cheeky $1.
# [Example usage]
# Given:
# A file I want to place into a directory of its own name...
# ./John Wick.mp4
# ---
# Then:
# This script will create the required directory and move the file into it...
# ./John Wick/John Wick.mp4
# Currently lazily globbing all media files in the current directory (careful, this is just a wildcard on any .m prefixed extension):
# *.m* = .mkv, .mp4, .m4v, etc.
for filepath in *.m*; do
# Extract the basename from the path with no path or extension so we can use it as the directory name
dirname="${filepath%.*}";
# Create the directory from the name acquired above
mkdir -p "${dirname}";
# Move the existing file into the newly created directory
mv -v "${filepath}" "${dirname}";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment