This gonna be a little bit tricky, I guess π
When I open a terminal in the folder and run a script, that script should:
- move all content of all subfolders to a top parent folder (recursively)
- delete all empty folders
Parent folder we are standing in will contain no folders, just content of all of them.
It should work for hidden files and folders as well. It would be immensely useful for house cleaning π
I've tried something like:
find . -type f -name "*" -depth | xargs mv ./
But with no luck.
Thank you for your help π
Best Answer
The simplest and cleanest way in my opinion would be to use this command:
find
command #1:.
: searches in the current working directory;-type f
: searches for files;-exec [...]
: executes either a command for each result (using{} [...] \;
) or a command for all the results (using{} +
);mv --backup=numbered . \;
: moves each result in the current working directory, creating a backup of files with the same filename already in the current working directory;find
command #2:.
: searches in the current working directory;-maxdepth 1
: searches only in the first level of the target directory hierarchy;-type d
: searches for directories;-exec [...]
: executes either a command for each result (using{} [...] \;
) or a command for all the results (using{} +
);rm -r {} +
: deletes each result;Test on a test directory hierarchy: