I'm trying to create a bash function script that will allow me to create multiple directories with the names below. I'm also trying to organize files by extension into their corresponding folders (i.e. .jpg into pictures, .doc into documents, .gif into media, etc). The first part is fine it's the second half after the directories are made that confuses me.
#!/bin/bash
echo "Creating directory categories"
function make_folder
{
cd -; cd content; sudo mkdir ./$1
}
make_folder "documents"
make_folder "other"
make_folder "pictures"
make_folder "media"
echo "Directories have been made"; cd -
exit
ext="${filename##*.}" #set var ext to extension of files
find ./random -name | #find and list all files in random folder
#pipe results of find into if statement
if ext == ["jpg"; "jpeg"; "png"] #move ".jpg", etc to new destination
then
mv /path/to/source /path/to/destination
elif ext == [".gif"; ".mov"] #move ".gif", etc to new destination
then
mv /path/to/source /path/to/destination
else #move other files into to new destination
mv /path/to/source /path/to/destination
fi
for f in jpg mov; do files=(*."$f"); if [[ "${files[@]#*.}" == *jpg* ]]; then mv "${files[@]}" jpgdir/; else mv "${files[@]}" movies/; fi; done
This should get you going. If you prefer to useswitch
you can do so. Try to understand this one liner and see if it fits your situation – Valentin Bajrami Apr 13 '16 at 18:50if [[ "${files[@]#*.}" == *jpg* ]]; ....
will loop through the array and compare the extension. If the extension matches, then action is taken. – Valentin Bajrami Apr 13 '16 at 19:08