Last active
October 25, 2016 21:27
-
-
Save ronsims2/c16b1e9969f0231861a37e6c9427307e to your computer and use it in GitHub Desktop.
A shell script that archives files
This file contains hidden or 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 | |
#A list of file names | |
clear | |
#Read by line to handle filenames with spaces | |
OGIFS = $IFS | |
IFS=$(echo -en "\n\b") | |
myfiles=$(ls) | |
#A count of the files being processed, used to determine when new folder should be created | |
filecount=1 | |
filelimit=25 | |
folderprefix="archivo_" | |
foldersuffix=1 | |
filename="" | |
foldername="$folderprefix$foldersuffix" | |
$scriptlimit=10000 | |
if [ -d "$foldername" ]; then | |
echo "Directory found." | |
else | |
echo "No directory found so a new one was created." | |
mkdir "$foldername" | |
fi | |
for myfile in $myfiles | |
do | |
#exit to prevent script from running too long | |
if [ $filecount -gt $scriptlimit ]; then | |
exit 0 | |
fi | |
filename="$myfile" | |
#escape conditions | |
if [[ "$myfile" = "\$"* ]]; then | |
filename="\$myfile" | |
fi | |
if [[ "$myfile" = "\."* ]]; then | |
filename="\$myfile" | |
fi | |
((filecount++)) | |
if [ "$filecount" -gt "$filelimit" ]; then | |
echo "----------$foldername" | |
filecount=1 | |
((foldersuffix++)) | |
foldername="$folderprefix$foldersuffix" | |
if [ -d "$foldername" ]; then | |
echo "Directory found." | |
else | |
echo "No directory found so a new one was created." | |
mkdir "$foldername" | |
fi | |
fi | |
echo "item $myfile moved to: $foldername" | |
mv "$myfile" "$foldername/$myfile" | |
done | |
#Restore default | |
IFS=$OGIFS | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment