3

This question already has an answer here:

I want to delete my parent directory but keep all of its content. Currently my directory structure

$ /usermanagement/usermanagement/file1

After deleting my parent directory I want to see like bellow structure

$ /usermanagement/file1

marked as duplicate by roaima, αғsнιη, GAD3R, G-Man, peterh Sep 29 '17 at 20:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

May be I didn't understand your question properly. But the answer of what I have gathered from your question is,

Assuming you are in current directory, which name is nested and parent directory name is test.

$ cp -Rp . ../;cd ../;rm -rf nested

Explanation


  1. cp -Rp . ../ - This will copy all files including directory, nested directory and hidden files. Where,

    • -R flag is used for "copy directories recursively"

    • -p is to "preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all",

    • . is for indicating of current directory as source

    • .. indicating as parent directory as destination.

  2. cd ../ - This will move you from current to parent directory

  3. rm -rf nested - This will remove all the files including directories, nested directories and hidden files.

Sources: Man page

  • 1
    For directories with lots of data, this will take much longer than moving/renaming. If the data is very large, the cp may fail. The final rm is going to run whether there were problems or not. – BowlOfRed Sep 29 '17 at 20:40
  • Ok, that's scary. I have copied 1 tb of data with cp. Yes that may be slower but I didn't experience a scenario where cp failed. – muhammad Sep 30 '17 at 10:53
6

(assuming the directory you want to remove is the current working directory)

With zsh:

cd -P . &&
  mv -- *(D) .. &&
  cd -P .. &&
  rmdir ~-

With bash:

cd -P . &&
  (shopt -s dotglob && mv -- * ..) &&
  cd -P .. &&
  rmdir "$OLDPWD"

cd -P . makes sure the current working directory as stored in $PWD is symlink-free. We want to move all files and directories into the parent, including the hidden ones (hence the (D) globbing flag and dotglob options).

If that worked we change the current directory one level up and remove our previous directory.

(you may want to add a -i option to mv to get a chance to cancel moves that would overwrite files)

0

A simpler solution than the ones above is:

mv /usermanagement/usermanagement /usermanagement.tmp
rmdir /usermanagement
mv /usermanagement.tmp /usermanagement

I.e. move the lower usermanagement directory to the root directory with a temporary name, remove the (now empty) upper usermanagement directory, and finally rename the /usermanagement.tmp directory to /usermanagement.

0

Probably the most simple solution:

$ mv /usermanagement/usermanagement/* /usermanagement/ && rmdir /usermanagement/usermanagement
  • Depends on what you mean by simplest. If you have a million files in the directory, you would have a million calls to rename, except that you would probably get an argument too long error. You would also have to watch out for a file or directory named usermanagement in the subdirectory. – Johan Myréen Sep 29 '17 at 19:42

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