Is there a command line one-liner to move all the contents of all the direct subfolders of a folder to the folder itself and deleting the subfolders (which are now empty)? I.e., eliminate a level in the folder hierarchy?
To be absolutely clear, the deeper subfolder structure should be preserved.
I've run into this a couple of times now, and I was a little disappointed this doesn't seem to be a feature of Dolphin, nor something other people are struggling with.
I suspect this may have to be split in two, moving the contents of the subfolders, and deleting the subfolders. However, this may have the undesired side-effect of deleting empty second-level sub-folders.
Actually not a real one-liner:
First find everything in the subfolder with exact depth 2 and move it to
.
. Then find and delete all empty folders in the current directory.original_filename.~n~
(n meaning 1, 2, 3 ...).Update:
I'm not happy with the solution above. Too many caveats and problems. For example when --backup=numbered renames the parent folder of your folder you want to move ...
Better use a temporary directory to avoid problems. The temp dir should be on the same file system to avoid the need to copy the files.
To guard against deleting empty subdirectories, IMO the simplest way is to move to a temporary directory and then rename the temporary directory to the old one. Say the directory in question is
foo
, and you wantfoo/*/*
to becomefoo/*
. Do something like:--remove-files
, the firsttar
will delete the files as it processes them, finally deletingfoo
itself.--strip-components=2
, the secondtar
will chop off thefoo/*/
from paths it is extracting, sofoo/a/b
will becomeb
.--backup=t
is the same as withcp
ormv
- make numbered backup copies. However,mv
makes copies of directories if they have the same name, buttar
will merge the directories and make backups of files.--one-top-level=temp
tellstar
to create a directory namedtemp
and extract files there.For example:
And after running the
tar
commands:With the
tar
output, you can see whentar
renamed files for backing up, allowing you track which files became which backups.I'm mildly surprised that it works, but you can actually provide the same directory name for
--one-top-level
and eliminate need to rename. Just this pipeline is enough:Move everything in subdirectories into the current directories. rmdir won't delete directories unless they're empty so the second part only deletes empty directories.