So I am trying to sort files with specific extensions into specific folder (ones that have been chosen by user by command line arguments)
Lets say $1 (.jpg) $2 (.docx) etc
The script is all working and fine, but I am trying to write a loop that sorts these files into their folders (just simply based on their extensions, so .jpg into jpg folder, so basically mv .$1 $1
)
How can I write the loop so it will always add +1 into the command line argument until there is no more command line argument (lets say there are 5) and when there is no more argument, simply moves the unassigned files into a selected folder for it?
Here what i was trying to do
function sorting {
count = 1
while [ $count -le 5 ]; do
mv .$count $count
count=$((count +1))
done
}
Then we I tried to call the function in the script I used sorting
mv
command needs a wildcard, and at least some of the variable references should be double-quoted. Also, I always recommendmv -i
or-n
when doing bulk/automated moves, to avoid accidentally overwriting files in case of name conflicts. Something like:mv -i *.$1 "$1"
. Also, run your script through shellcheck.net; it's good at pointing out common mistakes.