Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to move files by order of size?


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?

share|improve this question
It is not trivially done. Is there are reason to do this? – Daniel Andersson May 28 '12 at 18:32

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.

share|improve this answer

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).

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.