Home / Programming / Bash / Simple Bash Script to Sort File Types into Directories

Simple Bash Script to Sort File Types into Directories

Often times I find myself needing to sort a lot of files into certain directories, for example I have downloaded a lot of free ebooks and a lot of them have different file types such as epub, mobi, azw, or even pdf and I want to keep them sorted into their respective folders to make it easier to pull up particular file types. To quickly do this I wrote a very simple and basic bash script that will allow me to do this.


array=( epub mobi pdf azw )

for i in "${array[@]}"
 echo "Moving books with file type: $i"
 find $sourcedir -type f -iname "*.$i" -print0 | xargs -0 -I file mv -v "file" "$bookdir/$i/"
 chown -R apache.apache "$bookdir/$i"

All it does is look for particular files that end in the extension that is in the array, and then move them into the bookdir/extension folder. So as an example if I had file1.epub in the /home/user/ directory, it would find that file and then move it to /books/epub.

This is a very basic script, and is just here to help people get the idea of how to automate tasks that you would normally do manually.

About geoff

Intelligence is the ability to avoid doing work, yet getting the work done. ~Linus Torvalds


  1. Hahaha… This is mine.


    cd /Users/username/Downloads/

    echo “Organizing your SHIT!”
    echo “”
    mv -v *.pdf PDF/
    mv -v *.epub PDF/
    mv -v *.torrent Torrents/
    mv -v *.rpm RPMs/
    mv -v *.gz tgz/
    mv -v *gz tgz/
    mv -v *tbz tgz/
    mv -v *.dmg DMG/
    mv -v *.bin BIN/
    mv -v *.zip ZIP
    mv -v *.conf configs/
    mv -v *.iso ISOs/
    mv -v *.doc* DOCS/
    mv -v *.png png/
    mv -v *.txt txt/
    mv -v *.mp3 mp3/
    mv -v *.jpg jpg/
    mv -v *.exe exe/
    mv -v *.wav wav/
    mv -v *.xml xml/
    mv -v *.cfg config/
    mv -v *.cfg configs/
    mv -v *.jar jar/
    mv -v *.ini ini/
    mv -v *.py py/
    mv -v *.pl perl/
    mv -v *.csv csv/
    mv -v *.pkg DMG/
    mv -v *.sh scripts/
    mv -v *.xls xls/
    mv -v *.xls* xls/
    mv -v *.war war/
    mv -v *.jpeg jpg/

    echo “”
    echo “Done!”

  2. Too funny, a bit long! I could even get fancy and make the array dynamic, but for me I didn’t need all that.

Leave a Reply

Your email address will not be published. Required fields are marked *