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.
-
2Out of curiosity: I would have thought that moving directories within a partition is "instantaneous", no matter how full the directory is? – Hagen von Eitzen Apr 17 '14 at 16:24
-
Not if you do it across a network connection. Then all files are downloaded and uploaded again. – MSalters Apr 18 '14 at 12:18
-
I had this issue when moving files on 1 host between two folders that happened to be in two different ZFS datasets (in the same pool), and thus treated like different filesystems. Something to bear in mind. – Programster Apr 30 '17 at 14:00
5 Answers
You could use rsync(1)
:
rsync --remove-source-files /path/to/source /path/to/destination
This will remove successfully transferred files from the original path.

- 15,096
- 3
- 42
- 61
-
2+1 For using an appropriate tool for an appropriate job, not every task is best done in a quickly made bash script – Vality Apr 18 '14 at 16:30
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.

- 244,070
- 43
- 506
- 972
Using the mv
command to move files from one volume to a different volume is a copy operation. But how would you run out of space on the source volume? You would only run out of space on the target volume if that volume is smaller than the total size of the files you're moving. But either way, you're only deallocating space on the source volume, not allocating more space on it.
If you use mv
to "move" files from one directory to another on the same volume, that's just a rename operation. You're not copying data, you're just adjusting file pointers to present a different directory hierarchy. You're not going to run out of space because the file data stays right where it was.
Either way, I'm not sure I see the problem. :-) Have you actually been trying this and running out of space, or are you just trying to plan ahead?

- 488
- 3
- 14
-
I think the user is using a network file system to export two different directory trees on the same real partition, to copy between them, he is for some reason sending them from one network file system to the other over TCP despite them being on the same disk... Odd I know, but it would cause this issue... – Vality Apr 18 '14 at 16:35
-
I wondered about that, and that *would* cause temporary duplication since the operation couldn't just monkey with file pointers. But if that's the case, it seems as if it would be much better to get a shell session directly on that machine and use `mv` the efficient way. – Craig Tullis Apr 18 '14 at 23:14
ls -1 | xargs -n1 -i echo mv '{}' destination
Just remove the echo when happy.

- 7,057
- 23
- 35
Just for reference, I believe the mv
command doesn't do a copy then delete operation, ie having
mv /home/foo /home/bar
exist simultaneously for a short period. It operates more like renaming. Essential changing the filesystem accounting so that what pointed at /home/foo
now points to /home/bar
without actually physically moving the data on disk from one place to another, or copying then deleteing from the previous location.

- 387
- 2
- 3
-
1I think he is copying between different file-systems, not just moving a directory to a new path. – Vality Apr 18 '14 at 16:36