5

I have this directory structure:

parent_folder/child_folder/

I want to copy the contents of the child folder to the parent folder, but I don't know how to do it with cp.

1

4 Answers 4

5

If you want to copy all content then run the command below

cp path-of-child-folder/* path-of-parent-folder

If particular content then run the command

cp path-of-child-folder/content path-of-parent-folder

2
  • 3
    FYI this does NOT work if the child folder contains hidden files (like .git)
    – Brandon
    Commented Feb 20, 2021 at 8:48
  • Use cp -r path-of-child-folder/. path-of-parent-folder to include hidden files. Commented May 9, 2022 at 9:00
1

You can use this simple sequence of commands (assuming that parent_folder is an absolute path):

cd parent_folder
cp -R child_folder/* .

The second command is the essential one.
And if you want to remove the child_folder after the copy procedure, use

rm -R child_folder

Now your parent_folder contains both the contents of itself plus the contents of child_folder.

Note that this doesn't flatten the directory structure on the child_folder.

0

In zsh:

cp -r child/*(D) parent/

This covers hidden files, as well.

0

It's simple as that:

cp -R file_name $(pwd)/../

This command will copy your file, and it will print the current directory and go to the parent folder.

If you want to access the grandparent folder, you just need to add one more "../"

EXAMPLE:

To copy the file to the parent folder:

cp -R file_name $(pwd)/../

To copy the file to the grandparent folder:

cp -R file_name $(pwd)/../../

To copy the file to the great-grandparent folder :

cp -R file_name $(pwd)/../../../

etc.

1
  • You don't even need this $(pwd) stuff. After all, you're already in that directory. .. by itself will refer to that directory's parent directory (and so on) just fine.
    – muru
    Commented Jul 18, 2021 at 2:40

You must log in to answer this question.

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