#!/bin/bash for file in *; do ## Skip unless this is a file if [ -f "$file" ]; then ## get the first four characters from filename, aka the year target="${file:0:4}" ## Create the target directory if it doesn't exist [ -d "$target" ] || mkdir "$target" 2>/dev/null ## Move the current file mv "$file" "$target" 2>/dev/null fi done