1

I have data arranged as follows:

--parent_dir
   |
   --4Bz27go1c
      |
      -- foo.md
      -- bar.tar.gz
      -- baz.c
      ..

I have a parent directory with a known name and a child directory with an unknown name (represented here as 4Bz27go1c. How can I copy all the contents from inside the child directory and move it up to inside the parent directory and also delete the now empty child directory?

To be clear, the final result that I'm describing should look like this:

 --parent_dir
    |
    -- foo.md
    -- bar.tar.gz
    -- baz.c
    ..
2

If you are in the child then: Using bash shell,

shopt -s dotglob
mv -t .. *
rmdir $(pwd)

If your mv does not have -t then:

shopt -s dotglob
mv * ..
rmdir $(pwd)
  • rmdir . won't work. -t implies GNU mv. – Stéphane Chazelas Feb 24 '15 at 20:50
  • rmdir . produces an error: Invalid argument. – turtle Feb 24 '15 at 20:55
  • Updated to not need -t. And strangely rmdir . does not work, don't know why. However rmdir $(pwd) does. – ctrl-alt-delor Feb 24 '15 at 20:56
0
cd parent_dir &&
  set -- */ &&
  for dir do
    [ -L "$dir" ] && continue
    mv -i -- "$dir"* "$dir"..?* "$dir".[!.]*
    rmdir -- "$dir"
  done

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.