Sunday, 5 December 2010

Code to sort your Linux downloads directory. I run this every day.

#!/bin/bash

# Set these two variables to the full pathname
# of your holding and storage directories

downloads=/home/akendrick451/Downloads


cd $downloads


#make sure we have our directories created
mkdir -p Music
mkdir -p Zips
mkdir -p Applications
mkdir -p Documents
mkdir -p Images
mkdir -p Scripts
mkdir -p Other

ls | while read file
do
filetype=$(file -ib "$file")
#echo $file $filetype
# echo "$filetype"

if [ "$filetype" = "application/octet-stream; charset=binary" ]; then
echo "music" $file $filetype
blMove=true
strFolder="Music"
elif [ "$filetype" = "application/x-directory; charset=binary" ]; then
blMove=false
elif [ "$filetype" = "application/zip; charset=binary" ]; then
blMove=true
strFolder="Zips"
elif [ "$filetype" = "application/x-gzip; charset=binary" ]; then
blMove=true
strFolder="Zips"
elif [ "$filetype" = "application/x-bzip2; charset=binary" ]; then
blMove=true
strFolder="Zips"
elif [ "$filetype" = "image/jpeg; charset=binary" ]; then
blMove=true
strFolder="Images"

elif [ "$filetype" = "image/png; charset=binary" ]; then
blMove=true
strFolder="Images"
elif [ "$filetype" = "image/x-ico; charset=binary" ]; then
blMove=true
strFolder="Images"

elif [ "$filetype" = "image/gif; charset=binary" ]; then
blMove=true
strFolder="Images"


elif [ "$filetype" = "application/pdf; charset=binary" ]; then
blMove=true
strFolder="Documents"
elif [ "$filetype" = "text/html; charset=utf-8" ]; then
strFolder="Documents"
blMove=true
elif [ "$filetype" = "text/plain; charset=us-ascii" ]; then
strFolder="Documents"
blMove=true


elif [ "$filetype" = "application/msword; charset=binary" ]; then
strFolder="Documents"
blMove=true

elif [ "$filetype" = "application/vnd.oasis.opendocument.text; charset=binary" ]; then
strFolder="Documents"
blMove=true

elif [ "$filetype" = "text/html; charset=us-ascii" ]; then
strFolder="Documents"
blMove=true

elif [ "$filetype" = "text/x-shellscript; charset=us-ascii" ]; then
strFolder="Scripts"
blMove=true


elif [ "$filetype" = "text/x-php; charset=us-ascii" ]; then
strFolder="Scripts"
blMove=true





elif [ "$filetype" = "application/vnd.ms-office; charset=binary" ]; then
blMove=true
strFolder="Documents"
else
blMove=true
strFolder="Other"
#echo "doc?" $file $filetype
fi

if [ $blMove = true ]; then
echo Moving... $file to $strFolder
blMove=false
mv "$file" $strFolder/"$file"
fi

done

No comments: