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.comYou 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
...
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.
What you want is:
cp -R t1/. t2/
The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.
cp dir1/* dir2
Or if you have directories inside dir1 that you'd want to copy as well
cp -r dir1/* dir2
How to copy in bash all directory and files recursive? - Stack Overflow
What command do I use to copy all files in a directory without copying the directory itself?
Copy folder and files inside directory to another directory, which are modified within the last 30 minutes
bash - How to copy files from the folder without the folder itself - Unix & Linux Stack Exchange
Videos
cp -r ./SourceFolder ./DestFolder
code for a simple copy.
cp -r ./SourceFolder ./DestFolder
code for a copy with success result
cp -rv ./SourceFolder ./DestFolder
code for Forcefully if source contains any readonly file it will also copy
cp -rf ./SourceFolder ./DestFolder
for details help
cp --help
Title says it all. cp -R copies the directory and the files, but I only want to copy the directory contents.
cp -r /path/to/someDirA/folderTwo /path/to/someDirB/
The -r option to cp tells it to recurse on directories, copying its contents.
You can use cp to copy files and directories:
cp -r /path/to/someDirA/folderTwo /path/to/someDirB
The -r option is needed when copying directories.
Alternatively, you can use rsync:
rsync -a /path/to/someDirA/folderTwo /path/to/someDirB
You can copy the contents of a folder /source to another existing folder /dest with the command:
cp -a /source/. /dest/
The -a option is an improved recursive option, that preserves all file attributes and symlinks.
The . at end of the source path is a specific cp syntax that copies all files and folders, including hidden ones.
An alternate is rsync:
rsync -a source/ destination
The advantages of rsync are:
- After the initial sync, it will then copy only the files that have changed.
- You can use it over a network, convenient for files in $HOME, especially config files.
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?
advanced cp
cp -r /home/username/A/. /usr/lib/B/
This is especially great because it works no matter whether the target directory already exists.
shell globbing
If there are not too many objects in the directory then you can use shell globbing:
mkdir -p /usr/lib/B/
shopt -s dotglob
cp -r /home/username/A/* /usr/lib/B/
rsync
rsync -a /home/username/A/ /usr/lib/B/
The / at the end of the source path is important; works no matter whether the target directory already exists.
find
mkdir -p /usr/lib/B/
find /home/username/A/ -mindepth 1 -maxdepth 1 -exec cp -r -t /usr/lib/B/ {} +
or if you don't need empty subdirectories:
find /home/username/A/ -mindepth 1 -type f -exec cp --parents -t /usr/lib/B/ {} +
(without mkdir)
If on a GNU system, from man cp:
-T, --no-target-directory
treat DEST as a normal file
This allows you to write cp -rT /home/username/A/ /usr/lib/B/ to do exactly the right thing.