Skip to content

Instantly share code, notes, and snippets.

@erosenberg
Created December 15, 2019 16:50
Show Gist options
  • Save erosenberg/594518f1a786ffc427aa02b205d664ee to your computer and use it in GitHub Desktop.
Save erosenberg/594518f1a786ffc427aa02b205d664ee to your computer and use it in GitHub Desktop.
Create directories from filenames and move the files into those directories, based on file type.
read -p "Please enter a file type: " filetype
read -p "Please enter a directory: " filesdir
if [ -z $filetype ]
then
echo "$filetype is empty. Please retry."
exit 2
fi
if [ ! -d $filesdir ]
then
echo "$filesdir does not exist. Please retry with another directory."
exit 2
fi
cd $filesdir
echo "Navigating to: $PWD "
echo "Searching for .$filetype's in $filesdir"
set -o errexit -o nounset
for file in *.$filetype
do
if [ -f "$file" ]; then
dir="${file%.$filetype}"
mkdir -- "$dir"
mv -- "$file" "$dir"
else
echo "No .$filetype's found in this directory."
fi
done
echo "Process complete. Files have been renamed and moved to their new directories."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment