You can use cp with the -r (copy recursively) flag and specify the new directory name:

cp -r /path/to/directory /path/to/location/new-name

In the above command replace the given paths with the real ones.

For example, to copy stuff from my home directory to an existing directory named backup and name the new directory stuff-backup (if this directory already exists, note that stuff will be copied into it, not overwrite it), you run:

cp -r ~/stuff ~/backup/stuff-backup

~ is a shortcut for your home directory /home/$USER or /home/zanna in my case. You can omit it if the current working directory is your home directory - you can see this from your prompt

zanna@monster:~$
              ^--the ~ here is where I am - home!

You can add the -v (verbose) flag to make cp report each copy being performed:

$ cp -vr stuff backup/stuff-backup
'stuff/thing1' -> 'backup/stuff-backup/thing1'
'stuff/thing2' -> 'backup/stuff-backup/thing2
...
Answer from Zanna on askubuntu.com
Top answer
1 of 7
184

You can use cp with the -r (copy recursively) flag and specify the new directory name:

cp -r /path/to/directory /path/to/location/new-name

In the above command replace the given paths with the real ones.

For example, to copy stuff from my home directory to an existing directory named backup and name the new directory stuff-backup (if this directory already exists, note that stuff will be copied into it, not overwrite it), you run:

cp -r ~/stuff ~/backup/stuff-backup

~ is a shortcut for your home directory /home/$USER or /home/zanna in my case. You can omit it if the current working directory is your home directory - you can see this from your prompt

zanna@monster:~$
              ^--the ~ here is where I am - home!

You can add the -v (verbose) flag to make cp report each copy being performed:

$ cp -vr stuff backup/stuff-backup
'stuff/thing1' -> 'backup/stuff-backup/thing1'
'stuff/thing2' -> 'backup/stuff-backup/thing2
...
2 of 7
16

The command you need is simply cp which stands for "copy".

You can use it for example with one of these syntaxes:

cp SOURCEFILE TARGETFILE
cp SOURCEFILE TARGETDIRECTORY

The first variant allows you to specify a new file name for the target file, while the second variant creates a copy with the same name in the target directory. You must of course substitute the place holders in capital letters with valid paths first.

However, cp by default operates on files only, not on directories. To get it to copy a complete directory with all its content recursively, you must add the -r option:

cp -r SOURCEDIRECTORY TARGETDIRECTORY

You can learn more about the cp command by typing man cp in your terminal.

Discussions

How to copy in bash all directory and files recursive? - Stack Overflow
I have script: find ./SourceFolder/ -maxdepth 4 -exec cp -R '{}' ./DestFolder/ \; SourceDir contains also sub-folders. Problem that in DestFolder not only all tree, but in up level all another le... More on stackoverflow.com
🌐 stackoverflow.com
What command do I use to copy all files in a directory without copying the directory itself?
rsync -aPh source destination copies both the directory “source" and its contents. rsync -aPh source/ destination copies only the contents of “source”. More on reddit.com
🌐 r/linuxquestions
13
9
October 28, 2020
Copy folder and files inside directory to another directory, which are modified within the last 30 minutes
Seems all you are seeking is rsync. Could be as simple as: rsync -a /home/user/abc/* /home/user/xyz/ More on reddit.com
🌐 r/linuxquestions
6
3
December 17, 2020
bash - How to copy files from the folder without the folder itself - Unix & Linux Stack Exchange
The / at the end of the source ... target directory already exists. ... The first one works just fine! Any ideas why home/username/A/* (with star-symbol) doesn't make sense? Variant with dot at the end helped me, thanks! ... shopt is bash specific. With zsh, use *(D). with ksh93, FIGNORE='@(.|..)'. cp -t is GNU specific. The find one will not work properly as it will copy both A/ and its content (including ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
January 25, 2015
🌐
W3Schools
w3schools.com › bash › bash_cp.php
Bash cp - Copy Files and Directories
This is useful for tracking the ... copy_of_my_file.txt 'my_file.txt' -> 'copy_of_my_file.txt' The -r option lets you copy entire directories, including all files and subdirectories....
🌐
nixCraft
cyberciti.biz › nixcraft › howto › commands › linux – how to copy a folder [ command line option ]
Linux - How To Copy a Folder [ Command Line Option ] - nixCraft
April 15, 2022 - The cp command is a Linux command for copying files and directories. The syntax is as follows: cp source destination cp dir1 dir2 cp -option source destination cp -option1 -option2 source destination · In this example copy /home/vivek/letters/ ...
Top answer
1 of 1
5

In most cases it doesn't matter. Instead, 'cp' decides based on whether the destination exists.

Assuming the source is always a directory, and you're using either cp -r or cp -a:

  1. If the source is a symlink to a directory, then the trailing slash after the source is important. Without it, you'd be only copying the symlink itself and not the directory it points to.

    If the source isn't a symlink, the trailing slash after the source doesn't matter to 'cp'.

  2. If the target points to an existing directory, then the whole source directory will be copied into the target. That is, cp -r abc def will cause def/abc/text.txt to be created. Trailing slashes on the source nor the destination do not matter.

    If the target doesn't exist, then the source directory will be copied as the target. That is, cp -r abc def will only create def/text.txt. Trailing slashes don't affect anything here, either.

The 2nd "rule" can be annoying, as it seems like you cannot copy an existing directory's contents directly into another directory without resorting to something like abc/* (which will miss hidden files), etc.

However, an easy way to achieve that is to specify . as the last path component – this will trick cp into "creating" a . in the destination:

cp -r /some/path/. /another/path

Trailing slash on the destination still doesn't matter.

(Note that these rules are not universal for all programs; in particular, rsync behaves differently and trailing slashes indeed tell it whether to copy the whole directory or only its contents.)


In short:

  • If you want to copy the directory whole, ensure the destination already exists (pre-create it using mkdir).
  • If you want to copy the directory's contents, make sure to end the source with /. (slash + dot) (like the website).
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › how-to-copy-a-directory-in-linux-with-the-cp-command
How to Copy a Directory in Linux with the cp Command
May 24, 2023 - To copy a directory and its contents, we need to include the -r (or --recursive) option, which enables recursive copying. This option ensures that all subdirectories and files within the directory are copied.
🌐
Sentry
sentry.io › sentry answers › linux › copy folder with files to another folder in linux
Copy folder with files to another folder in Linux | Sentry
To copy a folder with its contents, we must perform a recursive copy. We can do the following: ... The -a flag is equivalent to -dr --preserve=all. This does the following: ... In the command, we use source/. instead of source to take advantage ...
🌐
IBM
ibm.com › docs › en › aix › 7.2.0
Copying directories (cp command)
Use the cp command to create a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parameters into the file or directory specified by the TargetFile or TargetDirectory parameters.
🌐
Turbogeek
turbogeek.co.uk › home › linux › [solved] how to copy a directory (and its contents) in linux
[SOLVED] How to Copy a Directory (and its contents) in Linux
April 15, 2023 - Replace /path/to/source/directory with the path to the directory you want to copy and /path/to/destination/directory with the path to the location where you want to copy the directory. Press Enter. The cp command will copy the directory and ...
🌐
EaseUS
easeus.com › data recovery resource › how to copy linux directory and contents
4 Ways to Copy Directory and Contents to Another Location (Linux) - EaseUS
February 12, 2026 - The only addition is that you need to use the wildcard character, so all the files are copied. Step 1.To copy directory content on Linux, you can use the cp command with the -R option.
🌐
Linuxize
linuxize.com › home › linux commands › linux cp command: copy files and directories
Linux cp Command: Copy Files and Directories | Linuxize
February 22, 2026 - The cp command copies files and directories in Linux. This guide covers recursive copy, preserving attributes, no-overwrite, and practical examples.
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › cp-command-linux-examples
cp Command in Linux - GeeksforGeeks
4 days ago - We used `ls` command to display ... command cannot copy directories. To copy a directory and its contents, the -r or -R (recursive) option must be used....
🌐
Reddit
reddit.com › r/linuxquestions › copy folder and files inside directory to another directory, which are modified within the last 30 minutes
r/linuxquestions on Reddit: Copy folder and files inside directory to another directory, which are modified within the last 30 minutes
December 17, 2020 -

Hi guys,
I have two folders : /home/user/abc & /home/user/xyz

I run a script that generates a folder with files inside abc, so end result would be /home/user/abc/generatedFolder1/ with a bunch of files in it.

Is there a command I could use to copy this generatedFolder1 and the files in it to xyz, so the folder structure would remain the same? I did some googling around but most commands just would either omit the directory or copy the files too, to the xyz directory directly.

Stuff like :

find /home/user/abc/ -type d -mmin -30 -exec cp -pf {} /home/user/xyz/ \;

did not work, idk why. Can someone help me, Im a little new to the find command and am finding it a little confusing. I tried the above command with -r after the cp, and it just creates the abc folder recursively without stopping?