0

For Example,

I have 5 directories inside parent directory, I want to delete parent directory without deleting 5 subdirectories.

As of now am doing with below code,

 mv parentFolder/* parentFolder/.. && rm -rf parentFolder

Do we have any other simpler option than this?

1
mv parentFolder/* parentFolder/..

Would move all the (non-hidden) files, not only those of type directory.

To move only the directories, with zsh (also shortening the code)

(){mv $1/{*(/),..} && rm -rf $1} parentFolder

Or to include hidden ones:

(){mv $1/{*(D/),..} && rm -rf $1} parentFolder
  • (){..$1..} arg: anonymous function with an argument (to factorise the parentFolder)
  • {a,b} csh-style brace expansion, also to factorize
  • (/): glob qualifier to select only directories. You could also do */ (which would also work in bash, but would also include symlinks to directories).
  • (D/): D for dot-files (hidden files whose name starts with .).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.