Home > Enterprise >  how to move Files to a folder using mv in bash with condotions?
how to move Files to a folder using mv in bash with condotions?

Time:07-02

I was writing this code to organize my downloads folder so it wouldn't be messy. i want the files to be moved to their proper folder like for mp4 it should go to the Videos folder. but the code is not working

#! /bin/bash

if [[ -d Videos/ ]] && [[ -d Compressed/ ]] && [[ -d Music/ ]] && [[ -d Pictures/ ]] && [[ -d Executables/ ]] && [[ -d Misc/ ]]
then
    #Compressed Files
    if [[ -f *.7z ]]
    then
        mv *.7z Compressed/
    fi

    if [[ -f *.rar ]]
    then
        mv *.rar Compressed/
    fi

    if [[ -f *.zx ]] 
    then
        mv *.xz Compressed/
    fi

    if [[ -f *.zip ]]
    then
        mv *.zip Compressed/
    fi

    if [[ -f *.gz ]]
    then
        mv *.gz Compressed/
    fi

    if [[ -f *.iso ]]
    then
        mv *.iso Compressed/
    fi
    #Videos
    
    if [[ -f *.mp4 ]]
    then
        mv *.mp4 Videos/
    fi

    if [[ -f *.mov ]]
    then
        mv *.mov Videos/
    fi

    if [[ -f *.wvm ]]
    then
        mv *.wvm Videos/
    fi

    if [[ -f *.mkv ]]
    then
        mv *.mkv Videos/
    fi

    #Musics
    if [[ -f *.mp3 ]]
    then
        mv *.mp3 Music/
    fi

    if [[ -f *.wav ]]
    then
        mv *.wav Music/
    fi

    if [[ -f *.ogg ]]
    then
        mv *.ogg Music/
    fi

    #Pictures
    if [[ -f *.png ]]
    then
        mv *.png Pictures/
    fi

    if [[ -f *.jpg ]]
    then
        mv *.jpg Pictures/
    fi

    if [[ -f *.jpeg ]]
    then
        mv *.jpeg Pictures/
    fi

    if [[ -f *.svg ]]
    then
        mv *.svg Pictures/
    fi

    if [[ -f *.webp ]]
    then
        mv *.webp Pictures/
    fi

    #documents
    if [[ -f *.doc ]]
    then
        mv *.doc Documents/
    fi

    if [[ -f *.docx ]]
    then
        mv *.docx Documents/
    fi

    if [[ -f *.pdf ]]
    then
        mv *.pdf Documents/
    fi

    if [[ -f *.pptx ]]
    then
        mv *.pptx Documents/
    fi

    if [[ -f *.ppt ]]
    then
        mv *.ppt Documents/
    fi

    if [[ -f *.xls ]]
    then
        mv *.xls Documents/
    fi

    if [[ -f *.xlsx ]]
    then
        mv *.xlsx Documents/
    fi

    #Executables
    if [[ -x *.64 ]]
    then
        mv *.64 Executables/
    fi

    if [[ -f *.deb ]]
    then
        mv *.deb Executables/
    fi

    if [[ -f *.rpm ]]
    then
        mv *.rpm Executables/
    fi

    #Misc FIles (Unknown file formats)

else
    echo "No Directories found"
    sleep 1
    echo "Creating Directories"
    mkdir Videos/ Compressed/ Music/ Pictures/ Executables/ Documents/ Misc/
    sleep 1
    echo "moving files"
    #Compressed Files
    if [[ -f *.7z ]]
    then
        mv *.7z Compressed/
    fi

    if [[ -f *.rar ]]
    then
        mv *.rar Compressed/
    fi

    if [[ -f *.zx ]] 
    then
        mv *.xz Compressed/
    fi

    if [[ -f *.zip ]]
    then
        mv *.zip Compressed/
    fi

    if [[ -f *.gz ]]
    then
        mv *.gz Compressed/
    fi

    if [[ -f *.iso ]]
    then
        mv *.iso Compressed/
    fi

    #Videos
    
    if [[ -f *.mp4 ]]
    then
        mv *.mp4 Videos/
    fi

    if [[ -f *.mov ]]
    then
        mv *.mov Videos/
    fi

    if [[ -f *.wvm ]]
    then
        mv *.wvm Videos/
    fi

    if [[ -f *.mkv ]]
    then
        mv *.mkv Videos/
    fi

    #Musics
    if [[ -f *.mp3 ]]
    then
        mv *.mp3 Music/
    fi

    if [[ -f *.wav ]]
    then
        mv *.wav Music/
    fi

    if [[ -f *.ogg ]]
    then
        mv *.ogg Music/
    fi

    #Pictures
    if [[ -f *.png ]]
    then
        mv *.png Pictures/
    fi

    if [[ -f *.jpg ]]
    then
        mv *.jpg Pictures/
    fi

    if [[ -f *.jpeg ]]
    then
        mv *.jpeg Pictures/
    fi

    if [[ -f *.svg ]]
    then
        mv *.svg Pictures/
    fi

    if [[ -f *.webp ]]
    then
        mv *.webp Pictures/
    fi

    #documents
    if [[ -f *.doc ]]
    then
        mv *.doc Documents/
    fi

    if [[ -f *.docx ]]
    then
        mv *.docx Documents/
    fi

    if [[ -f *.pdf ]]
    then
        mv *.pdf Documents/
    fi

    if [[ -f *.pptx ]]
    then
        mv *.pptx Documents/
    fi

    if [[ -f *.ppt ]]
    then
        mv *.ppt Documents/
    fi

    if [[ -f *.xls ]]
    then
        mv *.xls Documents/
    fi

    if [[ -f *.xlsx ]]
    then
        mv *.xlsx Documents/
    fi

    #Executables
    if [[ -x *.64 ]]
    then
        mv *.64 Executables/
    fi

    if [[ -f *.deb ]]
    then
        mv *.deb Executables/
    fi

    if [[ -f *.rpm ]]
    then
        mv *.rpm Executables/
    fi

    #Mics FIles (Unknown file formats)
fi

I guess it's from 'if' statement but i don't know how to fix it because i'm quite new to bash. the mkdir is working but all of the "if [[ -f *.mp4 ]]" was not.

CodePudding user response:

Filename expansion is not performed within the bash compund command [[...]]. So, for instance, *.7z in [[ -f *.7z ]] means literally a filename *.7z. That's why your script won't work. The script below may be what you want:

#!/bin/bash

mkdir -p Videos Compressed Music Pictures Documents Executables Misc || exit

for f in ./*.{7z,rar,zx,zip,gz,iso}; do
    [[ -f $f ]] && mv "$f" Compressed
done

for f in ./*.{mp4,mov,wvm,mkv}; do
    [[ -f $f ]] && mv "$f" Videos
done

for f in ./*.{mp3,wav,ogg}; do
    [[ -f $f ]] && mv "$f" Music
done

for f in ./*.{png,jpg,jpeg,svg,webp}; do
    [[ -f $f ]] && mv "$f" Pictures
done

for f in ./*.{doc,docx,pdf,pptx,ppt,xls,xlsx}; do
    [[ -f $f ]] && mv "$f" Documents
done

Notice the use of -p option in mkdir. The missing directories will be created and existing directories will be ignored without an error by this option.

  • Related