Last active
January 10, 2024 06:25
-
-
Save wcoastsands/622c1ecc8cb463bb3faec0cef61cb908 to your computer and use it in GitHub Desktop.
Sorts files into subfolders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Sorts files into subfolders by date, or by file extension. | |
# | |
# TODO: Set the permissions on this file to 700. | |
# chmod 700 sort-files.sh | |
# TODO: Create symlinks for use with this script. | |
# ln -s sort-files.sh sort-files | |
# File extension (optional parameter). | |
EXT="${1^^}" | |
# Number of files moved. Value should be 0. | |
COUNT=0 | |
# Limits number of files moved. To disable, set value to 0. | |
LIMIT=0 | |
# Prevents files from being moved. To disable, set value to 0. | |
TESTMODE=0 | |
# Param 1: Message string | |
function LogMessage(){ | |
if [[ $TESTMODE -gt 0 ]]; then | |
echo "[TESTMODE] $1" | |
else | |
echo "$1" | |
fi | |
} | |
# Param 1: File name | |
# Param 2: Target subfolder | |
function MoveFile(){ | |
FILE="$1" | |
DIR="$2" | |
LogMessage "move '$FILE' to folder '$DIR'" | |
if [[ $TESTMODE -le 0 ]]; then | |
mkdir -p -v "$DIR" | |
mv -v "$FILE" "$DIR/" | |
fi | |
COUNT=$((COUNT+1)) | |
} | |
# Param 1: File name | |
# Param 2: File extension | |
function SortByExt(){ | |
FILE="$1" | |
FILE_EXT="${1##*.}" | |
FILE_EXT="${FILE_EXT^^}" | |
SORT_EXT="${2^^}" | |
DIR="$SORT_EXT" | |
if [[ "${FILE_EXT^^}" == "$SORT_EXT" ]]; then | |
LogMessage "match found: file '$FILE'" | |
MoveFile "$FILE" "$DIR" | |
fi | |
} | |
# Param 1: File name | |
function SortByDate(){ | |
FILE="$1" | |
DIR="$(date -r "$FILE" +%Y-%m)" | |
MoveFile "$FILE" "$DIR" | |
} | |
# Start sorting... | |
if [[ ! -z "$EXT" ]]; then | |
LogMessage "Sorting $EXT files into subfolder..." | |
else | |
LogMessage "Sorting files into subfolders by date..." | |
fi | |
for FILE in *.*; do | |
if [[ ! -z "$EXT" ]]; then | |
SortByExt "$FILE" "$EXT" | |
else | |
SortByDate "$FILE" | |
fi | |
if [[ "$LIMIT" -gt 0 && "$COUNT" -ge "$LIMIT" ]]; then | |
LogMessage "sorting limit reached" | |
break | |
fi | |
done | |
LogMessage "sorting complete: moved $COUNT files" | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Sorts files recursively by file extension (one directory deep). | |
# | |
# TODO: Set the permissions on this file to 700. | |
# chmod 700 sort-recursive.sh | |
# TODO: Create symlinks for use with this script. | |
# ln -s sort-recursive.sh sort-recursive | |
# ln -s sort-recursive.sh sort-recurs | |
# ln -s sort-recursive.sh sort-more | |
# File extension (optional parameter). | |
EXT="${1^^}" | |
# Number of files moved. Value should be 0. | |
COUNT=0 | |
# Limits number of files moved. To disable, set value to 0. | |
LIMIT=0 | |
# Prevents files from being moved. To disable, set value to 0. | |
TESTMODE=0 | |
# Param 1: Message string | |
function LogMessage(){ | |
if [[ $TESTMODE -gt 0 ]]; then | |
echo "[TESTMODE] $1" | |
else | |
echo "$1" | |
fi | |
} | |
# Start sorting... | |
if [[ ! -z "$EXT" ]]; then | |
LogMessage "Recursively sorting $EXT files into subfolders..." | |
else | |
LogMessage "Please specify a file extension to sort by." | |
exit 1 | |
fi | |
for DIR in */ ; do | |
if [[ ! -d "$DIR" ]]; then | |
continue | |
fi | |
cd "$DIR" | |
LogMessage "current directory: '$(pwd)'" | |
if [[ $TESTMODE -le 0 ]]; then | |
sort-files "$EXT" | |
fi | |
cd .. | |
COUNT=$((COUNT + 1)) | |
if [[ "$LIMIT" -gt 0 && "$COUNT" -ge "$LIMIT" ]]; then | |
LogMessage "sorting limit reached" | |
break | |
fi | |
done | |
LogMessage "sorting complete: sorted files in $COUNT folders" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment