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.
A GUI file manager might be a good option, shouldn't have any surprises – Xen2050 – 2018-04-12T00:49:43.107