0

Is it possible to move files by order of size?

Like:

mv /path/to/x /path/to/y

Where x and y are directories. However, smaller files are moved first. Or bigger files first.

Also, is there a reason not to do this?

1
  • 1
    It is not trivially done. Is there are reason to do this? May 28, 2012 at 18:32

3 Answers 3

1

You could do it like this (untested):

for file in `ls --sort=size /path/to/x/*`; do mv $file /path/to/y/; done

You can reverse the sort by adding the --reverse parameter to ls. If the filenames contain spaces, you might need to quote $file:

... mv "$file" /path/to/y/ ...

If the filenames might contain a double quote, you are in real trouble.

It's also possible that the following could work, but it depends on mv implementation and might also not work for very many files (and it still has problems with spaces and/or quotes in filenames):

mv `ls --sort=size /path/to/x/*` /path/to/y/

You ask: "is there a reason not to do this?" The question should be reversed. Is there a reason to do this? If you have a reason, go ahead and do it.

1

If you are moving the files within a single filesystem, then the data in the files does not get moved -- only the names of the files are moved between directories. Thus you can move a 10 gigabyte file instantly (within a single filesystem). Thus, to go to the trouble of finding the sizes of all of the files just so the smallest ones can be moved first would be a waste of time (in this case).

0

A reason not to do that is that it would require you to build an index of all files before moving. Then get the size of all those files and sort them. Before you can even start copying.

And then there is no guarantee that the state you calculated before the copy process is exactly the same during the copy process, thus, making it pointless.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .