Bash: mv directory one at a time

NinjaCat asked:

I am trying to move all subdirectories of a folder to another share on the same server. If I do a mv *, I will run out of space since the folders are not removed until all folders get transferred. So I’d like to create a short script that loops through each one. Does any one have an example that I can look at? I’ve searched around but can’t find exactly what I am looking for.

My answer:


You want for.

An example (this will just show what will be done):

for item in *; do
    echo mv "$item" /destination/directory
done

When you’re happy, remove echo to do it for real.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.