Skip to content

Instantly share code, notes, and snippets.

@davidofwatkins
Created August 19, 2016 06:26
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 davidofwatkins/57190d16a15c8d7f00240117739f81c0 to your computer and use it in GitHub Desktop.
Save davidofwatkins/57190d16a15c8d7f00240117739f81c0 to your computer and use it in GitHub Desktop.
File Organization

This contains a few handy scripts for organizing files (particularly video files). First, use ./remove-non-video-files.sh to clear out any non-video files (and optionally non-subtitle files), then use ./org-files.sh to move all files matching a specified pattern into separate folders.

# !/usr/bin/bash
# A quick script to clean up multiple files with similar names into single directories.
# This will create directories as specified and move all files based on those directory names
# into them.
# Example usage: the below command will move any files in all subdirectories that match:
#
# *game*of*thrones* --> Game-Of-Thrones/
# *silicon*valley* --> Silicon-Valley/
# *doctor*who* --> Doctor-Who/
#
# ./org-files.sh Game-of-Thrones Silicon-Valley Doctor-Who
#
# @todo: might be nice to search matched files for TV *S##E##* format and organize them futher into folders
set -u # error when referencing undefined vars
GLOBIGNORE="*" # bash doesn't normally like "*" in strings
if [ -z "$@" ]; then
echo "Usage: pass a list of hyphenated directory names to create and to search for."
exit 1
fi
show_names=("$@") # turn our arguments into an array
for name in "${show_names[@]}"
do
search=${name//-/\*}
mkdir -p $name
echo "Removing all files matching \"*$search*\" to \"$name\"..."
find . -iname "*$search*" -type f -exec mv "{}" $(echo "./$name/") \;
done
# Now we're all done, remove all empty folders
echo ""
echo "Cleaning up empty directories..."
find . -type d -empty -delete
# !/usr/bin/bash
#
# Quick script to remove all files in all subdirectories that do not have one of the following:
#
# ".mkv"
# ".mp4"
# ".wmv"
# ".avi"
# ".mov"
# ".srt"
# ".sub"
#
# Usage:
# ./remove-non-video-files.sh [-s|--keep-subtitles]
#
# -s, --keep-subtitles Keep subtitle files
#
# You will see a preview of results before being asked to remove them
set -u # error when referencing undefined vars
# Get a list of all video files
files=$(find . -type f | grep -v ".mkv" | grep -v ".mp4" | grep -v ".wmv" | grep -v ".avi" | grep -v ".mov")
# If specified, add subtitle files to the list
if [[ $* == *--keep-subtitles* || $* == *-s* ]]; then
files=$(echo $files | tr " " "\n" | grep -v ".srt" | grep -v ".sub")
fi
if [ ! "$files" ];then
echo "No files to remove."
exit 0
fi
echo "The following files will be removd:"
echo ""
echo "$files"
echo ""
printf "Are you sure? [y/n] "
read answer
answer="${answer,,}" # make answer lowercase
if [ "$answer" != 'y' -a "$answer" != 'yes' ]; then
exit 0
fi
# Escape spaces and delete!
echo "$files" | sed 's/ /\\ /g' | xargs rm -f
echo "...DONE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment