How can I remove all subdirectories but keep those files in the parent directory?
I am using ksh on AIX and my directory structure looks like below.
dir/ subdir1/file1 subdir2/file2 subdir3/file3
I want to remove all subdirectories subdir1
, subdir2
etc. I want to save files file1
, file2
etc. In dir
. On the other hand, I want to move all files in subdirs
to dir
and then delete all empty ones subdirs
. So the end result will be:
dir/ file1 file2 file3 ....
Which command should you use?
source to share
I would just use a command find
that should work in any shell:
find ./dir -mindepth 2 -type f -exec mv {} ./dir/ \;
It starts with ./dir
and finds files below the first level ./dir
and then moves those files to ./dir
.
To remove empty subdirectories use:
find ./dir -mindepth 1 -type d -empty -delete
Warning . This will overwrite files with the same name if file name conflicts occur.
source to share