2

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 Answers 2

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)
3
  • rmdir . won't work. -t implies GNU mv. Feb 24, 2015 at 20:50
  • rmdir . produces an error: Invalid argument.
    – turtle
    Feb 24, 2015 at 20:55
  • Updated to not need -t. And strangely rmdir . does not work, don't know why. However rmdir $(pwd) does. Feb 24, 2015 at 20:56
0
cd parent_dir &&
  set -- */ &&
  for dir do
    [ -L "$dir" ] && continue
    mv -i -- "$dir"* "$dir"..?* "$dir".[!.]*
    rmdir -- "$dir"
  done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .