Skip to content

Instantly share code, notes, and snippets.

@gregyjames
Created October 31, 2019 08:19
Show Gist options
  • Save gregyjames/f7a3175fe42551dd00b08a5ee07a3dd9 to your computer and use it in GitHub Desktop.
Save gregyjames/f7a3175fe42551dd00b08a5ee07a3dd9 to your computer and use it in GitHub Desktop.
Shell script to organize a folder based on file extension.
#!/bin/bash
#Get all files in the directory
for file in *
do
#Get the filenames and extensions
filename=$(basename -- "$file")
extension="${filename##*.}"
#If the file isn't the cleaning script
if [ "$file" != "clean.sh" ]; then
if [ ! -d "./$extension" ]; then
mkdir $extension
echo "Made the folder for $extension"
fi
if [ -f "$filename" ]; then
mv "$file" "./$extension/$file"
echo "Moved $filename to the $extension folder."
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment