moving files and folders to a subfolder

1

I would like to move all files and folder from one directory to one of its subfolders. How do I do that?

I am using BusyBox and linux.

ex:

move all files and folder in /my/path/ to /my/path/subfolder/.

Copy, and then delete solutions are not affordable.

Thanks.

hebbo

Posted 2018-04-11T23:03:03.923

Reputation: 189

A GUI file manager might be a good option, shouldn't have any surprises – Xen2050 – 2018-04-12T00:49:43.107

Answers

0

After more digging and experimentation. I found the answer: -prune is used to avoid recusing into sub-directories. ! -name is used to exclude the target sub-directory, and then exec executes the move operation. The {} is replaced with file/directory names from the find command.

find /my/path/* -prune ! -name subfolder -exec mv {} /my/path/subfolder/. +

hebbo

Posted 2018-04-11T23:03:03.923

Reputation: 189

What find implementation can do this? Normally … -exec mv {} /my/path/subfolder/. + fails because you cannot separate {} and +, they must be at the very end: {} +; this is not the case with \;. – Kamil Maciorowski – 2018-04-12T04:57:35.037

This may be unnecessarily complex. – Raghu Ranganathan – 2018-04-12T05:04:07.877

I am using busyBox – hebbo – 2018-04-12T15:49:31.017

2

Solutions that use * (expanded by shell) won't work with too many objects in /my/path/. In such case you'll get:

argument list too long

This approach doesn't use *:

cd /my/path/ &&
find . -mindepth 1 -maxdepth 1 ! -name subfolder -exec mv -t subfolder/ {} +

Unfortunately -mindepth and -maxdepth options of find are not POSIX-compliant; neither -t option of mv is, I think.

This variant should satisfy POSIX:

cd /my/path/ &&
find . ! -name . -prune ! -name subfolder -exec mv {} subfolder/ \;

(I adapted this Unix & Linux SE answer). Sadly it calls mv for every object found, thus it's slow.


Fast alternative approach, if only you can create directories anew (initially neither /my/path/subfolder/ nor /my/subfolder/ should exist):

  • rename path/ to subfolder/,
  • recreate path/,
  • move subfolder/ into path/.

Note on inode-based filesystem this should be equally fast, no matter how many objects there are in path/. The code:

cd /my/ &&
test ! -e subfolder/ && mv path/ subfolder/ &&
mkdir path/ &&
mv subfolder/ path/

In this case I used && a lot to emphasize the procedure should abort if any of its steps fails. However this approach is inconvenient if you need path/ or subfolder/ to have non-default permissions, ownership etc.

Kamil Maciorowski

Posted 2018-04-11T23:03:03.923

Reputation: 38 429

1

mv * subfolder 

Of course, it will fail moving the "subfolder" directory into itself, but everything else will move

Gman Smith

Posted 2018-04-11T23:03:03.923

Reputation: 1 681

Is this in mv's specification? or is this hoping to be lucky? it may stop at first failure and I will end up with some of files and folder being moves but not all. – hebbo – 2018-04-11T23:18:16.507

@hebbo it will not stop at first failure – Gman Smith – 2018-04-12T00:29:09.007

>

  • excludes hidden files (or so testing shows me, though there's probably a setting for it somewhere)
  • < – Xen2050 – 2018-04-12T00:48:53.260

    Files with name starting with a dot (like .git) are excluded to. To hotfix this run mv .* subfolder (mv dot-start subfolder) – Matěj Kříž – 2019-08-06T12:14:06.520

    didnt work for me – Triamus – 2019-10-23T14:45:18.883

    0

    You might want to check out the mv command. You can try searching for all files and folders in a directory and exclude a sub-directory then copy all found to that sub-directory using find with the mv command.

    See a similar stack-overflow question https://stackoverflow.com/questions/4612157/how-to-use-mv-command-to-move-files-except-those-in-a-specific-directory

    CheesyMacHack

    Posted 2018-04-11T23:03:03.923

    Reputation: 66

    0

    The simplest way to do this is:

    mv !(subfolder) subfolder
    

    '!' means NOT, similar to programming languages, where mv will move all files and folders to the required subfolder with the exception of the subfolder.

    Additional things like moving hidden folders and dot folders are described here: https://askubuntu.com/questions/91740/how-to-move-all-files-in-current-folder-to-subfolder

    Raghu Ranganathan

    Posted 2018-04-11T23:03:03.923

    Reputation: 651

    This did not work with the version of find I am using. – hebbo – 2018-04-12T15:52:29.417